From 306a09a384e6ba0fe02ae3a44d28ceeb372b3a25 Mon Sep 17 00:00:00 2001 From: Todd Volkert Date: Mon, 3 Jun 2019 09:50:21 -0700 Subject: [PATCH] Update consolidateHttpClientResponseBytes() to use compressionState (#33729) This updates to use the new HttpClientResponse.compressionState API that was recently added in the SDK. https://github.com/flutter/flutter/issues/32374 --- .../src/foundation/consolidate_response.dart | 20 +++++---- .../lib/src/painting/image_provider.dart | 1 - .../foundation/consolidate_response_test.dart | 41 +++++++------------ 3 files changed, 27 insertions(+), 35 deletions(-) diff --git a/packages/flutter/lib/src/foundation/consolidate_response.dart b/packages/flutter/lib/src/foundation/consolidate_response.dart index ddbe73fd2c..3c64e19d7e 100644 --- a/packages/flutter/lib/src/foundation/consolidate_response.dart +++ b/packages/flutter/lib/src/foundation/consolidate_response.dart @@ -43,10 +43,8 @@ typedef BytesReceivedCallback = void Function(int cumulative, int total); /// bytes from this method (assuming the response is sending compressed bytes), /// set both [HttpClient.autoUncompress] to false and the `autoUncompress` /// parameter to false. -// TODO(tvolkert): Remove the [client] param once https://github.com/dart-lang/sdk/issues/36971 is fixed. Future consolidateHttpClientResponseBytes( HttpClientResponse response, { - HttpClient client, bool autoUncompress = true, BytesReceivedCallback onBytesReceived, }) { @@ -58,15 +56,21 @@ Future consolidateHttpClientResponseBytes( int expectedContentLength = response.contentLength; if (expectedContentLength == -1) expectedContentLength = null; - if (response.headers?.value(HttpHeaders.contentEncodingHeader) == 'gzip') { - if (client?.autoUncompress ?? true) { + switch (response.compressionState) { + case HttpClientResponseCompressionState.compressed: + if (autoUncompress) { + // We need to un-compress the bytes as they come in. + sink = gzip.decoder.startChunkedConversion(output); + } + break; + case HttpClientResponseCompressionState.decompressed: // response.contentLength will not match our bytes stream, so we declare // that we don't know the expected content length. expectedContentLength = null; - } else if (autoUncompress) { - // We need to un-compress the bytes as they come in. - sink = gzip.decoder.startChunkedConversion(output); - } + break; + case HttpClientResponseCompressionState.notCompressed: + // Fall-through. + break; } int bytesReceived = 0; diff --git a/packages/flutter/lib/src/painting/image_provider.dart b/packages/flutter/lib/src/painting/image_provider.dart index 8f12d66c3c..170fde073e 100644 --- a/packages/flutter/lib/src/painting/image_provider.dart +++ b/packages/flutter/lib/src/painting/image_provider.dart @@ -552,7 +552,6 @@ class NetworkImage extends ImageProvider { final Uint8List bytes = await consolidateHttpClientResponseBytes( response, - client: _httpClient, onBytesReceived: (int cumulative, int total) { chunkEvents.add(ImageChunkEvent( cumulativeBytesLoaded: cumulative, diff --git a/packages/flutter/test/foundation/consolidate_response_test.dart b/packages/flutter/test/foundation/consolidate_response_test.dart index 9bbe8f9002..9a06e09ff7 100644 --- a/packages/flutter/test/foundation/consolidate_response_test.dart +++ b/packages/flutter/test/foundation/consolidate_response_test.dart @@ -14,17 +14,11 @@ void main() { group(consolidateHttpClientResponseBytes, () { final List chunkOne = [0, 1, 2, 3, 4, 5]; final List chunkTwo = [6, 7, 8, 9, 10]; - MockHttpClient client; MockHttpClientResponse response; - MockHttpHeaders headers; setUp(() { - client = MockHttpClient(); response = MockHttpClientResponse(); - headers = MockHttpHeaders(); - when(client.autoUncompress).thenReturn(true); - when(response.headers).thenReturn(headers); - when(headers.value(HttpHeaders.contentEncodingHeader)).thenReturn(null); + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.notCompressed); when(response.listen( any, onDone: anyNamed('onDone'), @@ -50,7 +44,7 @@ void main() { when(response.contentLength) .thenReturn(chunkOne.length + chunkTwo.length); final List bytes = - await consolidateHttpClientResponseBytes(response, client: client); + await consolidateHttpClientResponseBytes(response); expect(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); @@ -58,7 +52,7 @@ void main() { test('Converts a compressed HttpClientResponse with contentLength to bytes', () async { when(response.contentLength).thenReturn(chunkOne.length); final List bytes = - await consolidateHttpClientResponseBytes(response, client: client); + await consolidateHttpClientResponseBytes(response); expect(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); @@ -66,7 +60,7 @@ void main() { test('Converts an HttpClientResponse without contentLength to bytes', () async { when(response.contentLength).thenReturn(-1); final List bytes = - await consolidateHttpClientResponseBytes(response, client: client); + await consolidateHttpClientResponseBytes(response); expect(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); @@ -77,7 +71,6 @@ void main() { final List records = []; await consolidateHttpClientResponseBytes( response, - client: client, onBytesReceived: (int cumulative, int total) { records.addAll([cumulative, total]); }, @@ -114,7 +107,7 @@ void main() { }); when(response.contentLength).thenReturn(-1); - expect(consolidateHttpClientResponseBytes(response, client: client), + expect(consolidateHttpClientResponseBytes(response), throwsA(isInstanceOf())); }); @@ -122,7 +115,6 @@ void main() { when(response.contentLength).thenReturn(-1); final Future> result = consolidateHttpClientResponseBytes( response, - client: client, onBytesReceived: (int cumulative, int total) { throw 'misbehaving callback'; }, @@ -137,7 +129,7 @@ void main() { final List gzippedChunkTwo = gzipped.sublist(gzipped.length ~/ 2); setUp(() { - when(headers.value(HttpHeaders.contentEncodingHeader)).thenReturn('gzip'); + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); when(response.listen( any, onDone: anyNamed('onDone'), @@ -159,27 +151,26 @@ void main() { }); }); - test('Uncompresses GZIP bytes if autoUncompress is true and response.autoUncompress is false', () async { - when(client.autoUncompress).thenReturn(false); + test('Uncompresses GZIP bytes if autoUncompress is true and response.compressionState is compressed', () async { + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); when(response.contentLength).thenReturn(gzipped.length); - final List bytes = await consolidateHttpClientResponseBytes(response, client: client); + final List bytes = await consolidateHttpClientResponseBytes(response); expect(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]); }); - test('returns gzipped bytes if autoUncompress is false and response.autoUncompress is false', () async { - when(client.autoUncompress).thenReturn(false); + test('returns gzipped bytes if autoUncompress is false and response.compressionState is compressed', () async { + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); when(response.contentLength).thenReturn(gzipped.length); - final List bytes = await consolidateHttpClientResponseBytes(response, client: client, autoUncompress: false); + final List bytes = await consolidateHttpClientResponseBytes(response, autoUncompress: false); expect(bytes, gzipped); }); test('Notifies onBytesReceived with gzipped numbers', () async { - when(client.autoUncompress).thenReturn(false); + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.compressed); when(response.contentLength).thenReturn(gzipped.length); final List records = []; await consolidateHttpClientResponseBytes( response, - client: client, onBytesReceived: (int cumulative, int total) { records.addAll([cumulative, total]); }, @@ -193,13 +184,13 @@ void main() { ]); }); - test('Notifies onBytesReceived with expectedContentLength of -1 if response.autoUncompress is true', () async { + test('Notifies onBytesReceived with expectedContentLength of -1 if response.compressionState is decompressed', () async { final int syntheticTotal = (chunkOne.length + chunkTwo.length) * 2; + when(response.compressionState).thenReturn(HttpClientResponseCompressionState.decompressed); when(response.contentLength).thenReturn(syntheticTotal); final List records = []; await consolidateHttpClientResponseBytes( response, - client: client, onBytesReceived: (int cumulative, int total) { records.addAll([cumulative, total]); }, @@ -216,6 +207,4 @@ void main() { }); } -class MockHttpClient extends Mock implements HttpClient {} class MockHttpClientResponse extends Mock implements HttpClientResponse {} -class MockHttpHeaders extends Mock implements HttpHeaders {}