Skip to content

Commit a12254b

Browse files
committed
fix: improve OAuth authentication tests to handle null data cases
1 parent fbbbcba commit a12254b

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

packages/better_networking/test/services/oauth_callback_server_test.dart

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,34 @@ void main() {
6060
});
6161

6262
test('should find available port when default is busy', () async {
63-
// Start a server on port 8080 to make it busy
64-
final busyServer = await HttpServer.bind(
65-
InternetAddress.loopbackIPv4,
66-
8080,
67-
);
63+
// Find a port that's actually available for testing
64+
late int testPort;
65+
late HttpServer busyServer;
66+
67+
// Try to find an available port in the testing range 9080-9090
68+
for (int port = 9080; port <= 9090; port++) {
69+
try {
70+
busyServer = await HttpServer.bind(
71+
InternetAddress.loopbackIPv4,
72+
port,
73+
);
74+
testPort = port;
75+
break;
76+
} catch (e) {
77+
// Port is busy, try next one
78+
if (port == 9090) {
79+
// Skip this test if no ports available in our test range
80+
return;
81+
}
82+
}
83+
}
6884

6985
try {
7086
final callbackUrl = await server.start();
7187

7288
// Should find a different port
7389
expect(callbackUrl, startsWith('http://localhost:'));
74-
expect(callbackUrl, isNot(contains(':8080')));
90+
expect(callbackUrl, isNot(contains(':$testPort')));
7591
} finally {
7692
await busyServer.close();
7793
}

packages/better_networking/test/utils/auth/auth_handling_test.dart

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void main() {
423423
);
424424

425425
test(
426-
'given handleAuth when OAuth1 authentication is provided then it should throw UnimplementedError',
426+
'given handleAuth when OAuth1 authentication is provided but oauth1 data is null then it should not add headers',
427427
() async {
428428
const httpRequestModel = HttpRequestModel(
429429
method: HTTPVerb.get,
@@ -432,15 +432,15 @@ void main() {
432432

433433
const authModel = AuthModel(type: APIAuthType.oauth1);
434434

435-
expect(
436-
() async => await handleAuth(httpRequestModel, authModel),
437-
throwsA(isA<UnimplementedError>()),
438-
);
435+
final result = await handleAuth(httpRequestModel, authModel);
436+
437+
expect(result.headers, isEmpty);
438+
expect(result.isHeaderEnabledList, isEmpty);
439439
},
440440
);
441441

442442
test(
443-
'given handleAuth when OAuth2 authentication is provided then it should throw UnimplementedError',
443+
'given handleAuth when OAuth2 authentication is provided but oauth2 data is null then it should throw Exception',
444444
() async {
445445
const httpRequestModel = HttpRequestModel(
446446
method: HTTPVerb.get,
@@ -451,7 +451,13 @@ void main() {
451451

452452
expect(
453453
() async => await handleAuth(httpRequestModel, authModel),
454-
throwsA(isA<UnimplementedError>()),
454+
throwsA(
455+
isA<Exception>().having(
456+
(e) => e.toString(),
457+
'message',
458+
contains('Failed to get OAuth2 Data'),
459+
),
460+
),
455461
);
456462
},
457463
);

0 commit comments

Comments
 (0)