Skip to content

Commit

Permalink
fix: ensure that proxy logs are updated after the xhr has actually co…
Browse files Browse the repository at this point in the history
…mpleted (#21373)

Co-authored-by: Emily Rohrbough <emilyrohrbough@users.noreply.github.com>
  • Loading branch information
ryanthemanuel and emilyrohrbough committed May 9, 2022
1 parent 331e541 commit 0d958dc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 13 additions & 6 deletions packages/driver/cypress/integration/cypress/proxy-logging_spec.ts
Expand Up @@ -181,15 +181,22 @@ describe('Proxy Logging', () => {
}
})

const oldOnload = cy.state('server').options.onLoad
const xhr = new win.XMLHttpRequest()

cy.stub(cy.state('server').options, 'onLoad').log(false).callsFake(function (...args) {
setTimeout(() => {
oldOnload.call(this, ...args)
}, 500)
const logIncomingRequest = Cypress.ProxyLogging.logIncomingRequest
const updateRequestWithResponse = Cypress.ProxyLogging.updateRequestWithResponse

// To simulate the xhr call landing second, we send updateRequestWithResponse immediately after
// the call is intercepted
cy.stub(Cypress.ProxyLogging, 'logIncomingRequest').log(false).callsFake(function (...args) {
logIncomingRequest.call(this, ...args)
updateRequestWithResponse.call(this, {
requestId: args[0].requestId,
status: 404,
})
})

const xhr = new win.XMLHttpRequest()
cy.stub(Cypress.ProxyLogging, 'updateRequestWithResponse').log(false).callsFake(function () {})

xhr.open('GET', '/some-url')
xhr.send()
Expand Down
24 changes: 17 additions & 7 deletions packages/driver/src/cypress/proxy-logging.ts
Expand Up @@ -324,13 +324,7 @@ export default class ProxyLogging {
return proxyRequest
}

private updateRequestWithResponse (responseReceived: BrowserResponseReceived): void {
const proxyRequest = _.find(this.proxyRequests, ({ preRequest }) => preRequest.requestId === responseReceived.requestId)

if (!proxyRequest) {
return debug('unmatched responseReceived event %o', responseReceived)
}

private updateProxyRequestWithResponse (proxyRequest, responseReceived) {
proxyRequest.responseReceived = responseReceived

proxyRequest.updateConsoleProps()
Expand All @@ -343,6 +337,22 @@ export default class ProxyLogging {
proxyRequest.log?.end()
}

private updateRequestWithResponse (responseReceived: BrowserResponseReceived): void {
const proxyRequest = _.find(this.proxyRequests, ({ preRequest }) => preRequest.requestId === responseReceived.requestId)

if (!proxyRequest) {
return debug('unmatched responseReceived event %o', responseReceived)
}

if (proxyRequest.xhr && proxyRequest.xhr.xhr.readyState !== XMLHttpRequest.DONE) {
proxyRequest.xhr.xhr.addEventListener('load', () => {
this.updateProxyRequestWithResponse(proxyRequest, responseReceived)
})
} else {
this.updateProxyRequestWithResponse(proxyRequest, responseReceived)
}
}

private updateRequestWithError (error: RequestError): void {
const proxyRequest = _.find(this.proxyRequests, ({ preRequest }) => preRequest.requestId === error.requestId)

Expand Down

3 comments on commit 0d958dc

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0d958dc May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.6.1/linux-x64/develop-0d958dc88d6be3128549631ea36abcf792725c67/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0d958dc May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.6.1/win32-x64/develop-0d958dc88d6be3128549631ea36abcf792725c67/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 0d958dc May 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Circle has built the darwin x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/9.6.1/darwin-x64/develop-0d958dc88d6be3128549631ea36abcf792725c67/cypress.tgz

Please sign in to comment.