Skip to content

Commit

Permalink
✅ Tests for HTTP/2
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexV525 committed Nov 24, 2023
1 parent e91c7a4 commit 7147ea6
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
4 changes: 2 additions & 2 deletions plugins/http2_adapter/test/http2_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:test/test.dart';
void main() {
test('adds one to input values', () async {
final dio = Dio()
..options.baseUrl = 'https://pub.dev/'
..options.baseUrl = 'https://httpbun.com/'
..interceptors.add(LogInterceptor())
..httpClientAdapter = Http2Adapter(
ConnectionManager(
Expand All @@ -16,7 +16,7 @@ void main() {
);

Response<String> response;
response = await dio.get('?xx=6');
response = await dio.get('get');
assert(response.statusCode == 200);
response = await dio.get(
'nkjnjknjn.html',
Expand Down
111 changes: 111 additions & 0 deletions plugins/http2_adapter/test/timeout_test.dart
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),
);
});
}

0 comments on commit 7147ea6

Please sign in to comment.