From 7cfadc47af1651688c78b6769f8efae55d3d5f11 Mon Sep 17 00:00:00 2001 From: Peter Leibiger Date: Wed, 21 Jun 2023 09:28:11 +0200 Subject: [PATCH] Test improvements (#1875) * restore base64 comparison in upload tests - httpbun has base64 data now implemented * add tests for async SocketException being thrown in the IO adapter --- dio/test/stacktrace_test.dart | 110 +++++++++++++++++++++++++++------- dio/test/upload_test.dart | 12 ++-- 2 files changed, 93 insertions(+), 29 deletions(-) diff --git a/dio/test/stacktrace_test.dart b/dio/test/stacktrace_test.dart index 2f405f207..9451f515b 100644 --- a/dio/test/stacktrace_test.dart +++ b/dio/test/stacktrace_test.dart @@ -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 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 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 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 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(); diff --git a/dio/test/upload_test.dart b/dio/test/upload_test.dart index da3d51118..4cef5f5df 100644 --- a/dio/test/upload_test.dart +++ b/dio/test/upload_test.dart @@ -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', ); @@ -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', );