stub method
void
stub(})
States what method url answers.
url matches on the request's path when it is a plain string with no
scheme — '/api/messages' — and on the whole url when it has one.
Neither is a substring match: a stub that fires for a url nobody meant it
to is the failure this whole surface exists to avoid, and a RegExp is
how anything looser is said out loud.
Exactly one of json, body and throws is the answer; status alone
is an empty response with that code. throws is the shape an app's error
handling is tested with — a SocketException, a TimeoutException, or
anything else the app catches.
Implementation
void stub(
String method,
Pattern url, {
Object? json,
List<int>? body,
String? contentType,
int status = 200,
Map<String, String> headers = const {},
Object? throws,
}) {
if ([json, body, throws].where((v) => v != null).length > 1) {
throw ArgumentError(
'A stub answers with one of json:, body: or throws:, not several.',
);
}
_stubs.insert(
0,
_Stub(
method: method.toUpperCase(),
url: url,
status: status,
body: _bodyOf(json, body),
contentType: contentType ?? (json == null ? null : 'application/json'),
headers: {
for (var e in headers.entries) e.key: [e.value],
},
throws: throws,
),
);
}