File tree Expand file tree Collapse file tree
cypress/integration/cypress Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -181,15 +181,22 @@ describe('Proxy Logging', () => {
181181 }
182182 } )
183183
184- const oldOnload = cy . state ( 'server' ) . options . onLoad
184+ const xhr = new win . XMLHttpRequest ( )
185185
186- cy . stub ( cy . state ( 'server' ) . options , 'onLoad' ) . log ( false ) . callsFake ( function ( ...args ) {
187- setTimeout ( ( ) => {
188- oldOnload . call ( this , ...args )
189- } , 500 )
186+ const logIncomingRequest = Cypress . ProxyLogging . logIncomingRequest
187+ const updateRequestWithResponse = Cypress . ProxyLogging . updateRequestWithResponse
188+
189+ // To simulate the xhr call landing second, we send updateRequestWithResponse immediately after
190+ // the call is intercepted
191+ cy . stub ( Cypress . ProxyLogging , 'logIncomingRequest' ) . log ( false ) . callsFake ( function ( ...args ) {
192+ logIncomingRequest . call ( this , ...args )
193+ updateRequestWithResponse . call ( this , {
194+ requestId : args [ 0 ] . requestId ,
195+ status : 404 ,
196+ } )
190197 } )
191198
192- const xhr = new win . XMLHttpRequest ( )
199+ cy . stub ( Cypress . ProxyLogging , 'updateRequestWithResponse' ) . log ( false ) . callsFake ( function ( ) { } )
193200
194201 xhr . open ( 'GET' , '/some-url' )
195202 xhr . send ( )
Original file line number Diff line number Diff line change @@ -324,13 +324,7 @@ export default class ProxyLogging {
324324 return proxyRequest
325325 }
326326
327- private updateRequestWithResponse ( responseReceived : BrowserResponseReceived ) : void {
328- const proxyRequest = _ . find ( this . proxyRequests , ( { preRequest } ) => preRequest . requestId === responseReceived . requestId )
329-
330- if ( ! proxyRequest ) {
331- return debug ( 'unmatched responseReceived event %o' , responseReceived )
332- }
333-
327+ private updateProxyRequestWithResponse ( proxyRequest , responseReceived ) {
334328 proxyRequest . responseReceived = responseReceived
335329
336330 proxyRequest . updateConsoleProps ( )
@@ -343,6 +337,22 @@ export default class ProxyLogging {
343337 proxyRequest . log ?. end ( )
344338 }
345339
340+ private updateRequestWithResponse ( responseReceived : BrowserResponseReceived ) : void {
341+ const proxyRequest = _ . find ( this . proxyRequests , ( { preRequest } ) => preRequest . requestId === responseReceived . requestId )
342+
343+ if ( ! proxyRequest ) {
344+ return debug ( 'unmatched responseReceived event %o' , responseReceived )
345+ }
346+
347+ if ( proxyRequest . xhr && proxyRequest . xhr . xhr . readyState !== XMLHttpRequest . DONE ) {
348+ proxyRequest . xhr . xhr . addEventListener ( 'load' , ( ) => {
349+ this . updateProxyRequestWithResponse ( proxyRequest , responseReceived )
350+ } )
351+ } else {
352+ this . updateProxyRequestWithResponse ( proxyRequest , responseReceived )
353+ }
354+ }
355+
346356 private updateRequestWithError ( error : RequestError ) : void {
347357 const proxyRequest = _ . find ( this . proxyRequests , ( { preRequest } ) => preRequest . requestId === error . requestId )
348358
You can’t perform that action at this time.
0 commit comments