MockResponseBuilder class

Sets how a MockHttpTransport answers one prepared request; returned by MockHttpTransport.when.

Call exactly one of its methods: a fixed response (respond), one computed from the request (respondWith), a stream (respondStream, respondEvents) or a failure (fail). A request matched before any of them was called throws a StateError.

// The remote service answers a created book with its location.
transport.when(HttpMethod.post, '/books').respondWith(
  (request) => HttpResponse(
    201,
    headers: HttpHeaders({'Location': '/books/978-0'}),
    request: request,
  ),
);

Spring (Java): ResponseActions.andRespond(...).

Properties

hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

fail(Object error) → void
Makes the request fail with error instead of answering, as a lost connection does; an HttpTransportException tests how the client retries or reports it.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
respond(int status, {Object? json, String? body, List<int>? bytes, Map<String, String> headers = const {}}) → void
Answers with status, headers and at most one kind of body: json, encoded, with Content-Type: application/json unless headers set another; body, text sent in UTF-8; or bytes. With none of them the body is empty. If several are given, json wins, then body.
respondEvents(List<ServerSentEvent<Object?>> events, {int status = 200, bool keepOpen = false, Map<String, String> headers = const {}}) → void
Answers with a stream of server-sent events, as an EventStream endpoint does: Content-Type: text/event-stream (unless headers set another), then events, one chunk each. Data that is not a String is written as JSON. The stream ends after the last event, so a client that reconnects sends a new request; with keepOpen it stays open until the client cancels.
respondStream(int status, Stream<List<int>> body(), {Map<String, String> headers = const {}}) → void
Answers with status, headers and the byte stream body returns; body is called again for each request, so every request gets a fresh stream. Use it for a slow or endless body.
respondWith(FutureOr<HttpResponse> handler(HttpRequest request)) → void
Answers with the response handler builds from the request, for an answer that depends on what was sent (an echo, a generated id).
toString() → String
A string representation of this object.
inherited

Operators

operator ==(Object other) → bool
The equality operator.
inherited