MockServerHttpRequest class final

An HTTP request built in memory, to call the application in a test without a server.

MockMvc builds one for every request it performs and hands it to the application; a RequestPostProcessor receives it to add a user, a header or a token. Build one yourself only to call an HttpHandler, a WebFilter or a controller method directly. Unlike a real request, everything can be set: headers, query parameters, cookies, body, remoteAddress, principal.

// Calls a WebFilter directly with a JSON POST.
final request = MockServerHttpRequest(HttpMethod.post, '/api/books')
  ..headers.contentType = 'application/json'
  ..setBody('{"isbn": "978-0"}');
await filter.doFilter(request, MockServerHttpResponse(), chain);

Spring (Java):

MockHttpServletRequest request =
    new MockHttpServletRequest("POST", "/api/books");
request.setContentType("application/json");
request.setContent("{\"isbn\": \"978-0\"}".getBytes());
Implemented types
Available extensions

Constructors

MockServerHttpRequest(HttpMethod method, String target, {HttpHeaders? headers})
A method request to target: a path with an optional query (/books?q=dune), resolved against http://localhost, or a full URL (https://shop.test/books, which also makes isSecure true). headers start empty unless given.

Properties

attributes → Map<String, Object?>
A mutable map where filters and handlers leave values for the code that runs later on the same request, such as a parsed form or a trace id. It lives as long as the request.
final
body → Stream<Uint8List>
The body as a stream of bytes. It can be listened to only once: a second read throws a StateError. On the web starter's server a body above server.max-http-request-body-size ends with a PayloadTooLargeException, whatever its Content-Type (the starter's multipart resolver reads uploads with their own limit, spring.web.multipart.max-request-size). For small bodies, ServerHttpRequestBody.readBodyBytes and ServerHttpRequestBody.readBodyText are simpler.
no setteroverride
bodyBytes → Uint8List
The body set with setBody (empty by default), readable any number of times, unlike body.
no setter
contentType → MediaType?

Available on ServerHttpRequest, provided by the ServerHttpRequestBody extension

The Content-Type header parsed, or null when it is missing or is not a valid media type.
no setter
cookies → Map<String, String>
The cookies of the Cookie header, by name (quotes around a value removed).
no setteroverride
hashCode → int
The hash code for this object.
no setterinherited
headers → HttpHeaders
The headers the client sent. Treat them as read-only; to change them for the rest of the chain, wrap the request in a ServerHttpRequestDecorator.
final
isSecure → bool
Whether the request came over TLS (https or wss).
no setteroverride
isWebSocketUpgrade → bool
Whether the client asks to open a WebSocket on this request (Upgrade: websocket).
no setteroverride
method → HttpMethod
The HTTP method of the request.
final
path → String
The path without the query, still percent-encoded (/api/books/978-0). Behind a server.context-path, filters and handlers see the path without that prefix.
no setteroverride
principal ↔ Principal?
The authenticated user or client, or null while nobody is authenticated. An authentication filter (for example the security starter's) sets it; a controller parameter of type Principal receives it.
getter/setter pairoverride
queryParameters → Map<String, List<String>>
The query parameters, decoded, each name with all its values in order (?tag=a&tag=b gives {'tag': ['a', 'b']}).
no setteroverride
remoteAddress ↔ String?
The IP address of the client, or null when the server does not know it. Behind a proxy this is the proxy's address.
getter/setter pairoverride-getter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
uri ↔ Uri
The full URL as the client asked for it: scheme, host, port, path and query (on the web starter's server, the host is the one of the Host header).
getter/setter pairoverride-getter
webSocketClient → Future<WebSocketSession>
The client's end of the WebSocket, once the application accepted the handshake with upgradeToWebSocket; a test sends and receives messages through it. It never completes when the handshake is refused. MockMvc.webSocket returns it.
no setter

Methods

addCookie(String name, String value) → void
Adds the cookie name=value to the Cookie header.
addParameter(String name, String value) → void
Adds value to the query parameter name of uri (the values already there stay).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
readBodyBytes({int? maxLength}) → Future<Uint8List>

Available on ServerHttpRequest, provided by the ServerHttpRequestBody extension

Reads the whole body into memory. Throws a PayloadTooLargeException as soon as it grows past maxLength bytes (no limit when null). Like ServerHttpRequest.body, it can be called only once.
readBodyText({int? maxLength}) → Future<String>

Available on ServerHttpRequest, provided by the ServerHttpRequestBody extension

Reads the whole body as text, decoded with the charset of the Content-Type: ISO-8859-1 and US-ASCII are recognised, anything else (or no charset) is read as UTF-8, with malformed bytes replaced. maxLength limits the size in bytes as in readBodyBytes.
setBody(Object body) → void
Sets the body to body, a String (encoded in UTF-8) or a list of bytes, and sets Content-Length to match. Throws an ArgumentError for anything else. It does not set Content-Type.
toString() → String
A string representation of this object.
inherited
upgradeToWebSocket({String? protocol}) → Future<WebSocketSession>
Accepts the WebSocket handshake and returns the open connection.
override

Operators

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