Skip to content

Commit

Permalink
Add a fake response for PNG images (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan authored Dec 15, 2023
1 parent db2cb76 commit c114aa0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.1.3-wip

* Add `MockClient.pngResponse`, which makes it easier to fake image responses.

## 1.1.2

* Allow `web: '>=0.3.0 <0.5.0'`.
Expand Down
18 changes: 18 additions & 0 deletions pkgs/http/lib/src/mock_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// for details. 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:convert';

import 'base_client.dart';
import 'base_request.dart';
import 'byte_stream.dart';
Expand All @@ -10,6 +12,11 @@ import 'response.dart';
import 'streamed_request.dart';
import 'streamed_response.dart';

final _pngImageData = base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDw'
'AEhQGAhKmMIQAAAABJRU5ErkJggg==',
);

// TODO(nweiz): once Dart has some sort of Rack- or WSGI-like standard for
// server APIs, MockClient should conform to it.

Expand Down Expand Up @@ -69,6 +76,17 @@ class MockClient extends BaseClient {
var bodyStream = request.finalize();
return await _handler(request, bodyStream);
}

/// Return a response containing a PNG image.
static Response pngResponse({BaseRequest? request}) {
final headers = {
'content-type': 'image/png',
'content-length': '${_pngImageData.length}'
};

return Response.bytes(_pngImageData, 200,
request: request, headers: headers, reasonPhrase: 'OK');
}
}

/// A handler function that receives [StreamedRequest]s and sends
Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: http
version: 1.1.2
version: 1.1.3-wip
description: A composable, multi-platform, Future-based API for HTTP requests.
repository: https://github.com/dart-lang/http/tree/master/pkgs/http

Expand Down
22 changes: 22 additions & 0 deletions pkgs/http/test/mock_client_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:http/src/request.dart';
import 'package:http/testing.dart';
import 'package:test/test.dart';

Expand Down Expand Up @@ -43,4 +44,25 @@ void main() {
expect(await client.read(Uri.http('example.com', '/foo')),
equals('you did it'));
});

test('pngResponse with default options', () {
final response = MockClient.pngResponse();
expect(response.statusCode, 200);
expect(response.bodyBytes.take(8),
[137, 80, 78, 71, 13, 10, 26, 10] // PNG header
);
expect(response.request, null);
expect(response.headers, containsPair('content-type', 'image/png'));
});

test('pngResponse with request', () {
final request = Request('GET', Uri.https('example.com'));
final response = MockClient.pngResponse(request: request);
expect(response.statusCode, 200);
expect(response.bodyBytes.take(8),
[137, 80, 78, 71, 13, 10, 26, 10] // PNG header
);
expect(response.request, request);
expect(response.headers, containsPair('content-type', 'image/png'));
});
}

0 comments on commit c114aa0

Please sign in to comment.