fix: improve OAuth authentication tests to handle null data cases

This commit is contained in:
Udhay-Adithya
2025-08-07 22:18:14 +05:30
parent fbbbcbae05
commit a12254ba7e
2 changed files with 35 additions and 13 deletions

View File

@@ -60,18 +60,34 @@ void main() {
}); });
test('should find available port when default is busy', () async { test('should find available port when default is busy', () async {
// Start a server on port 8080 to make it busy // Find a port that's actually available for testing
final busyServer = await HttpServer.bind( late int testPort;
InternetAddress.loopbackIPv4, late HttpServer busyServer;
8080,
); // 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 { try {
final callbackUrl = await server.start(); final callbackUrl = await server.start();
// Should find a different port // Should find a different port
expect(callbackUrl, startsWith('http://localhost:')); expect(callbackUrl, startsWith('http://localhost:'));
expect(callbackUrl, isNot(contains(':8080'))); expect(callbackUrl, isNot(contains(':$testPort')));
} finally { } finally {
await busyServer.close(); await busyServer.close();
} }

View File

@@ -423,7 +423,7 @@ void main() {
); );
test( 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 { () async {
const httpRequestModel = HttpRequestModel( const httpRequestModel = HttpRequestModel(
method: HTTPVerb.get, method: HTTPVerb.get,
@@ -432,15 +432,15 @@ void main() {
const authModel = AuthModel(type: APIAuthType.oauth1); const authModel = AuthModel(type: APIAuthType.oauth1);
expect( final result = await handleAuth(httpRequestModel, authModel);
() async => await handleAuth(httpRequestModel, authModel),
throwsA(isA<UnimplementedError>()), expect(result.headers, isEmpty);
); expect(result.isHeaderEnabledList, isEmpty);
}, },
); );
test( 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 { () async {
const httpRequestModel = HttpRequestModel( const httpRequestModel = HttpRequestModel(
method: HTTPVerb.get, method: HTTPVerb.get,
@@ -451,7 +451,13 @@ void main() {
expect( expect(
() async => await handleAuth(httpRequestModel, authModel), () async => await handleAuth(httpRequestModel, authModel),
throwsA(isA<UnimplementedError>()), throwsA(
isA<Exception>().having(
(e) => e.toString(),
'message',
contains('Failed to get OAuth2 Data'),
),
),
); );
}, },
); );