MockServerHttpResponse class final
An HTTP response kept in memory, so a test can read what the application answered: status, headers, cookies and body.
MockMvc passes one to the application for every request and the
test reads it from the result. Besides the full body (bodyText,
json) it can follow an answer that is still being streamed:
committed and closed tell where the response is, chunks and
eventStream deliver the body as it is written, and disconnect
plays a client that goes away.
// Calls the application directly and checks its answer.
final response = MockServerHttpResponse();
await handler.handle(
MockServerHttpRequest(HttpMethod.get, '/api/books/978-0'),
response,
);
expect(response.statusCode, 200);
expect((response.json! as Map)['title'], 'Dune');
Spring (Java):
MockHttpServletResponse response = mockMvc.perform(get("/api/books/978-0"))
.andReturn().getResponse();
assertEquals(200, response.getStatus());
- Implemented types
- Available extensions
Constructors
Properties
- bodyBytes → Uint8List
-
Every body byte written so far.
no setter
- bodyText → String
-
The body written so far, decoded as UTF-8 (malformed bytes
replaced).
no setter
-
chunks
→ Stream<
List< int> > -
The chunks written from now on, one event per write; the stream
ends when the response is closed. Earlier chunks are not repeated.
no setter
-
closed
→ Future<
void> -
Completes when the application closed the response.
no setter
-
committed
→ Future<
void> -
Completes when the response is committed: at the first body bytes, a
flush or close. For a stream this is when its headers are out.
no setter
-
The cookies the application set with addCookie, in order.
final
-
done
→ Future<
void> -
Completes when the response is over: closed, or the client went
away. A long-running stream (server-sent events) listens to it to
stop producing.
no setteroverride
-
events
→ List<
ServerSentEvent< String?> > -
The server-sent events written so far, read from the body.
no setter
-
eventStream
→ Stream<
ServerSentEvent< String?> > -
The server-sent events written from now on, as they arrive; ends
when the response is closed.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- headers → HttpHeaders
-
The headers to send. Changes made after the response is committed
(isCommitted) no longer reach the client.
final
- isClosed → bool
-
Whether the application closed the response. Writing after that
throws a
StateError.no setter - isCommitted → bool
-
Whether the status and headers are already sent, which happens at
the first body bytes, a flush or close. A filter that wants to
change headers checks this first.
no setteroverride
- json → Object?
-
The body decoded as JSON; throws a
FormatExceptionwhen it is not JSON.no setter - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- statusCode ↔ int
-
The status code to send; 200 until set. Changing it after the
response is committed has no effect.
getter/setter pairoverride
Methods
-
add(
List< int> bytes) → void -
Appends
bytesto the body; commits the response first if needed.override -
addCookie(
ResponseCookie cookie) → void -
Sends
cookieto the client in aSet-Cookieheader. Call it before the response is committed.override -
addStream(
Stream< List< stream) → Future<int> >void> -
Appends every chunk of
streamto the body; completes when the stream is done. Use it to send a file or a proxied body without holding it in memory.override -
close(
) → Future< void> -
Ends the response: commits it if nothing was written, then finishes
the body. Every response must be closed once.
override
-
disconnect(
) → void - Plays a client that goes away: done completes, so an endless stream (server-sent events) stops writing. The response itself is not closed.
-
flush(
) → Future< void> -
Sends what was written so far to the client now instead of when the
buffer fills; used after each server-sent event so that it arrives
at once.
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
send(
int status, String text, {String contentType = 'text/plain; charset=utf-8'}) → Future< void> -
Available on ServerHttpResponse, provided by the ServerHttpResponseWriting extension
Sendsstatuswithtextas the body (UTF-8,Content-Lengthset) and closes the response.contentTypedefaults totext/plain; charset=utf-8; pass another for JSON or HTML text. -
toString(
) → String -
A string representation of this object.
inherited
-
write(
String text) → void -
Appends
textto the body, encoded in UTF-8 whatever theContent-Typesays.override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited