image_headers_test.dart 2.2 KB
Newer Older
1 2 3 4 5
// 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.

import 'dart:async';
6 7
import 'dart:io';

8 9
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
10
import 'package:mockito/mockito.dart';
11

12
import '../painting/image_data.dart';
13 14

void main() {
15 16 17 18
  final MockHttpClient client = MockHttpClient();
  final MockHttpClientRequest request = MockHttpClientRequest();
  final MockHttpClientResponse response = MockHttpClientResponse();
  final MockHttpHeaders headers = MockHttpHeaders();
19

20
  testWidgets('Headers', (WidgetTester tester) async {
21
    HttpOverrides.runZoned<Future<void>>(() async {
22
      await tester.pumpWidget(Image.network(
23 24 25 26 27 28 29
        'https://www.example.com/images/frame.png',
        headers: const <String, String>{'flutter': 'flutter'},
      ));

      verify(headers.add('flutter', 'flutter')).called(1);

    }, createHttpClient: (SecurityContext _) {
30
      when(client.getUrl(any)).thenAnswer((_) => Future<HttpClientRequest>.value(request));
31
      when(request.headers).thenReturn(headers);
32
      when(request.close()).thenAnswer((_) => Future<HttpClientResponse>.value(response));
33
      when(response.contentLength).thenReturn(kTransparentImage.length);
34
      when(response.statusCode).thenReturn(HttpStatus.ok);
35
      when(response.listen(any)).thenAnswer((Invocation invocation) {
36 37
        final void Function(List<int>) onData = invocation.positionalArguments[0];
        final void Function() onDone = invocation.namedArguments[#onDone];
38
        final void Function(Object, [ StackTrace ]) onError = invocation.namedArguments[#onError];
39
        final bool cancelOnError = invocation.namedArguments[#cancelOnError];
40
        return Stream<List<int>>.fromIterable(<List<int>>[kTransparentImage]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
41 42 43
      });
      return client;
    });
44 45
  });
}
46 47 48 49 50 51 52

class MockHttpClient extends Mock implements HttpClient {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}

class MockHttpClientResponse extends Mock implements HttpClientResponse {}

53
class MockHttpHeaders extends Mock implements HttpHeaders {}