Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a fake response for PNG images #1081

Merged
merged 2 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[optional] Consider pull this out to a const pngHeader variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if I prefer that...so I'm leaving it for now.

);
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'));
});
}