From a12254ba7e450d0e546c8fb23b3cf00b9a19ee2d Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Thu, 7 Aug 2025 22:18:14 +0530 Subject: [PATCH] fix: improve OAuth authentication tests to handle null data cases --- .../services/oauth_callback_server_test.dart | 28 +++++++++++++++---- .../test/utils/auth/auth_handling_test.dart | 20 ++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/better_networking/test/services/oauth_callback_server_test.dart b/packages/better_networking/test/services/oauth_callback_server_test.dart index 4ef2440c..fc94e46e 100644 --- a/packages/better_networking/test/services/oauth_callback_server_test.dart +++ b/packages/better_networking/test/services/oauth_callback_server_test.dart @@ -60,18 +60,34 @@ void main() { }); test('should find available port when default is busy', () async { - // Start a server on port 8080 to make it busy - final busyServer = await HttpServer.bind( - InternetAddress.loopbackIPv4, - 8080, - ); + // Find a port that's actually available for testing + late int testPort; + late HttpServer busyServer; + + // Try to find an available port in the testing range 9080-9090 + for (int port = 9080; port <= 9090; port++) { + try { + busyServer = await HttpServer.bind( + InternetAddress.loopbackIPv4, + port, + ); + testPort = port; + break; + } catch (e) { + // Port is busy, try next one + if (port == 9090) { + // Skip this test if no ports available in our test range + return; + } + } + } try { final callbackUrl = await server.start(); // Should find a different port expect(callbackUrl, startsWith('http://localhost:')); - expect(callbackUrl, isNot(contains(':8080'))); + expect(callbackUrl, isNot(contains(':$testPort'))); } finally { await busyServer.close(); } diff --git a/packages/better_networking/test/utils/auth/auth_handling_test.dart b/packages/better_networking/test/utils/auth/auth_handling_test.dart index 40f2cb2a..9b8857f1 100644 --- a/packages/better_networking/test/utils/auth/auth_handling_test.dart +++ b/packages/better_networking/test/utils/auth/auth_handling_test.dart @@ -423,7 +423,7 @@ void main() { ); test( - 'given handleAuth when OAuth1 authentication is provided then it should throw UnimplementedError', + 'given handleAuth when OAuth1 authentication is provided but oauth1 data is null then it should not add headers', () async { const httpRequestModel = HttpRequestModel( method: HTTPVerb.get, @@ -432,15 +432,15 @@ void main() { const authModel = AuthModel(type: APIAuthType.oauth1); - expect( - () async => await handleAuth(httpRequestModel, authModel), - throwsA(isA()), - ); + final result = await handleAuth(httpRequestModel, authModel); + + expect(result.headers, isEmpty); + expect(result.isHeaderEnabledList, isEmpty); }, ); test( - 'given handleAuth when OAuth2 authentication is provided then it should throw UnimplementedError', + 'given handleAuth when OAuth2 authentication is provided but oauth2 data is null then it should throw Exception', () async { const httpRequestModel = HttpRequestModel( method: HTTPVerb.get, @@ -451,7 +451,13 @@ void main() { expect( () async => await handleAuth(httpRequestModel, authModel), - throwsA(isA()), + throwsA( + isA().having( + (e) => e.toString(), + 'message', + contains('Failed to get OAuth2 Data'), + ), + ), ); }, );