complete method

  1. @override
void complete({
  1. BaseResponse? response,
})
inherited

Implementation

@override
void complete({BaseResponse? response}) {
  var resp = response ?? MockResponse.ok();

  // Defer the "fetching" of the response until the request has been sent.
  onSent.then((_) async {
    // Coerce the response to the correct format (streamed or not).
    if (_streamResponse && resp is Response) {
      final standardResponse = resp as Response;
      resp = StreamedResponse.fromByteStream(
          standardResponse.status,
          standardResponse.statusText,
          standardResponse.headers,
          Stream.fromIterable([standardResponse.body.asBytes()]));
    }
    if (!_streamResponse && resp is StreamedResponse) {
      final streamedResponse = resp as StreamedResponse;
      resp = Response.fromBytes(
          streamedResponse.status,
          streamedResponse.statusText,
          streamedResponse.headers,
          await streamedResponse.body.toBytes());
    }

    if (resp is StreamedResponse) {
      final streamedResponse = resp as StreamedResponse;
      final progressListener = http_utils.ByteStreamProgressListener(
          streamedResponse.body.byteStream,
          total: streamedResponse.contentLength);
      progressListener.progressStream.listen(downloadProgressController.add);
      resp = StreamedResponse.fromByteStream(
          streamedResponse.status,
          streamedResponse.statusText,
          streamedResponse.headers,
          progressListener.byteStream);
    } else {
      final standardResponse = resp as Response;
      final total = standardResponse.body.asBytes().length;
      downloadProgressController.add(RequestProgress(total, total));
    }

    _response.complete(resp);
  });
}