MockResponse constructor

MockResponse(
  1. int status, {
  2. dynamic body,
  3. Encoding? encoding,
  4. Map<String, String>? headers,
  5. String? statusText,
})

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>).');
  }
}