MockWordpressAdapter class

A Dio HttpClientAdapter that serves canned WordPress responses.

Use it to test code that depends on a WordpressClient without a live site:

final adapter = MockWordpressAdapter()
  ..reply('GET', '/posts', const MockResponse(body: [{'id': 1}]));

final client = WordpressClient.fromDioInstance(
  baseUrl: Uri.parse('https://example.com/wp-json/wp/v2'),
  instance: Dio()..httpClientAdapter = adapter,
);

Routes are matched in registration order; the first match wins. A String route matches when the request path ends with it, a RegExp is tested against the path. Unmatched requests are recorded and answered with a WordPress style rest_no_route 404 (or throw when strict).

Constructors

MockWordpressAdapter({bool strict = false})

Properties

hashCode → int
The hash code for this object.
no setterinherited
lastRequest → RecordedRequest?
The most recent request, or null if none was made.
no setter
requests → List<RecordedRequest>
Every request seen so far, in order.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited
strict → bool
Throw a StateError for requests no route matches.
final

Methods

callCount({String? method, Pattern? path}) → int
Number of requests seen, optionally narrowed by method and path.
clearRequests() → void
Forgets recorded requests, keeping the routes.
close({bool force = false}) → void
Close the current adapter and its inner clients or requests.
fetch(RequestOptions options, Stream<Uint8List>? requestStream, Future<void>? cancelFuture) → Future<ResponseBody>
Implement this method to make real HTTP requests.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
on(String method, Pattern path, MockHandler handler) → void
Answers matching requests with the result of handler.
reply(String method, Pattern path, MockResponse response) → void
Answers every matching request with response.
replySequence(String method, Pattern path, List<MockResponse> responses) → void
Answers matching requests with each of responses in turn, repeating the last one once exhausted. Handy for retry and polling tests.
toString() → String
A string representation of this object.
inherited

Operators

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