Expectations are one-time only. Every time a mock-aware request is dispatched, it is compared against the list of unsatisfied expectations and chooses the first match (if one exists) to use to resolve said request.
By default, an expected request is resolved with a successful 200 OK response.
The expected URI must match exactly, including scheme, host, path, and query.
var uri = Uri.parse('https://example.org/resource');
MockTransports.http.expect('GET', uri);
var response = await transport.Http.get(uri);
print(response.status); // 200
Alternatively, you can specify a response to use.
var uri = Uri.parse('https://example.org/resource');
var response = new MockResponse.unauthorized(
body: 'Invalid access token',
headers: {'x-session': 'ab93s...'});
MockTransports.http.expect(
'GET',
uri,
respondWith: response);
try {
await transport.Http.get(uri);
} on transport.RequestException catch (e) {
print(e.response.status); // 401
print(e.response.body.asString()); // 'Invalid access token'
}
Causing a failure is slightly different than returning a mock response with a non-200 response code. The following example simulates an exception that would be thrown during the transport logic prior to sending a request or after receiving a response (e.g. a request/response interceptor throwing).
var uri = Uri.parse('https://example.org/resource');
MockTransports.http.expect(
'GET',
uri,
failWith: new Exception('Unexpected error...'));
try {
await transport.Http.get(uri);
} on transport.RequestException catch (e) {
print(e.error); // 'Unexpected error...'
}
Note that the request logic always wraps errors and exceptions that happen during the request attempt in a
transport.RequestException
. The original error (if applicable) can be found via theerror
property as seen above.
Using expect()
requires that the request matches the given URI exactly.
If you don't know the exact URI or would like to be more flexible in your
matching, you can use expectPattern()
.
var uriPattern = new RegExp('.*/example');
MockTransports.http.expectPattern('GET', uriPattern);
var response = await transport.Http.get(Uri.parse('http://localhost:8080/example'));
print(response.status); // 200