MockAdapter class
Test-only HttpAdapter that returns canned responses and records every request it received.
Usage
final adapter = MockAdapter()
..when(method: HttpMethod.get, pathContains: '/posts/1')
.thenReturn(HttpResponse(
statusCode: 200,
data: {'id': 1, 'title': 'Hello'},
headers: const {},
))
..when(method: HttpMethod.post, pathContains: '/error')
.thenThrow(HttpAdapterException(
kind: HttpAdapterErrorKind.timeout,
message: 'simulated timeout',
));
final netAnchor = NetAnchor.builder()
.httpAdapter(adapter)
.baseUrl('https://api.test')
.build();
// ... run your code ...
expect(adapter.recordedRequests, hasLength(1));
expect(adapter.lastRequest!.method, HttpMethod.get);
Stubs are matched in registration order; the first match wins. If no stub matches, send throws an HttpAdapterException of kind HttpAdapterErrorKind.unknown so unconfigured paths fail loudly.
- Implemented types
Constructors
- MockAdapter()
- Creates an empty mock adapter.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- lastRequest → HttpRequest?
-
Convenience accessor — the most recently received request.
no setter
-
recordedRequests
→ List<
HttpRequest> -
Every request that has been send-t through this adapter, in order.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
close(
{bool force = false}) → void -
Release any resources held by the adapter (close pools, cancel keep-alive).
override
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
reset(
) → void - Drop all stubs and recorded requests.
-
send(
HttpRequest request) → Future< HttpResponse> -
Send
requestand return the resolved HttpResponse.override -
toString(
) → String -
A string representation of this object.
inherited
-
when(
{HttpMethod? method, String? path, String? pathContains, bool bodyMatcher(Object? body)?, bool headerMatcher(Map< String, String> headers)?, bool queryMatcher(Map<String, Object?> queryParameters)?}) → StubBuilder - Begin a fluent stub. Filters are AND-combined; omit them all to make a catch-all stub.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited