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
errorinstead 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,headersand at most one kind of body:json, encoded, withContent-Type: application/jsonunlessheadersset another;body, text sent in UTF-8; orbytes. With none of them the body is empty. If several are given,jsonwins, thenbody. -
respondEvents(
List< ServerSentEvent< events, {int status = 200, bool keepOpen = false, Map<Object?> >String, String> headers = const {}}) → void -
Answers with a stream of server-sent events, as an
EventStreamendpoint does:Content-Type: text/event-stream(unlessheadersset another), thenevents, one chunk each. Data that is not aStringis written as JSON. The stream ends after the last event, so a client that reconnects sends a new request; withkeepOpenit stays open until the client cancels. -
respondStream(
int status, Stream< List< body(), {Map<int> >String, String> headers = const {}}) → void -
Answers with
status,headersand the byte streambodyreturns;bodyis 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
handlerbuilds 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