-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
@TestOn('vm') | ||
import 'package:dio/dio.dart'; | ||
import 'package:dio_http2_adapter/dio_http2_adapter.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
late Dio dio; | ||
|
||
setUp(() { | ||
dio = Dio(); | ||
dio.options.baseUrl = 'https://httpbun.com/'; | ||
dio.httpClientAdapter = Http2Adapter( | ||
ConnectionManager(idleTimeout: Duration(seconds: 30)), | ||
); | ||
}); | ||
|
||
test('catch DioException when connectTimeout', () { | ||
dio.options.connectTimeout = Duration(milliseconds: 3); | ||
|
||
expectLater( | ||
dio.get('/drip-lines?delay=2'), | ||
allOf( | ||
throwsA(isA<DioException>()), | ||
throwsA(predicate((DioException e) => | ||
e.type == DioExceptionType.connectionTimeout && | ||
e.message!.contains('0:00:00.003000'))), | ||
), | ||
); | ||
}); | ||
|
||
test('catch DioException when receiveTimeout', () { | ||
dio.options.receiveTimeout = Duration(milliseconds: 10); | ||
|
||
expectLater( | ||
dio.get( | ||
'/bytes/${1024 * 1024 * 20}', | ||
options: Options(responseType: ResponseType.stream), | ||
), | ||
allOf([ | ||
throwsA(isA<DioException>()), | ||
throwsA( | ||
predicate<DioException>( | ||
(e) => e.type == DioExceptionType.receiveTimeout, | ||
), | ||
), | ||
throwsA( | ||
predicate<DioException>((e) => e.message!.contains('0:00:00.010000')), | ||
), | ||
]), | ||
); | ||
}, testOn: 'vm'); | ||
|
||
test('no DioException when receiveTimeout > request duration', () async { | ||
dio.options.receiveTimeout = Duration(seconds: 5); | ||
|
||
await dio.get('/drip?delay=1&numbytes=1'); | ||
}); | ||
|
||
test('ignores zero duration timeouts', () async { | ||
final dio = Dio( | ||
BaseOptions( | ||
baseUrl: 'https://httpbun.com/', | ||
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); | ||
await expectLater( | ||
dio.get('/drip-lines?delay=1'), | ||
allOf([ | ||
throwsA(isA<DioException>()), | ||
throwsA( | ||
predicate<DioException>( | ||
(e) => e.type == DioExceptionType.receiveTimeout, | ||
), | ||
), | ||
throwsA( | ||
predicate<DioException>( | ||
(e) => e.message!.contains('0:00:00.010000'), | ||
), | ||
), | ||
]), | ||
); | ||
dio.options.connectTimeout = Duration(milliseconds: 10); | ||
await expectLater( | ||
dio.get('/drip-lines?delay=1'), | ||
allOf([ | ||
throwsA(isA<DioException>()), | ||
throwsA( | ||
predicate<DioException>( | ||
(e) => e.type == DioExceptionType.connectionTimeout, | ||
), | ||
), | ||
throwsA( | ||
predicate<DioException>( | ||
(e) => e.message!.contains('0:00:00.010000'), | ||
), | ||
), | ||
]), | ||
); | ||
dio.options.connectTimeout = Duration.zero; | ||
// Override with request options. | ||
await dio.get( | ||
'/drip-lines?delay=1', | ||
options: Options(receiveTimeout: Duration.zero), | ||
); | ||
}); | ||
} |