Skip to content

Commit

Permalink
Local httpbun tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Mar 19, 2024
1 parent 35533ac commit a4d350e
Show file tree
Hide file tree
Showing 21 changed files with 98 additions and 93 deletions.
28 changes: 23 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ jobs:
cache: true
flutter-version: ${{ matrix.sdk == 'min' && '2.8.0' || '' }}
channel: ${{ matrix.sdk == 'min' && '' || matrix.channel }}
- run: |
chmod +x ./scripts/prepare_pinning_certs.sh
./scripts/prepare_pinning_certs.sh
- name: Install proxy for tests
run: sudo apt-get update && sudo apt-get install -y squid
- run: dart pub get
- uses: bluefireteam/melos-action@v3
with:
Expand All @@ -63,6 +58,28 @@ jobs:
- name: '[Verify step] Publish dry-run'
if: ${{ matrix.sdk == 'stable' }}
run: melos run publish-dry-run
# Tests
- run: ./scripts/prepare_pinning_certs.sh
- name: Install proxy for tests
run: sudo apt-get update && sudo apt-get install -y squid mkcert
- name: Start local httpbun
run: |
mkcert -install
mkcert -cert-file '/tmp/cert.pem' -key-file '/tmp/key.pem' httpbun.local
echo '127.0.0.1 httpbun.local' | sudo tee --append /etc/hosts
docker run \
--name httpbun \
--detach \
--publish 443:443 \
--volume /tmp:/tmp:ro \
--env HTTPBUN_TLS_CERT=/tmp/cert.pem \
--env HTTPBUN_TLS_KEY=/tmp/key.pem \
--pull always \
sharat87/httpbun
sleep 1
curl --fail --silent --show-error https://httpbun.local/any
- name: Use local httpbun in tests
run: melos run httpbun:local
- name: '[Verify step] Test Dart packages [VM]'
run: melos run test:vm
- name: '[Verify step] Test Dart packages [Chrome]'
Expand All @@ -71,6 +88,7 @@ jobs:
run: melos run test:web:firefox
- name: '[Verify step] Test Flutter packages'
run: melos run test:flutter
# Coverage
- name: '[Coverage] Format & print test coverage'
if: ${{ matrix.sdk == 'stable' }}
run: melos run coverage:show
Expand Down
2 changes: 1 addition & 1 deletion dio/test/download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ void main() {
expect(f.existsSync(), isTrue);

final cancelToken = CancelToken();
final dio = Dio()..options.baseUrl = 'https://httpbun.com';
final dio = Dio()..options.baseUrl = httpbunBaseUrl;

await expectLater(
dio.download(
Expand Down
2 changes: 1 addition & 1 deletion dio/test/test_suite_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import 'package:dio_test/tests.dart';

void main() {
dioAdapterTestSuite(
() => Dio(BaseOptions(baseUrl: 'https://httpbun.com/')),
(baseUrl) => Dio(BaseOptions(baseUrl: baseUrl)),
);
}
2 changes: 1 addition & 1 deletion dio/test/timeout_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ void main() {

setUp(() {
dio = Dio();
dio.options.baseUrl = 'https://httpbun.com/';
dio.options.baseUrl = httpbunBaseUrl;
});

group('Timeout exception of', () {
Expand Down
3 changes: 2 additions & 1 deletion dio/test/upload_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import 'dart:io';
import 'dart:typed_data';

import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

void main() {
late Dio dio;

setUp(() {
dio = Dio()..options.baseUrl = 'https://httpbun.com/';
dio = Dio()..options.baseUrl = httpbunBaseUrl;
});

test('Uint8List should not be transformed', () async {
Expand Down
18 changes: 0 additions & 18 deletions dio_test/dart_test.yaml

This file was deleted.

1 change: 1 addition & 0 deletions dio_test/lib/src/httpbun.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const httpbunBaseUrl = 'https://httpbun.com';
4 changes: 2 additions & 2 deletions dio_test/lib/src/test/cors_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import 'package:test/test.dart';
/// either "simple" or "preflighted". Reference:
/// https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS#simple_requests
void corsTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUp(() {
dio = create();
dio = create(httpbunBaseUrl);
});

group('CORS preflight', () {
Expand Down
12 changes: 6 additions & 6 deletions dio_test/lib/src/test/download_stream_tests.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@ import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:path/path.dart' as p;
import 'package:test/test.dart';

import '../../util.dart';

void downloadStreamTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
group('download', () {
late Dio dio;
late Directory tmp;

setUp(() {
dio = create();
dio = create(httpbunBaseUrl);
});

setUpAll(() {
Expand All @@ -24,6 +23,7 @@ void downloadStreamTests(
tmp.deleteSync(recursive: true);
});
});

test('bytes', () async {
final path = p.join(tmp.path, 'bytes.txt');

Expand Down Expand Up @@ -103,7 +103,7 @@ void downloadStreamTests(
test('cancels streamed response mid request', () async {
final cancelToken = CancelToken();
final response = await dio.get(
'bytes/${1024 * 1024 * 100}',
'/bytes/${1024 * 1024 * 100}',
options: Options(responseType: ResponseType.stream),
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
Expand All @@ -128,7 +128,7 @@ void downloadStreamTests(

await expectLater(
dio.download(
'bytes/${1024 * 1024 * 10}',
'/bytes/${1024 * 1024 * 10}',
path,
cancelToken: cancelToken,
onReceiveProgress: (c, t) {
Expand Down
7 changes: 4 additions & 3 deletions dio_test/lib/src/test/headers_tests.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

void headerTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUpAll(() {
dio = create();
setUp(() {
dio = create(httpbunBaseUrl);
});

group('headers', () {
Expand Down
8 changes: 5 additions & 3 deletions dio_test/lib/src/test/http_method_tests.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

void httpMethodTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
const data = {'content': 'I am payload'};

late Dio dio;
setUpAll(() {
dio = create();

setUp(() {
dio = create(httpbunBaseUrl);
});

group('HTTP method', () {
Expand Down
7 changes: 4 additions & 3 deletions dio_test/lib/src/test/parameter_tests.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

void parameterTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUpAll(() {
dio = create();
setUp(() {
dio = create(httpbunBaseUrl);
});

group('parameters', () {
Expand Down
11 changes: 5 additions & 6 deletions dio_test/lib/src/test/redirect_tests.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import 'package:dio/dio.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

import '../utils.dart';

void redirectTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUpAll(() {
dio = create();
setUp(() {
dio = create(httpbunBaseUrl);
});

group('redirects', () {
test('single', () async {
final response = await dio.get(
'/redirect',
queryParameters: {'url': 'https://httpbun.com/get'},
queryParameters: {'url': '$httpbunBaseUrl/get'},
onReceiveProgress: (received, total) {
// ignore progress
},
Expand Down
10 changes: 4 additions & 6 deletions dio_test/lib/src/test/status_code_tests.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import 'package:dio/dio.dart';
import 'package:dio_test/src/utils.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

import '../matcher.dart';

void statusCodeTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUpAll(() {
dio = create();
setUp(() {
dio = create(httpbunBaseUrl);
});

group('status code', () {
Expand Down
4 changes: 2 additions & 2 deletions dio_test/lib/src/test/suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:dio/dio.dart';
import 'package:dio_test/tests.dart';

typedef TestSuiteFunction = void Function(
Dio Function() create,
Dio Function(String baseUrl) create,
);

const _tests = [
Expand All @@ -17,7 +17,7 @@ const _tests = [
];

void dioAdapterTestSuite(
Dio Function() create, {
Dio Function(String baseUrl) create, {
List<TestSuiteFunction> tests = _tests,
}) =>
tests.forEach((test) => test(create));
26 changes: 10 additions & 16 deletions dio_test/lib/src/test/timeout_tests.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import 'dart:async';

import 'package:dio/dio.dart';
import 'package:dio_test/src/matcher.dart';
import 'package:dio_test/util.dart';
import 'package:test/test.dart';

import '../utils.dart';

void timeoutTests(
Dio Function() create,
Dio Function(String baseUrl) create,
) {
late Dio dio;

setUp(() {
dio = create();
dio = create(httpbunBaseUrl);
});

group('Timeout exception of', () {
Expand Down Expand Up @@ -80,30 +78,26 @@ void timeoutTests(
});

test('ignores zero duration timeouts', () async {
final dio = Dio(
BaseOptions(
baseUrl: 'https://httpbun.com/',
connectTimeout: Duration.zero,
receiveTimeout: Duration.zero,
),
);
dio.options
..connectTimeout = Duration.zero
..receiveTimeout = Duration.zero;
// Ignores zero duration timeouts from the base options.
await dio.get('/drip-lines?delay=1');
// Reset the base options.
dio.options.receiveTimeout = Duration(milliseconds: 10);
dio.options.receiveTimeout = Duration(milliseconds: 1);
await expectLater(
dio.get('/drip-lines?delay=1'),
throwsDioException(
DioExceptionType.receiveTimeout,
messageContains: '0:00:00.010000',
messageContains: dio.options.receiveTimeout.toString(),
),
);
dio.options.connectTimeout = Duration(milliseconds: 10);
dio.options.connectTimeout = Duration(milliseconds: 1);
await expectLater(
dio.get(nonRoutableUrl),
throwsDioException(
DioExceptionType.connectionTimeout,
messageContains: '0:00:00.010000',
messageContains: dio.options.connectTimeout.toString(),
),
);
dio.options.connectTimeout = Duration.zero;
Expand Down
1 change: 1 addition & 0 deletions dio_test/lib/util.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export 'src/httpbun.dart';
export 'src/matcher.dart';
export 'src/utils.dart';
6 changes: 6 additions & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ scripts:
exec: dart pub publish --dry-run
packageFilters:
noPrivate: true
httpbun:local:
description: Run httpbun locally
run: echo "const httpbunBaseUrl = 'https://httpbun.local';" > dio_test/lib/src/httpbun.dart
httpbun:com:
description: Run httpbun locally
run: echo "const httpbunBaseUrl = 'https://httpbun.com';" > dio_test/lib/src/httpbun.dart
test:
name: All tests
run: |
Expand Down
Loading

0 comments on commit a4d350e

Please sign in to comment.