Skip to content

Commit

Permalink
Test improvements (#1875)
Browse files Browse the repository at this point in the history
* restore base64 comparison in upload tests - httpbun has base64 data
now implemented
* add tests for async SocketException being thrown in the IO adapter
  • Loading branch information
kuhnroyal authored Jun 21, 2023
1 parent 73e5b13 commit 7cfadc4
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 29 deletions.
110 changes: 89 additions & 21 deletions dio/test/stacktrace_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -177,28 +177,96 @@ void main() async {
testOn: '!browser',
);

test(
DioExceptionType.unknown,
() async {
final dio = Dio(BaseOptions()..baseUrl = 'https://does.not.exist');
group('DioExceptionType.unknown', () {
test(
JsonUnsupportedObjectError,
() async {
final dio = Dio(BaseOptions()..baseUrl = 'https://does.not.exist');

await expectLater(
dio.get(
'/test',
options: Options(contentType: Headers.jsonContentType),
data: Object(),
),
throwsA(allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is JsonUnsupportedObjectError,
(DioException e) =>
e.stackTrace.toString().contains('test/stacktrace_test.dart'),
])),
);
},
testOn: '!browser',
);
await expectLater(
dio.get(
'/test',
options: Options(contentType: Headers.jsonContentType),
data: Object(),
),
throwsA(allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is JsonUnsupportedObjectError,
(DioException e) =>
e.stackTrace.toString().contains('test/stacktrace_test.dart'),
])),
);
},
testOn: '!browser',
);

test(
'SocketException on request',
() async {
final dio = Dio(BaseOptions()..baseUrl = 'https://does.not.exist')
..httpClientAdapter = IOHttpClientAdapter();

await expectLater(
dio.get(
'/test',
data: 'test',
),
throwsA(allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is SocketException,
(DioException e) => (e.error as SocketException)
.message
.startsWith("Failed host lookup: 'does.not.exist'"),
(DioException e) =>
e.stackTrace.toString().contains('test/stacktrace_test.dart'),
])),
);
},
testOn: 'vm',
);

test(
'SocketException on response',
() async {
final dio = Dio(BaseOptions()..baseUrl = 'https://does.not.exist')
..httpClientAdapter = IOHttpClientAdapter(
createHttpClient: () {
final request = MockHttpClientRequest();
final client = MockHttpClient();
when(client.openUrl(any, any)).thenAnswer(
(_) async => request,
);
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')),
);
return client;
},
);

await expectLater(
dio.get(
'/test',
data: 'test',
),
throwsA(allOf([
isA<DioException>(),
(DioException e) => e.type == DioExceptionType.unknown,
(DioException e) => e.error is SocketException,
(DioException e) =>
e.stackTrace.toString().contains('test/stacktrace_test.dart'),
])),
);
},
testOn: 'vm',
);
});

test('Interceptor gets stacktrace in onError', () async {
final dio = Dio();
Expand Down
12 changes: 4 additions & 8 deletions dio/test/upload_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,8 @@ void main() {
);
expect(r.data['headers']['Content-Length'], contentLength.toString());

// Image content comparison not working with httpbun for now.
// See https://github.com/sharat87/httpbun/issues/5
// final img = base64Encode(f.readAsBytesSync());
// expect(r.data['data'], 'data:application/octet-stream;base64,$img');
final img = base64Encode(f.readAsBytesSync());
expect(r.data['data'], img);
},
testOn: 'vm',
);
Expand All @@ -94,10 +92,8 @@ void main() {
);
expect(r.data['headers']['Content-Length'], contentLength.toString());

// Image content comparison not working with httpbun for now.
// See https://github.com/sharat87/httpbun/issues/5
// final img = base64Encode(f.readAsBytesSync());
// expect(r.data['data'], 'data:application/octet-stream;base64,$img');
final img = base64Encode(f.readAsBytesSync());
expect(r.data['data'], img);
},
testOn: 'vm',
);
Expand Down

0 comments on commit 7cfadc4

Please sign in to comment.