Skip to content

Commit

Permalink
Improve tests (#1918)
Browse files Browse the repository at this point in the history
<!-- Write down your pull request descriptions. -->

- Change `SocketException on request` to be platform-independent.
- Improve the readability of `SocketException on response`.

### New Pull Request Checklist

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

Co-authored-by: Flop <[email protected]>
  • Loading branch information
hgraceb and hgraceb authored Aug 3, 2023
1 parent 3762bd3 commit b5b0d43
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,15 @@ void main() async {
() async {
final dio = Dio()
..options.baseUrl = 'https://does.not.exist'
..httpClientAdapter = IOHttpClientAdapter();
..httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final client = MockHttpClient();
when(client.openUrl(any, any)).thenThrow(
SocketException('test'),
);
return client;
},
);

await expectLater(
dio.get('/test', data: 'test'),
Expand All @@ -247,9 +255,6 @@ void main() async {
isA<DioException>(),
(e) => e.type == DioExceptionType.unknown,
(e) => e.error is SocketException,
(e) => (e.error as SocketException)
.message
.startsWith("Failed host lookup: 'does.not.exist'"),
(e) => e.stackTrace
.toString()
.contains('test/stacktrace_test.dart'),
Expand All @@ -273,8 +278,9 @@ void main() async {
when(request.headers).thenReturn(MockHttpHeaders());
when(request.addStream(any)).thenAnswer((_) => Future.value());
when(request.close()).thenAnswer(
(_) async => Future.delayed(Duration(milliseconds: 50),
() => throw SocketException('test')),
(_) => Future.delayed(Duration(milliseconds: 50), () {
throw SocketException('test');
}),
);
return client;
},
Expand Down

0 comments on commit b5b0d43

Please sign in to comment.