respond method
void
respond(})
Answers with status, headers and at most one kind of body:
json, encoded, with Content-Type: application/json unless
headers set another; body, text sent in UTF-8; or bytes. With
none of them the body is empty. If several are given, json wins,
then body.
Implementation
void respond(
int status, {
Object? json,
String? body,
List<int>? bytes,
Map<String, String> headers = const {},
}) {
final responseHeaders = HttpHeaders(headers);
final List<int> content;
if (json != null) {
content = utf8.encode(jsonEncode(json));
if (!responseHeaders.contains(HttpHeaders.contentTypeHeader)) {
responseHeaders.set(
HttpHeaders.contentTypeHeader,
MediaType.applicationJson,
);
}
} else if (body != null) {
content = utf8.encode(body);
} else {
content = bytes ?? const [];
}
_expectation.answer = (request) async => StreamedHttpResponse(
status,
headers: responseHeaders.copy(),
body: Stream.value(content),
request: request,
);
}