Skip to content

Commit

Permalink
Test improvements (cfug#2152)
Browse files Browse the repository at this point in the history
* move some tests from http2/dio to shared test project
* add some new tests for `validateStatus`
* wrap `SocketException` in `DioExceptionType.connectionError` instead
of `DioExceptionType.unknown` in the `Http2Adapter` -> this brings the
behavior in line with the default adapters

<!-- Write down your pull request descriptions. -->

### New Pull Request Checklist

- [ ] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [ ] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [ ] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [ ] I have added the required tests to prove the fix/feature I'm
adding
- [ ] I have updated the documentation (if necessary)
- [ ] I have run the tests without failures
- [ ] I have updated the `CHANGELOG.md` in the corresponding package

### Additional context and info (if any)

<!-- Provide more context and info about the PR. -->
  • Loading branch information
kuhnroyal committed Mar 21, 2024
1 parent faa0b6c commit 95d1fa4
Show file tree
Hide file tree
Showing 18 changed files with 510 additions and 531 deletions.
3 changes: 3 additions & 0 deletions dio/dart_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ override_platforms:
settings:
# headless argument has to be set explicitly for non-chrome browsers
arguments: --headless
executable:
# https://github.com/dart-lang/test/pull/2195
mac_os: '/Applications/Firefox.app/Contents/MacOS/firefox'
48 changes: 0 additions & 48 deletions dio/test/basic_test.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
import 'dart:async';
import 'dart:io';

import 'package:dio/dio.dart';
import 'package:dio/src/utils.dart';
import 'package:test/test.dart';

import 'mock/adapters.dart';

void main() {
test('send with an invalid URL', () async {
await expectLater(
Dio().get('http://http.invalid'),
throwsA(
allOf([
isA<DioException>(),
(DioException e) => e.type == (DioExceptionType.connectionError),
if (!kIsWeb) (DioException e) => e.error is SocketException,
]),
),
);
});

test('cancellation', () async {
final dio = Dio()
..httpClientAdapter = MockAdapter()
Expand All @@ -36,37 +21,4 @@ void main() {
throwsA((e) => e is DioException && CancelToken.isCancel(e)),
);
});

test('status error', () async {
final dio = Dio()
..options.baseUrl = EchoAdapter.mockBase
..httpClientAdapter = EchoAdapter();
await expectLater(
dio.get('/401'),
throwsA(
(e) =>
e is DioException &&
e.type == DioExceptionType.badResponse &&
e.response!.statusCode == 401,
),
);
final r = await dio.get(
'/401',
options: Options(validateStatus: (status) => true),
);
expect(r.statusCode, 401);
});

test('post map', () async {
final dio = Dio()
..options.baseUrl = EchoAdapter.mockBase
..httpClientAdapter = EchoAdapter();

final response = await dio.post(
'/post',
data: {'a': 1, 'b': 2, 'c': 3},
);
expect(response.data, '{"a":1,"b":2,"c":3}');
expect(response.statusCode, 200);
});
}
54 changes: 30 additions & 24 deletions dio/test/download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,33 +82,39 @@ void main() {
expect(response.data, null);
});

test('download timeout', () async {
final dio = Dio();
final timeoutMatcher = allOf([
throwsA(isA<DioException>()),
throwsA(
predicate<DioException>(
(e) => e.type == DioExceptionType.receiveTimeout,
test(
'download timeout',
() async {
final dio = Dio();
final timeoutMatcher = allOf([
throwsA(isA<DioException>()),
throwsA(
predicate<DioException>(
(e) => e.type == DioExceptionType.receiveTimeout,
),
),
),
]);
await expectLater(
dio.downloadUri(
Uri.parse('$serverUrl/download').replace(
queryParameters: {'count': '3', 'gap': '2'},
]);
await expectLater(
dio.downloadUri(
Uri.parse('$serverUrl/download').replace(
queryParameters: {'count': '3', 'gap': '2'},
),
p.join(tmp.path, 'download_timeout.md'),
options: Options(receiveTimeout: Duration(seconds: 1)),
),
p.join(tmp.path, 'download_timeout.md'),
timeoutMatcher,
);
// Throws nothing if it constantly gets response bytes.
await dio.download(
'https://github.com/cfug/flutter.cn/archive/refs/heads/main.zip',
p.join(tmp.path, 'main.zip'),
options: Options(receiveTimeout: Duration(seconds: 1)),
),
timeoutMatcher,
);
// Throws nothing if it constantly gets response bytes.
await dio.download(
'https://github.com/cfug/flutter.cn/archive/refs/heads/main.zip',
p.join(tmp.path, 'main.zip'),
options: Options(receiveTimeout: Duration(seconds: 1)),
);
});
);
},
// The download of the main.zip file can be slow,
// so we need to increase the timeout.
timeout: Timeout(Duration(minutes: 1)),
);

test('download cancellation', () async {
final savePath = p.join(tmp.path, 'download_cancellation.md');
Expand Down
22 changes: 0 additions & 22 deletions dio/test/exception_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,6 @@ void main() {
expect(error, isA<Exception>());
});

test('catch DioException', () async {
DioException? error;
try {
await Dio().get('https://does.not.exist');
fail('did not throw');
} on DioException catch (e) {
error = e;
}
expect(error, isNotNull);
});

test('catch DioException as Exception', () async {
DioException? error;
try {
await Dio().get('https://does.not.exist');
fail('did not throw');
} on DioException catch (e) {
error = e;
}
expect(error, isNotNull);
});

test(
'catch DioException: hostname mismatch',
() async {
Expand Down
81 changes: 81 additions & 0 deletions dio_test/lib/src/test/basic_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import 'dart:io';

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

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

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

group('basic request', () {
test(
'works with non-TLS requests',
() async {
await dio.get('http://flutter.cn/');
await dio.get('https://flutter.cn/non-exist-destination');
},
testOn: 'vm',
);

test('fails with an invalid HTTP URL', () {
expectLater(
dio.get('http://does.not.exist'),
throwsDioException(
DioExceptionType.connectionError,
matcher: kIsWeb
? null
: isA<DioException>().having(
(e) => e.error,
'inner exception',
isA<SocketException>(),
),
),
);
});

test('fails with an invalid HTTPS URL', () {
expectLater(
dio.get('https://does.not.exist'),
throwsDioException(
DioExceptionType.connectionError,
matcher: kIsWeb
? null
: isA<DioException>().having(
(e) => e.error,
'inner exception',
isA<SocketException>(),
),
),
);
});

test('throws DioException that can be caught', () async {
try {
await dio.get('https://does.not.exist');
fail('did not throw');
} on DioException catch (e) {
expect(e, isNotNull);
}
});

test('POST string', () async {
final response = await dio.post('/post', data: 'TEST');
expect(response.data['data'], 'TEST');
});

test('POST map', () async {
final response = await dio.post(
'/post',
data: {'a': 1, 'b': 2, 'c': 3},
);
expect(response.data['data'], '{"a":1,"b":2,"c":3}');
expect(response.statusCode, 200);
});
});
}
Loading

0 comments on commit 95d1fa4

Please sign in to comment.