mock_image_http.dart 1.91 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
import 'dart:async';
6 7 8
import 'dart:io';

import 'package:mockito/mockito.dart';
9

10
import '../../../packages/flutter/test/painting/image_data.dart';
11 12

// Returns a mock HTTP client that responds with an image to all requests.
13
MockHttpClient createMockImageHttpClient(SecurityContext _) {
14 15 16 17 18
  final MockHttpClient client = MockHttpClient();
  final MockHttpClientRequest request = MockHttpClientRequest();
  final MockHttpClientResponse response = MockHttpClientResponse();
  final MockHttpHeaders headers = MockHttpHeaders();
  when(client.getUrl(any)).thenAnswer((_) => Future<HttpClientRequest>.value(request));
19
  when(request.headers).thenReturn(headers);
20
  when(request.close()).thenAnswer((_) => Future<HttpClientResponse>.value(response));
21
  when(response.contentLength).thenReturn(kTransparentImage.length);
22
  when(response.statusCode).thenReturn(HttpStatus.ok);
23
  when(response.listen(any)).thenAnswer((Invocation invocation) {
24 25 26 27
    final void Function(List<int>) onData = invocation.positionalArguments[0] as void Function(List<int>);
    final void Function() onDone = invocation.namedArguments[#onDone] as void Function();
    final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError] as void Function(Object, [StackTrace]);
    final bool cancelOnError = invocation.namedArguments[#cancelOnError] as bool;
28
    return Stream<List<int>>.fromIterable(<List<int>>[kTransparentImage]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
29
  });
30 31 32 33 34 35 36 37 38
  return client;
}

class MockHttpClient extends Mock implements HttpClient {}

class MockHttpClientRequest extends Mock implements HttpClientRequest {}

class MockHttpClientResponse extends Mock implements HttpClientResponse {}

39
class MockHttpHeaders extends Mock implements HttpHeaders {}