consolidate_response_test.dart 6.14 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
@TestOn('!chrome')
6

7 8 9 10
import 'dart:async';
import 'dart:io';

import 'package:flutter/foundation.dart';
11
import 'package:flutter_test/flutter_test.dart';
12

13 14
final Uint8List chunkOne = Uint8List.fromList(<int>[0, 1, 2, 3, 4, 5]);
final Uint8List chunkTwo = Uint8List.fromList(<int>[6, 7, 8, 9, 10]);
15

16
void main() {
17
  group(consolidateHttpClientResponseBytes, () {
18
    late MockHttpClientResponse response;
19 20

    setUp(() {
21 22 23 24
      response = MockHttpClientResponse(
        chunkOne: chunkOne,
        chunkTwo: chunkTwo,
      );
25 26
    });

27
    test('Converts an HttpClientResponse with contentLength to bytes', () async {
28
      response.contentLength = chunkOne.length + chunkTwo.length;
29 30
      final List<int> bytes =
          await consolidateHttpClientResponseBytes(response);
31 32 33 34

      expect(bytes, <int>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    });

35
    test('Converts a compressed HttpClientResponse with contentLength to bytes', () async {
36
      response.contentLength = chunkOne.length;
37 38
      final List<int> bytes =
          await consolidateHttpClientResponseBytes(response);
39 40 41 42

      expect(bytes, <int>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    });

43
    test('Converts an HttpClientResponse without contentLength to bytes', () async {
44
      response.contentLength = -1;
45 46
      final List<int> bytes =
          await consolidateHttpClientResponseBytes(response);
47 48 49 50

      expect(bytes, <int>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
    });

51 52
    test('Notifies onBytesReceived for every chunk of bytes', () async {
      final int syntheticTotal = (chunkOne.length + chunkTwo.length) * 2;
53
      response.contentLength = syntheticTotal;
54
      final List<int?> records = <int?>[];
55
      await consolidateHttpClientResponseBytes(
56
        response,
57 58
        onBytesReceived: (int cumulative, int? total) {
          records.addAll(<int?>[cumulative, total]);
59 60 61 62 63 64 65 66 67 68 69
        },
      );

      expect(records, <int>[
        chunkOne.length,
        syntheticTotal,
        chunkOne.length + chunkTwo.length,
        syntheticTotal,
      ]);
    });

70
    test('forwards errors from HttpClientResponse', () async {
71 72
      response = MockHttpClientResponse(error: Exception('Test Error'));
      response.contentLength = -1;
73

Dan Field's avatar
Dan Field committed
74
      expect(consolidateHttpClientResponseBytes(response), throwsException);
75
    });
76 77

    test('Propagates error to Future return value if onBytesReceived throws', () async {
78
      response.contentLength = -1;
79
      final Future<List<int>> result = consolidateHttpClientResponseBytes(
80
        response,
81
        onBytesReceived: (int cumulative, int? total) {
82 83 84 85 86 87 88 89
          throw 'misbehaving callback';
        },
      );

      expect(result, throwsA(equals('misbehaving callback')));
    });

    group('when gzipped', () {
90 91 92
      final List<int> gzipped = gzip.encode(chunkOne.followedBy(chunkTwo).toList());
      final List<int> gzippedChunkOne = gzipped.sublist(0, gzipped.length ~/ 2);
      final List<int> gzippedChunkTwo = gzipped.sublist(gzipped.length ~/ 2);
93 94

      setUp(() {
95 96
        response = MockHttpClientResponse(chunkOne: gzippedChunkOne, chunkTwo: gzippedChunkTwo);
        response.compressionState = HttpClientResponseCompressionState.compressed;
97 98
      });

99
      test('Uncompresses GZIP bytes if autoUncompress is true and response.compressionState is compressed', () async {
100
        response.contentLength = gzipped.length;
101
        final List<int> bytes = await consolidateHttpClientResponseBytes(response);
102 103 104
        expect(bytes, <int>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
      });

105
      test('returns gzipped bytes if autoUncompress is false and response.compressionState is compressed', () async {
106
        response.contentLength = gzipped.length;
107
        final List<int> bytes = await consolidateHttpClientResponseBytes(response, autoUncompress: false);
108 109 110 111
        expect(bytes, gzipped);
      });

      test('Notifies onBytesReceived with gzipped numbers', () async {
112
        response.contentLength = gzipped.length;
113
        final List<int?> records = <int?>[];
114
        await consolidateHttpClientResponseBytes(
115
          response,
116 117
          onBytesReceived: (int cumulative, int? total) {
            records.addAll(<int?>[cumulative, total]);
118 119 120 121 122 123 124 125 126 127 128
          },
        );

        expect(records, <int>[
          gzippedChunkOne.length,
          gzipped.length,
          gzipped.length,
          gzipped.length,
        ]);
      });

129
      test('Notifies onBytesReceived with expectedContentLength of -1 if response.compressionState is decompressed', () async {
130
        final int syntheticTotal = (chunkOne.length + chunkTwo.length) * 2;
131 132
        response.compressionState = HttpClientResponseCompressionState.decompressed;
        response.contentLength = syntheticTotal;
133
        final List<int?> records = <int?>[];
134
        await consolidateHttpClientResponseBytes(
135
          response,
136 137
          onBytesReceived: (int cumulative, int? total) {
            records.addAll(<int?>[cumulative, total]);
138 139 140
          },
        );

141
        expect(records, <int?>[
142
          gzippedChunkOne.length,
143
          null,
144
          gzipped.length,
145
          null,
146 147 148
        ]);
      });
    });
149
  });
150 151
}

152
class MockHttpClientResponse extends Fake implements HttpClientResponse {
153
  MockHttpClientResponse({this.error, this.chunkOne = const <int>[], this.chunkTwo = const <int>[]});
154 155 156 157 158 159 160 161 162 163 164 165

  final dynamic error;
  final List<int> chunkOne;
  final List<int> chunkTwo;

  @override
  int contentLength = 0;

  @override
  HttpClientResponseCompressionState compressionState = HttpClientResponseCompressionState.notCompressed;

  @override
166
  StreamSubscription<List<int>> listen(void Function(List<int> event)? onData, {Function? onError, void Function()? onDone, bool? cancelOnError}) {
167
    if (error != null) {
168
      return Stream<List<int>>.fromFuture(Future<List<int>>.error(error as Object)).listen(
169 170 171 172 173 174
          onData,
          onDone: onDone,
          onError: onError,
          cancelOnError: cancelOnError,
        );
    }
175
    return Stream<List<int>>.fromIterable(<List<int>>[chunkOne, chunkTwo]).listen(
176 177 178 179 180 181 182
      onData,
      onDone: onDone,
      onError: onError,
      cancelOnError: cancelOnError,
    );
  }
}