consolidate_response_test.dart 3.37 KB
Newer Older
1 2 3 4
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

import 'package:flutter/foundation.dart';
import 'package:mockito/mockito.dart';

11 12
import '../flutter_test_alternative.dart';

13 14 15 16 17 18 19
void main() {
  group(consolidateHttpClientResponseBytes, () {
    final List<int> chunkOne = <int>[0, 1, 2, 3, 4, 5];
    final List<int> chunkTwo = <int>[6, 7, 8, 9, 10];
    MockHttpClientResponse response;

    setUp(() {
20
      response = MockHttpClientResponse();
21
      when(response.listen(
22
         any,
23 24
         onDone: anyNamed('onDone'),
         onError: anyNamed('onError'),
25
         cancelOnError: anyNamed('cancelOnError'),
26 27 28 29 30 31
      )).thenAnswer((Invocation invocation) {
        final void Function(List<int>) onData = invocation.positionalArguments[0];
        final void Function(Object) onError = invocation.namedArguments[#onError];
        final void Function() onDone = invocation.namedArguments[#onDone];
        final bool cancelOnError = invocation.namedArguments[#cancelOnError];

32
        return Stream<List<int>>.fromIterable(
33 34 35 36 37 38
            <List<int>>[chunkOne, chunkTwo]).listen(
          onData,
          onDone: onDone,
          onError: onError,
          cancelOnError: cancelOnError,
        );
39 40 41
      });
    });

42
    test('Converts an HttpClientResponse with contentLength to bytes', () async {
43 44 45 46
      when(response.contentLength)
          .thenReturn(chunkOne.length + chunkTwo.length);
      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
    test('Converts a compressed HttpClientResponse with contentLength to bytes', () async {
52 53 54 55 56 57 58
      when(response.contentLength).thenReturn(chunkOne.length);
      final List<int> bytes =
          await consolidateHttpClientResponseBytes(response);

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

59
    test('Converts an HttpClientResponse without contentLength to bytes', () async {
60
      when(response.contentLength).thenReturn(-1);
61 62
      final List<int> bytes =
          await consolidateHttpClientResponseBytes(response);
63 64 65 66 67 68

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

    test('forwards errors from HttpClientResponse', () async {
      when(response.listen(
69
        any,
70 71
        onDone: anyNamed('onDone'),
        onError: anyNamed('onError'),
72
        cancelOnError: anyNamed('cancelOnError'),
73 74 75 76 77 78
      )).thenAnswer((Invocation invocation) {
        final void Function(List<int>) onData = invocation.positionalArguments[0];
        final void Function(Object) onError = invocation.namedArguments[#onError];
        final void Function() onDone = invocation.namedArguments[#onDone];
        final bool cancelOnError = invocation.namedArguments[#cancelOnError];

79 80
        return Stream<List<int>>.fromFuture(
                Future<List<int>>.error(Exception('Test Error')))
81 82 83 84 85 86
            .listen(
          onData,
          onDone: onDone,
          onError: onError,
          cancelOnError: cancelOnError,
        );
87 88 89
      });
      when(response.contentLength).thenReturn(-1);

90
      expect(consolidateHttpClientResponseBytes(response),
91
          throwsA(isInstanceOf<Exception>()));
92 93 94 95
    });
  });
}

96
class MockHttpClientResponse extends Mock implements HttpClientResponse {}