flutter/dev/manual_tests/test/mock_image_http.dart
Jonah Williams 88cc977384
Remove package:http from Flutter (#15416)
* use HttpOverrides and dart:io HttpClient in flutter

* add missing package:http dependency

* update flutter packages and remove comment about createHttpClient from flutter_test

* move byte loading logic to common class, move string parsing logic to base class

* addAll doesn't work for a Uint8List

* use bytes.setRange

* undo addition to hello_world

* add newline to end of binding.dart

* and a newline for hello world

* refactor to function and add tests

* address comments on unknown length case

* alignment

* sort alaphabetically

* rename convertResponse to consolidateClientHttpClientResponseBytes.  Add header

* fix alignment in test
2018-03-22 08:21:01 -07:00

35 lines
1.7 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:mockito/mockito.dart';
import '../../../packages/flutter/test/painting/image_data.dart';
// Returns a mock HTTP client that responds with an image to all requests.
MockHttpClient createMockImageHttpClient(SecurityContext _) {
final MockHttpClient client = new MockHttpClient();
final MockHttpClientRequest request = new MockHttpClientRequest();
final MockHttpClientResponse response = new MockHttpClientResponse();
final MockHttpHeaders headers = new MockHttpHeaders();
when(client.getUrl(typed(any))).thenReturn(new Future<HttpClientRequest>.value(request));
when(request.headers).thenReturn(headers);
when(request.close()).thenReturn(new Future<HttpClientResponse>.value(response));
when(response.contentLength).thenReturn(kTransparentImage.length);
when(response.statusCode).thenReturn(HttpStatus.OK);
when(response.listen(typed(any))).thenAnswer((Invocation invocation) {
final void Function(List<int>) onData = invocation.positionalArguments[0];
final void Function() onDone = invocation.namedArguments[#onDone];
final void Function(Object, [StackTrace]) onError = invocation.namedArguments[#onError];
final bool cancelOnError = invocation.namedArguments[#cancelOnError];
return new Stream<List<int>>.fromIterable(<List<int>>[kTransparentImage]).listen(onData, onDone: onDone, onError: onError, cancelOnError: cancelOnError);
});
return client;
}
class MockHttpClient extends Mock implements HttpClient {}
class MockHttpClientRequest extends Mock implements HttpClientRequest {}
class MockHttpClientResponse extends Mock implements HttpClientResponse {}
class MockHttpHeaders extends Mock implements HttpHeaders {}