MockRequestBuilder class final

Describes one request for MockMvc.perform: method, path, headers, query parameters, cookies, user and body.

The shortcuts of MockMvc (MockMvc.get, MockMvc.post and the others) build it for you; build it yourself for cookies, a principal, a RequestPostProcessor (a test user, a CSRF token) or a method without a shortcut. Its collections are mutable, so a cascade fills them.

await mvc.perform(
  MockRequestBuilder(HttpMethod.get, '/api/books')
    ..params['q'] = 'dune'
    ..headers['Accept'] = 'application/json',
).andExpect(status: 200);

Spring (Java): MockHttpServletRequestBuilder, from get(...), post(...) and the other MockMvcRequestBuilders.

Constructors

MockRequestBuilder(HttpMethod method, String path, [List<Object?> uriVariables = const []])
A method request to path. Each {name} placeholder of path is filled, in order, from uriVariables, URL-encoded; extra placeholders stay as written.

Properties

cookies → Map<String, String>
Cookies to send, by name.
final
hashCode → int
The hash code for this object.
no setterinherited
headers → Map<String, String>
The headers to send, by name.
final
method → HttpMethod
The HTTP method.
final
params → Map<String, Object>
Query parameters to add; a List value sends the parameter once per element.
final
path → String
The path, with the URI variables filled in; it may carry a query (/api/books?q=dune).
final
principal ↔ Principal?
The authenticated user the request comes from; null for an anonymous request. With spring_boot_starter_security, its user(...) post-processor sets a full security context instead.
getter/setter pair
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

build() → MockServerHttpRequest
Builds the request; MockMvc calls it, and then applies the post-processors, which this does not.
content(Object body) → void
Sets the body: a String (sent as UTF-8) or bytes. No Content-Type is added; set one in headers.
json(Object? value) → void
Sets the body to value encoded as JSON, and Content-Type: application/json unless headers already have a content type.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() → String
A string representation of this object.
inherited
with_(RequestPostProcessor processor) → void
Adds processor, which changes the request just before it is sent, after the MockMvc.defaultPostProcessors. Spring's .with(...); the trailing _ is there because with is a Dart keyword.

Operators

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