Skip to content

Commit c114aa0

Browse files
authored
Add a fake response for PNG images (#1081)
1 parent db2cb76 commit c114aa0

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

pkgs/http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.1.3-wip
2+
3+
* Add `MockClient.pngResponse`, which makes it easier to fake image responses.
4+
15
## 1.1.2
26

37
* Allow `web: '>=0.3.0 <0.5.0'`.

pkgs/http/lib/src/mock_client.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
import 'dart:convert';
6+
57
import 'base_client.dart';
68
import 'base_request.dart';
79
import 'byte_stream.dart';
@@ -10,6 +12,11 @@ import 'response.dart';
1012
import 'streamed_request.dart';
1113
import 'streamed_response.dart';
1214

15+
final _pngImageData = base64Decode(
16+
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDw'
17+
'AEhQGAhKmMIQAAAABJRU5ErkJggg==',
18+
);
19+
1320
// TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
1421
// server APIs, MockClient should conform to it.
1522

@@ -69,6 +76,17 @@ class MockClient extends BaseClient {
6976
var bodyStream = request.finalize();
7077
return await _handler(request, bodyStream);
7178
}
79+
80+
/// Return a response containing a PNG image.
81+
static Response pngResponse({BaseRequest? request}) {
82+
final headers = {
83+
'content-type': 'image/png',
84+
'content-length': '${_pngImageData.length}'
85+
};
86+
87+
return Response.bytes(_pngImageData, 200,
88+
request: request, headers: headers, reasonPhrase: 'OK');
89+
}
7290
}
7391

7492
/// A handler function that receives [StreamedRequest]s and sends

pkgs/http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: http
2-
version: 1.1.2
2+
version: 1.1.3-wip
33
description: A composable, multi-platform, Future-based API for HTTP requests.
44
repository: https://github.com/dart-lang/http/tree/master/pkgs/http
55

pkgs/http/test/mock_client_test.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:convert';
66

77
import 'package:http/http.dart' as http;
8+
import 'package:http/src/request.dart';
89
import 'package:http/testing.dart';
910
import 'package:test/test.dart';
1011

@@ -43,4 +44,25 @@ void main() {
4344
expect(await client.read(Uri.http('example.com', '/foo')),
4445
equals('you did it'));
4546
});
47+
48+
test('pngResponse with default options', () {
49+
final response = MockClient.pngResponse();
50+
expect(response.statusCode, 200);
51+
expect(response.bodyBytes.take(8),
52+
[137, 80, 78, 71, 13, 10, 26, 10] // PNG header
53+
);
54+
expect(response.request, null);
55+
expect(response.headers, containsPair('content-type', 'image/png'));
56+
});
57+
58+
test('pngResponse with request', () {
59+
final request = Request('GET', Uri.https('example.com'));
60+
final response = MockClient.pngResponse(request: request);
61+
expect(response.statusCode, 200);
62+
expect(response.bodyBytes.take(8),
63+
[137, 80, 78, 71, 13, 10, 26, 10] // PNG header
64+
);
65+
expect(response.request, request);
66+
expect(response.headers, containsPair('content-type', 'image/png'));
67+
});
4668
}

0 commit comments

Comments
 (0)