Validates that a TestResponse has the specified status code, body and headers.
This matcher will validate the status code, body and headers of a TestResponse.
If the status code of the response does not match the expected status code in this matcher, the matcher will fail.
bodyMatcher
is used to evaluate the decoded value of the HTTP response body. In doing so, this method will implicitly
ensure that the HTTP response body was decoded according to its Content-Type header. bodyMatcher
may be a matcher or it may
be Map
or List
. When bodyMatcher
is a Map
or List
, the value is compared for equality to the decoded HTTP response body. For example,
the following would match on a response with Content-Type: application/json and a body of '{"key" : "value"}':
var response = await client.request("/foo").get();
expect(response, hasResponse(200, {"key" : "value"});
When using a matcher, the matcher will use its own matching behavior. For example, if the response had a JSON list of strings, the following would expect that each object contains the substring 'foo':
var response = await client.request("/foo").get();
expect(response, hasResponse(200, everyElement(contains("foo")));
For matching a subset of keys in a Map
, see partial.
You may optionally validate HTTP headers as well. By default, only key-value pairs in headers
are evaluated for matches. Headers
that exist in the response but are not included in headers
will not be evaluated and will not impact whether or not this matcher succeeds
or fails. If you wish to match an exhaustive list of all headers in a request, pass failIfContainsUnmatchedHeader
as true.
Header keys are case-insensitive strings. Header values are typically instances of String
or instances of Matcher
. If using a matcher,
you may optionally wrap the matcher in asNumber or asDateTime to convert the header value in the response to an instance of int
or DateTime
prior to it being matched.
Example:
var response = await client.request("/foo").get();
expect(response, hasResponse(200, [], headers: {
"x-version" : asNumber(greaterThan(1))(
});
Source
_HTTPResponseMatcher hasResponse(int statusCode, dynamic bodyMatcher, {Map<String, dynamic> headers: null, bool failIfContainsUnmatchedHeader: false}) { return new _HTTPResponseMatcher( statusCode, (headers != null ? new _HTTPHeaderMatcher(headers, failIfContainsUnmatchedHeader) : null), (bodyMatcher != null ? new _HTTPBodyMatcher(bodyMatcher) : null)); }