|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:async/async.dart'; |
| 6 | +import 'package:http/http.dart'; |
| 7 | +import 'package:stream_channel/stream_channel.dart'; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import 'compressed_response_body_server_vm.dart' |
| 11 | + if (dart.library.html) 'compressed_response_body_server_web.dart'; |
| 12 | + |
| 13 | +/// Tests that the [Client] correctly implements HTTP responses with compressed |
| 14 | +/// bodies. |
| 15 | +/// |
| 16 | +/// If the response is encoded using a recognized 'Content-Encoding' then the |
| 17 | +/// [Client] must decode it. Otherwise it must return the content unchanged. |
| 18 | +/// |
| 19 | +/// The 'Content-Encoding' and 'Content-Length' headers may be absent for |
| 20 | +/// responses with a 'Content-Encoding' and, if present, their values are |
| 21 | +/// undefined. |
| 22 | +/// |
| 23 | +/// The value of `StreamedResponse.contentLength` is not defined for responses |
| 24 | +/// with a 'Content-Encoding' header. |
| 25 | +void testCompressedResponseBody(Client client) async { |
| 26 | + group('response body', () { |
| 27 | + late final String host; |
| 28 | + late final StreamChannel<Object?> httpServerChannel; |
| 29 | + late final StreamQueue<Object?> httpServerQueue; |
| 30 | + const message = 'Hello World!'; |
| 31 | + |
| 32 | + setUpAll(() async { |
| 33 | + httpServerChannel = await startServer(); |
| 34 | + httpServerQueue = StreamQueue(httpServerChannel.stream); |
| 35 | + host = 'localhost:${await httpServerQueue.next}'; |
| 36 | + }); |
| 37 | + tearDownAll(() => httpServerChannel.sink.add(null)); |
| 38 | + |
| 39 | + test('gzip: small response with content length', () async { |
| 40 | + // Test a supported content encoding. |
| 41 | + final response = await client.get(Uri.http(host, '/gzip')); |
| 42 | + final requestHeaders = await httpServerQueue.next as Map; |
| 43 | + |
| 44 | + expect((requestHeaders['accept-encoding'] as List).join(', '), |
| 45 | + contains('gzip')); |
| 46 | + expect(response.body, message); |
| 47 | + expect(response.bodyBytes, message.codeUnits); |
| 48 | + expect(response.contentLength, message.length); |
| 49 | + expect(response.headers['content-type'], 'text/plain'); |
| 50 | + expect(response.isRedirect, isFalse); |
| 51 | + expect(response.reasonPhrase, 'OK'); |
| 52 | + expect(response.request!.method, 'GET'); |
| 53 | + expect(response.statusCode, 200); |
| 54 | + }); |
| 55 | + |
| 56 | + test('gzip: small response streamed with content length', () async { |
| 57 | + // Test a supported content encoding. |
| 58 | + final request = Request('GET', Uri.http(host, '/gzip', {'length': ''})); |
| 59 | + final response = await client.send(request); |
| 60 | + final requestHeaders = await httpServerQueue.next as Map; |
| 61 | + |
| 62 | + expect((requestHeaders['accept-encoding'] as List).join(', '), |
| 63 | + contains('gzip')); |
| 64 | + expect(await response.stream.bytesToString(), message); |
| 65 | + expect(response.headers['content-type'], 'text/plain'); |
| 66 | + expect(response.isRedirect, isFalse); |
| 67 | + expect(response.reasonPhrase, 'OK'); |
| 68 | + expect(response.request!.method, 'GET'); |
| 69 | + expect(response.statusCode, 200); |
| 70 | + }); |
| 71 | + |
| 72 | + test('upper: small response streamed with content length', () async { |
| 73 | + // Test an unsupported content encoding. |
| 74 | + final request = Request('GET', Uri.http(host, '/upper', {'length': ''})); |
| 75 | + final response = await client.send(request); |
| 76 | + await httpServerQueue.next; |
| 77 | + |
| 78 | + expect(await response.stream.bytesToString(), message.toUpperCase()); |
| 79 | + expect(response.headers['content-type'], 'text/plain'); |
| 80 | + expect(response.isRedirect, isFalse); |
| 81 | + expect(response.reasonPhrase, 'OK'); |
| 82 | + expect(response.request!.method, 'GET'); |
| 83 | + expect(response.statusCode, 200); |
| 84 | + }); |
| 85 | + }); |
| 86 | +} |
0 commit comments