Skip to content

Commit

Permalink
More
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Dec 15, 2023
1 parent 53f0945 commit 27b4b1e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
21 changes: 14 additions & 7 deletions pkgs/http/lib/src/mock_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,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 @@ -73,13 +78,15 @@ class MockClient extends BaseClient {
}

/// Return a response containing a PNG image.
static Response pngResponse() => Response.bytes(
base64Decode(
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQ'
'GAhKmMIQAAAABJRU5ErkJggg==',
),
200,
headers: const {'Content-Type': 'image/png'});
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
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 27b4b1e

Please sign in to comment.