@@ -84,12 +84,29 @@ describe("Credentials", () => {
8484 } ,
8585 ] ) ( "$description" , async ( { apiTokenIssuer, expectedUrl } ) => {
8686 const parsedUrl = new URL ( expectedUrl ) ;
87- // Use hostname instead of host to avoid port issues, then add port explicitly if non-default
88- const isDefaultPort = ( parsedUrl . protocol === "https:" && parsedUrl . port === "" ) ||
89- ( parsedUrl . protocol === "http:" && parsedUrl . port === "" ) ;
90- const baseUrl = isDefaultPort
91- ? `${ parsedUrl . protocol } //${ parsedUrl . hostname } `
92- : `${ parsedUrl . protocol } //${ parsedUrl . hostname } :${ parsedUrl . port } ` ;
87+
88+ // Nock requires explicit port matching in some environments
89+ // For default ports, try matching with explicit port included
90+ const defaultPort = parsedUrl . protocol === "https:" ? "443" : "80" ;
91+ const portToUse = parsedUrl . port || defaultPort ;
92+ const baseUrl = `${ parsedUrl . protocol } //${ parsedUrl . hostname } :${ portToUse } ` ;
93+
94+ // Extensive logging for CI debugging
95+ console . log ( "=== Token Refresh Test Debug Info ===" ) ;
96+ console . log ( "Input apiTokenIssuer:" , apiTokenIssuer ) ;
97+ console . log ( "Expected URL:" , expectedUrl ) ;
98+ console . log ( "Parsed URL details:" ) ;
99+ console . log ( " - protocol:" , parsedUrl . protocol ) ;
100+ console . log ( " - hostname:" , parsedUrl . hostname ) ;
101+ console . log ( " - host:" , parsedUrl . host ) ;
102+ console . log ( " - port:" , parsedUrl . port ) ;
103+ console . log ( " - pathname:" , parsedUrl . pathname ) ;
104+ console . log ( " - search:" , parsedUrl . search ) ;
105+ console . log ( "Nock setup:" ) ;
106+ console . log ( " - baseUrl:" , baseUrl ) ;
107+ console . log ( " - path:" , parsedUrl . pathname + parsedUrl . search ) ;
108+ console . log ( " - full mock URL:" , baseUrl + parsedUrl . pathname + parsedUrl . search ) ;
109+ console . log ( "Active nock interceptors:" , nock . activeMocks ( ) ) ;
93110
94111 const scope = nock ( baseUrl )
95112 . post ( parsedUrl . pathname + parsedUrl . search )
@@ -98,6 +115,8 @@ describe("Credentials", () => {
98115 expires_in : 300 ,
99116 } ) ;
100117
118+ console . log ( "Nock scope created, active mocks:" , nock . activeMocks ( ) ) ;
119+
101120 const credentials = new Credentials (
102121 {
103122 method : CredentialsMethod . ClientCredentials ,
@@ -112,7 +131,17 @@ describe("Credentials", () => {
112131 mockTelemetryConfig ,
113132 ) ;
114133
115- await credentials . getAccessTokenHeader ( ) ;
134+ try {
135+ await credentials . getAccessTokenHeader ( ) ;
136+ console . log ( "Request succeeded!" ) ;
137+ console . log ( "Scope done?" , scope . isDone ( ) ) ;
138+ console . log ( "Remaining active mocks:" , nock . activeMocks ( ) ) ;
139+ } catch ( error ) {
140+ console . error ( "Request failed with error:" , error ) ;
141+ console . error ( "Nock pending mocks:" , nock . pendingMocks ( ) ) ;
142+ console . error ( "Nock active mocks:" , nock . activeMocks ( ) ) ;
143+ throw error ;
144+ }
116145
117146 expect ( scope . isDone ( ) ) . toBe ( true ) ;
118147 } ) ;
@@ -167,18 +196,37 @@ describe("Credentials", () => {
167196 }
168197 ] ) ( "should normalize audience from apiTokenIssuer when using PrivateKeyJWT client credentials ($description)" , async ( { apiTokenIssuer, expectedUrl, expectedAudience } ) => {
169198 const parsedUrl = new URL ( expectedUrl ) ;
170- // Use hostname instead of host to avoid port issues, then add port explicitly if non-default
171- const isDefaultPort = ( parsedUrl . protocol === "https:" && parsedUrl . port === "" ) ||
172- ( parsedUrl . protocol === "http:" && parsedUrl . port === "" ) ;
173- const baseUrl = isDefaultPort
174- ? `${ parsedUrl . protocol } //${ parsedUrl . hostname } `
175- : `${ parsedUrl . protocol } //${ parsedUrl . hostname } :${ parsedUrl . port } ` ;
199+
200+ // Nock requires explicit port matching in some environments
201+ // For default ports, try matching with explicit port included
202+ const defaultPort = parsedUrl . protocol === "https:" ? "443" : "80" ;
203+ const portToUse = parsedUrl . port || defaultPort ;
204+ const baseUrl = `${ parsedUrl . protocol } //${ parsedUrl . hostname } :${ portToUse } ` ;
205+
206+ // Extensive logging for CI debugging
207+ console . log ( "=== PrivateKeyJWT Test Debug Info ===" ) ;
208+ console . log ( "Input apiTokenIssuer:" , apiTokenIssuer ) ;
209+ console . log ( "Expected URL:" , expectedUrl ) ;
210+ console . log ( "Expected audience:" , expectedAudience ) ;
211+ console . log ( "Parsed URL details:" ) ;
212+ console . log ( " - protocol:" , parsedUrl . protocol ) ;
213+ console . log ( " - hostname:" , parsedUrl . hostname ) ;
214+ console . log ( " - host:" , parsedUrl . host ) ;
215+ console . log ( " - port:" , parsedUrl . port ) ;
216+ console . log ( " - pathname:" , parsedUrl . pathname ) ;
217+ console . log ( "Nock setup:" ) ;
218+ console . log ( " - baseUrl:" , baseUrl ) ;
219+ console . log ( " - path:" , parsedUrl . pathname ) ;
220+ console . log ( " - full mock URL:" , baseUrl + parsedUrl . pathname ) ;
221+ console . log ( "Active nock interceptors:" , nock . activeMocks ( ) ) ;
176222
177223 const scope = nock ( baseUrl )
178224 . post ( parsedUrl . pathname , ( body : string ) => {
225+ console . log ( "Nock interceptor body matcher called with body:" , body ) ;
179226 const params = new URLSearchParams ( body ) ;
180227 const clientAssertion = params . get ( "client_assertion" ) as string ;
181228 const decoded = jose . decodeJwt ( clientAssertion ) ;
229+ console . log ( "Decoded JWT audience:" , decoded . aud ) ;
182230 expect ( decoded . aud ) . toBe ( `${ expectedAudience } ` ) ;
183231 return true ;
184232 } )
@@ -187,6 +235,8 @@ describe("Credentials", () => {
187235 expires_in : 300 ,
188236 } ) ;
189237
238+ console . log ( "Nock scope created, active mocks:" , nock . activeMocks ( ) ) ;
239+
190240 const credentials = new Credentials (
191241 {
192242 method : CredentialsMethod . ClientCredentials ,
@@ -201,7 +251,17 @@ describe("Credentials", () => {
201251 mockTelemetryConfig ,
202252 ) ;
203253
204- await credentials . getAccessTokenHeader ( ) ;
254+ try {
255+ await credentials . getAccessTokenHeader ( ) ;
256+ console . log ( "Request succeeded!" ) ;
257+ console . log ( "Scope done?" , scope . isDone ( ) ) ;
258+ console . log ( "Remaining active mocks:" , nock . activeMocks ( ) ) ;
259+ } catch ( error ) {
260+ console . error ( "Request failed with error:" , error ) ;
261+ console . error ( "Nock pending mocks:" , nock . pendingMocks ( ) ) ;
262+ console . error ( "Nock active mocks:" , nock . activeMocks ( ) ) ;
263+ throw error ;
264+ }
205265
206266 expect ( scope . isDone ( ) ) . toBe ( true ) ;
207267 } ) ;
0 commit comments