Skip to content

Commit 3c251ab

Browse files
committed
feat: update cred tests fix
1 parent 2194cc8 commit 3c251ab

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

tests/credentials.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,17 @@ describe("Credentials", () => {
8585
])("$description", async ({ apiTokenIssuer, expectedUrl }) => {
8686
const parsedUrl = new URL(expectedUrl);
8787

88-
// CRITICAL: Do NOT include default ports (443 for HTTPS, 80 for HTTP) in the base URL
89-
// because axios/Node.js URL.toString() omits them, but nock's interceptor sees them
90-
// in the format "hostname:port/path". We need to match what the actual HTTP request uses.
88+
// IMPORTANT: In Node.js 20 and earlier, nock's @mswjs/interceptors sees requests
89+
// with explicit default ports (443 for HTTPS, 80 for HTTP).
90+
// We must include the port in the nock base URL to match what the interceptor sees.
9191
let baseUrl;
9292
if (parsedUrl.port) {
9393
// Non-default port: include it
9494
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${parsedUrl.port}`;
9595
} else {
96-
// Default port: omit it to match axios behavior
97-
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`;
96+
// Default port: include it explicitly to match what nock's interceptor sees
97+
const defaultPort = parsedUrl.protocol === "https:" ? "443" : "80";
98+
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${defaultPort}`;
9899
}
99100

100101
// Extensive logging for CI debugging
@@ -203,16 +204,17 @@ describe("Credentials", () => {
203204
])("should normalize audience from apiTokenIssuer when using PrivateKeyJWT client credentials ($description)", async ({ apiTokenIssuer, expectedUrl, expectedAudience }) => {
204205
const parsedUrl = new URL(expectedUrl);
205206

206-
// CRITICAL: Do NOT include default ports (443 for HTTPS, 80 for HTTP) in the base URL
207-
// because axios/Node.js URL.toString() omits them, but nock's interceptor sees them
208-
// in the format "hostname:port/path". We need to match what the actual HTTP request uses.
207+
// IMPORTANT: In Node.js 20 and earlier, nock's @mswjs/interceptors sees requests
208+
// with explicit default ports (443 for HTTPS, 80 for HTTP).
209+
// We must include the port in the nock base URL to match what the interceptor sees.
209210
let baseUrl;
210211
if (parsedUrl.port) {
211212
// Non-default port: include it
212213
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${parsedUrl.port}`;
213214
} else {
214-
// Default port: omit it to match axios behavior
215-
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`;
215+
// Default port: include it explicitly to match what nock's interceptor sees
216+
const defaultPort = parsedUrl.protocol === "https:" ? "443" : "80";
217+
baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}:${defaultPort}`;
216218
}
217219

218220
// Extensive logging for CI debugging

0 commit comments

Comments
 (0)