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
methodrequest totarget: a path with an optional query (/books?q=dune), resolved againsthttp://localhost, or a full URL (https://shop.test/books, which also makes isSecure true).headersstart 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 aboveserver.max-http-request-body-sizeends with a PayloadTooLargeException, whatever itsContent-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
TheContent-Typeheader parsed, ornullwhen it is missing or is not a valid media type.no setter -
The cookies of the
Cookieheader, 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 (
httpsorwss).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 aserver.context-path, filters and handlers see the path without that prefix.no setteroverride - principal ↔ Principal?
-
The authenticated user or client, or
nullwhile nobody is authenticated. An authentication filter (for example the security starter's) sets it; a controller parameter of typePrincipalreceives it.getter/setter pairoverride -
queryParameters
→ Map<
String, List< String> > -
The query parameters, decoded, each name with all its values in
order (
?tag=a&tag=bgives{'tag': ['a', 'b']}).no setteroverride - remoteAddress ↔ String?
-
The IP address of the client, or
nullwhen 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
Hostheader).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.webSocketreturns it.no setter
Methods
-
addCookie(
String name, String value) → void -
Adds the cookie
name=valueto theCookieheader. -
addParameter(
String name, String value) → void -
Adds
valueto the query parameternameof 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 pastmaxLengthbytes (no limit whennull). 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 thecharsetof theContent-Type: ISO-8859-1 and US-ASCII are recognised, anything else (or no charset) is read as UTF-8, with malformed bytes replaced.maxLengthlimits the size in bytes as in readBodyBytes. -
setBody(
Object body) → void -
Sets the body to
body, aString(encoded in UTF-8) or a list of bytes, and setsContent-Lengthto match. Throws anArgumentErrorfor anything else. It does not setContent-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