complete method

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

Implementation

@override
void complete({BaseResponse? response}) {
  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 && response is Response) {
      final Response standardResponse = response as Response;
      response = StreamedResponse.fromByteStream(
          standardResponse.status,
          standardResponse.statusText,
          standardResponse.headers,
          Stream.fromIterable([standardResponse.body.asBytes()]));
    }
    if (!_streamResponse && response is StreamedResponse) {
      final StreamedResponse streamedResponse = response as StreamedResponse;
      response = Response.fromBytes(
          streamedResponse.status,
          streamedResponse.statusText,
          streamedResponse.headers,
          await streamedResponse.body.toBytes());
    }

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

    _response.complete(response);
  });
}