MockResponse constructor
MockResponse(})
Implementation
MockResponse(int status,
{body,
Encoding? encoding,
Map<String, String>? headers,
String? statusText}) {
// Ensure the headers are case insensitive.
headers = CaseInsensitiveMap<String>.from(headers ?? {});
// If an encoding was given, update the content-type charset parameter.
if (encoding != null) {
MediaType contentType = http_utils.parseContentTypeFromHeaders(headers);
contentType = contentType.change(parameters: {'charset': encoding.name});
headers['content-type'] = contentType.toString();
}
// Use a default status text based on the status code if one is not given.
statusText ??= _mapStatusToText(status);
body ??= '';
// Construct the body according to the data type.
if (body is String) {
_response = Response.fromString(status, statusText, headers, body);
} else if (body is List<int>) {
_response = Response.fromBytes(status, statusText, headers, body);
} else {
throw ArgumentError(
'Mock response body must be a String or bytes (List<int>).');
}
}