Skip to content

Commit

Permalink
Consolidate download stream tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kuhnroyal committed Mar 3, 2024
1 parent 2c3abd2 commit f37c5e2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 49 deletions.
30 changes: 0 additions & 30 deletions dio/test/cancel_token_test.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -59,7 +61,7 @@ void main() {
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/request_test.dart',
stackTraceContains: 'test/download_stream_tests.dart',
),
);
});
Expand All @@ -80,7 +82,7 @@ void main() {
),
throwsDioException(
DioExceptionType.cancel,
stackTraceContains: 'test/request_test.dart',
stackTraceContains: 'test/download_stream_tests.dart',
),
);

Expand All @@ -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',
),
);
});
Expand All @@ -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');
}
1 change: 1 addition & 0 deletions dio_test/lib/src/test/suite.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef TestSuiteFunction = void Function(

const _tests = [
corsTests,
downloadStreamTests,
headerTests,
httpMethodTests,
parameterTests,
Expand Down
1 change: 1 addition & 0 deletions dio_test/lib/tests.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit f37c5e2

Please sign in to comment.