diff --git a/.github/workflows/dart.yml b/.github/workflows/dart.yml index baebff25d7..3cb8d19d33 100644 --- a/.github/workflows/dart.yml +++ b/.github/workflows/dart.yml @@ -540,6 +540,44 @@ jobs: - job_006 - job_007 job_015: + name: "unit_test; linux; Dart dev; PKG: pkgs/http; `dart test --test-randomize-ordering-seed=random -p chrome -c dart2wasm`" + runs-on: ubuntu-latest + steps: + - name: Cache Pub hosted dependencies + uses: actions/cache@704facf57e6136b1bc63b828d79edcd491f0ee84 + with: + path: "~/.pub-cache/hosted" + key: "os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http;commands:test_4" + restore-keys: | + os:ubuntu-latest;pub-cache-hosted;sdk:dev;packages:pkgs/http + os:ubuntu-latest;pub-cache-hosted;sdk:dev + os:ubuntu-latest;pub-cache-hosted + os:ubuntu-latest + - name: Setup Dart SDK + uses: dart-lang/setup-dart@b64355ae6ca0b5d484f0106a033dd1388965d06d + with: + sdk: dev + - id: checkout + name: Checkout repository + uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 + - id: pkgs_http_pub_upgrade + name: pkgs/http; dart pub upgrade + run: dart pub upgrade + if: "always() && steps.checkout.conclusion == 'success'" + working-directory: pkgs/http + - name: "pkgs/http; dart test --test-randomize-ordering-seed=random -p chrome -c dart2wasm" + run: "dart test --test-randomize-ordering-seed=random -p chrome -c dart2wasm" + if: "always() && steps.pkgs_http_pub_upgrade.conclusion == 'success'" + working-directory: pkgs/http + needs: + - job_001 + - job_002 + - job_003 + - job_004 + - job_005 + - job_006 + - job_007 + job_016: name: "unit_test; linux; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test --platform chrome`" runs-on: ubuntu-latest steps: @@ -577,7 +615,7 @@ jobs: - job_005 - job_006 - job_007 - job_016: + job_017: name: "unit_test; linux; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: ubuntu-latest steps: @@ -615,7 +653,7 @@ jobs: - job_005 - job_006 - job_007 - job_017: + job_018: name: "unit_test; macos; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: macos-latest steps: @@ -653,7 +691,7 @@ jobs: - job_005 - job_006 - job_007 - job_018: + job_019: name: "unit_test; windows; Flutter stable; PKG: pkgs/flutter_http_example; `flutter test`" runs-on: windows-latest steps: diff --git a/pkgs/http/CHANGELOG.md b/pkgs/http/CHANGELOG.md index 650b3e3a2b..a6edd66e3b 100644 --- a/pkgs/http/CHANGELOG.md +++ b/pkgs/http/CHANGELOG.md @@ -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 diff --git a/pkgs/http/README.md b/pkgs/http/README.md index 157a52f678..642c238035 100644 --- a/pkgs/http/README.md +++ b/pkgs/http/README.md @@ -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 | ✅︎ | ✅︎ | ✅︎ | diff --git a/pkgs/http/lib/src/mock_client.dart b/pkgs/http/lib/src/mock_client.dart index bf2df40ee7..52f108a577 100644 --- a/pkgs/http/lib/src/mock_client.dart +++ b/pkgs/http/lib/src/mock_client.dart @@ -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'; @@ -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. @@ -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 diff --git a/pkgs/http/mono_pkg.yaml b/pkgs/http/mono_pkg.yaml index 0e2f9d8aa7..06f79d9816 100644 --- a/pkgs/http/mono_pkg.yaml +++ b/pkgs/http/mono_pkg.yaml @@ -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 diff --git a/pkgs/http/pubspec.yaml b/pkgs/http/pubspec.yaml index 118292fa13..1645f96048 100644 --- a/pkgs/http/pubspec.yaml +++ b/pkgs/http/pubspec.yaml @@ -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 diff --git a/pkgs/http/test/mock_client_test.dart b/pkgs/http/test/mock_client_test.dart index db561c51d6..625285cb33 100644 --- a/pkgs/http/test/mock_client_test.dart +++ b/pkgs/http/test/mock_client_test.dart @@ -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'; @@ -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')); + }); } diff --git a/tool/ci.sh b/tool/ci.sh index 44ea987b60..d4cc8d2ee6 100755 --- a/tool/ci.sh +++ b/tool/ci.sh @@ -95,6 +95,10 @@ for PKG in ${PKGS}; do echo 'dart test --platform chrome' dart test --platform chrome || EXIT_CODE=$? ;; + test_4) + echo 'dart test --test-randomize-ordering-seed=random -p chrome -c dart2wasm' + dart test --test-randomize-ordering-seed=random -p chrome -c dart2wasm || EXIT_CODE=$? + ;; *) echo -e "\033[31mUnknown TASK '${TASK}' - TERMINATING JOB\033[0m" exit 64