Skip to content

Commit

Permalink
Merge branch 'master' into feat/io-streamed-response-redirects
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 authored Dec 16, 2023
2 parents 09a0a71 + c114aa0 commit 1928b38
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 5 deletions.
44 changes: 41 additions & 3 deletions .github/workflows/dart.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkgs/http/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.1.3-wip

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

## 1.1.2
Expand Down
2 changes: 1 addition & 1 deletion pkgs/http/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ Some well supported implementations are:
| Implementation | Supported Platforms | SDK | Caching | HTTP3/QUIC | Platform Native |
| -------------- | ------------------- | ----| ------- | ---------- | --------------- |
| `package:http`[`IOClient`][ioclient] | Android, iOS, Linux, macOS, Windows | Dart, Flutter ||||
| `package:http`[`BrowserClient`][browserclient] | Web | Flutter || ✅︎ | ✅︎ | Dart, Flutter |
| `package:http`[`BrowserClient`][browserclient] | Web | Dart, Flutter || ✅︎ | ✅︎ | Dart, Flutter |
| [`package:cupertino_http`][cupertinohttp][`CupertinoClient`][cupertinoclient] | iOS, macOS | Flutter | ✅︎ | ✅︎ | ✅︎ |
| [`package:cronet_http`][cronethttp][`CronetClient`][cronetclient] | Android | Flutter | ✅︎ | ✅︎ ||
| [`package:fetch_client`][fetch][`FetchClient`][fetchclient] | Web | Dart, Flutter | ✅︎ | ✅︎ | ✅︎ |
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: 2 additions & 0 deletions pkgs/http/mono_pkg.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ stages:
- command: dart run --define=no_default_http_client=true test/no_default_http_client_test.dart
os:
- linux
- test: --test-randomize-ordering-seed=random -p chrome -c dart2wasm
sdk: dev
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'));
});
}
4 changes: 4 additions & 0 deletions tool/ci.sh

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1928b38

Please sign in to comment.