From f37c5e21517546a81c1827dade6eb137ba9c031f Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Sun, 3 Mar 2024 01:27:37 +0100 Subject: [PATCH] Consolidate download stream tests --- dio/test/cancel_token_test.dart | 30 -------------- .../lib/src/test/download_stream_tests.dart | 40 ++++++++++--------- dio_test/lib/src/test/suite.dart | 1 + dio_test/lib/tests.dart | 1 + 4 files changed, 23 insertions(+), 49 deletions(-) rename plugins/http2_adapter/test/request_test.dart => dio_test/lib/src/test/download_stream_tests.dart (80%) diff --git a/dio/test/cancel_token_test.dart b/dio/test/cancel_token_test.dart index a72dd6780..c6dc0da8c 100644 --- a/dio/test/cancel_token_test.dart +++ b/dio/test/cancel_token_test.dart @@ -1,5 +1,4 @@ import 'package:dio/dio.dart'; -import 'package:dio_test/util.dart'; import 'package:dio/src/adapters/io_adapter.dart'; import 'package:mockito/mockito.dart'; import 'package:test/test.dart'; @@ -30,35 +29,6 @@ void main() { CancelToken().cancel(); }); - test( - 'cancels streamed responses', - () async { - final dio = Dio()..options.baseUrl = 'https://httpbun.com/'; - - final cancelToken = CancelToken(); - - final response = await dio.get( - 'bytes/${1024 * 1024 * 100}', - options: Options(responseType: ResponseType.stream), - cancelToken: cancelToken, - onReceiveProgress: (c, t) { - if (c > 5000) { - cancelToken.cancel(); - } - }, - ); - - await expectLater( - (response.data as ResponseBody).stream.last, - throwsDioException( - DioExceptionType.cancel, - stackTraceContains: 'test/cancel_token_test.dart', - ), - ); - }, - testOn: 'vm', - ); - test('cancels multiple requests', () async { final client = MockHttpClient(); final token = CancelToken(); diff --git a/plugins/http2_adapter/test/request_test.dart b/dio_test/lib/src/test/download_stream_tests.dart similarity index 80% rename from plugins/http2_adapter/test/request_test.dart rename to dio_test/lib/src/test/download_stream_tests.dart index 1c4e45bbe..e45fbf83c 100644 --- a/plugins/http2_adapter/test/request_test.dart +++ b/dio_test/lib/src/test/download_stream_tests.dart @@ -1,27 +1,29 @@ import 'dart:io'; -import 'package:dio_http2_adapter/dio_http2_adapter.dart'; import 'package:dio/dio.dart'; -import 'package:dio_test/util.dart'; import 'package:path/path.dart' as p; import 'package:test/test.dart'; -void main() { - late Directory tmp; +import '../../util.dart'; - final dio = Dio() - ..httpClientAdapter = Http2Adapter(null) - ..options.baseUrl = 'https://httpbun.com/'; +void downloadStreamTests( + Dio Function() create, +) { + group('download', () { + late Dio dio; + late Directory tmp; - setUpAll(() { - tmp = Directory.systemTemp.createTempSync('dio_test_'); - addTearDown(() { - tmp.deleteSync(recursive: true); + setUp(() { + dio = create(); }); - }); - group('requests >', () { - test('download bytes', () async { + setUpAll(() { + tmp = Directory.systemTemp.createTempSync('dio_test_'); + addTearDown(() { + tmp.deleteSync(recursive: true); + }); + }); + test('bytes', () async { final path = p.join(tmp.path, 'bytes.txt'); final size = 10000; @@ -59,7 +61,7 @@ void main() { ), throwsDioException( DioExceptionType.cancel, - stackTraceContains: 'test/request_test.dart', + stackTraceContains: 'test/download_stream_tests.dart', ), ); }); @@ -80,7 +82,7 @@ void main() { ), throwsDioException( DioExceptionType.cancel, - stackTraceContains: 'test/request_test.dart', + stackTraceContains: 'test/download_stream_tests.dart', ), ); @@ -105,7 +107,7 @@ void main() { (response.data as ResponseBody).stream.last, throwsDioException( DioExceptionType.cancel, - stackTraceContains: 'test/request_test.dart', + stackTraceContains: 'test/download_stream_tests.dart', ), ); }); @@ -127,12 +129,12 @@ void main() { ), throwsDioException( DioExceptionType.cancel, - stackTraceContains: 'test/request_test.dart', + stackTraceContains: 'test/download_stream_tests.dart', ), ); await Future.delayed(const Duration(milliseconds: 250), () {}); expect(File(path).existsSync(), false); }); - }); + }, testOn: 'vm'); } diff --git a/dio_test/lib/src/test/suite.dart b/dio_test/lib/src/test/suite.dart index a3a675e4d..4abc37f8f 100644 --- a/dio_test/lib/src/test/suite.dart +++ b/dio_test/lib/src/test/suite.dart @@ -7,6 +7,7 @@ typedef TestSuiteFunction = void Function( const _tests = [ corsTests, + downloadStreamTests, headerTests, httpMethodTests, parameterTests, diff --git a/dio_test/lib/tests.dart b/dio_test/lib/tests.dart index 2c63d9ee6..9bd50ef37 100644 --- a/dio_test/lib/tests.dart +++ b/dio_test/lib/tests.dart @@ -1,4 +1,5 @@ export 'src/test/cors_tests.dart'; +export 'src/test/download_stream_tests.dart'; export 'src/test/headers_tests.dart'; export 'src/test/http_method_tests.dart'; export 'src/test/parameter_tests.dart';