MockClient constructor

MockClient(
  1. MockClientHandler fn
)

Creates a MockClient with a handler that receives Requests and sends Responses.

Implementation

MockClient(MockClientHandler fn)
    : this._((baseRequest, bodyStream) async {
        final bodyBytes = await bodyStream.toBytes();
        var request = Request(baseRequest.method, baseRequest.url)
          ..persistentConnection = baseRequest.persistentConnection
          ..followRedirects = baseRequest.followRedirects
          ..maxRedirects = baseRequest.maxRedirects
          ..headers.addAll(baseRequest.headers)
          ..bodyBytes = bodyBytes
          ..finalize();

        final response = await fn(request);
        return StreamedResponse(
            ByteStream.fromBytes(response.bodyBytes), response.statusCode,
            contentLength: response.contentLength,
            request: response.request,
            headers: response.headers,
            isRedirect: response.isRedirect,
            persistentConnection: response.persistentConnection,
            reasonPhrase: response.reasonPhrase);
      });