Skip to content

Commit

Permalink
fix: prevent stubbed cy.intercepts from reaching server (#15942)
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig authored Apr 12, 2021
1 parent 86ffe20 commit dde2f22
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
16 changes: 16 additions & 0 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,22 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
}).wait('@getFoo').its('request.url').should('include', '/foo')
})

// @see https://github.com/cypress-io/cypress/issues/15841
it('prevents requests from reaching destination server', function () {
const v = String(Date.now())

// this test creates server-side state via /set-var to test if requests are being sent or not
cy.then(async () => {
await $.get(`/set-var?v=${v}`)
expect(await $.get('/get-var')).to.eq(v)
})
.intercept('/set-var*', { statusCode: 200, body: 'else' })
.then(async () => {
await $.get(`/set-var?v=something`)
expect(await $.get('/get-var')).to.eq(v)
})
})

// @see https://github.com/cypress-io/cypress/issues/8532
context('can stub a response with an empty array', function () {
const assertEmptyArray = (done) => {
Expand Down
11 changes: 11 additions & 0 deletions packages/driver/cypress/plugins/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,17 @@ const createApp = (port) => {
.send('<html><body>server error</body></html>')
})

let _var = ''

app.get('/set-var', (req, res) => {
_var = req.query.v
res.sendStatus(200)
})

app.get('/get-var', (req, res) => {
res.send(_var)
})

app.use(express.static(path.join(__dirname, '..')))

app.use(require('errorhandler')())
Expand Down
16 changes: 13 additions & 3 deletions packages/net-stubbing/lib/server/intercepted-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class InterceptedRequest {
subscriptions: Subscription[]
}> = []
includeBodyInAfterResponse: boolean = false
responseSent: boolean = false
lastEvent?: string
onError: (err: Error) => void
/**
Expand All @@ -31,14 +32,13 @@ export class InterceptedRequest {
/**
* Finish the current request with a response.
*/
onResponse: (incomingRes: IncomingMessage, resStream: Readable) => void
_onResponse: (incomingRes: IncomingMessage, resStream: Readable) => void
/**
* A callback that can be used to send the response through the rest of the response proxy steps.
*/
continueResponse?: (newResStream?: Readable) => void
req: CypressIncomingRequest
res: CypressOutgoingResponse
incomingRes?: IncomingMessage
matchingRoutes: BackendRoute[]
state: NetStubbingState
socket: CyServer.Socket
Expand All @@ -49,14 +49,24 @@ export class InterceptedRequest {
this.res = opts.res
this.continueRequest = opts.continueRequest
this.onError = opts.onError
this.onResponse = opts.onResponse
this._onResponse = opts.onResponse
this.matchingRoutes = opts.matchingRoutes
this.state = opts.state
this.socket = opts.socket

this.addDefaultSubscriptions()
}

onResponse = (incomingRes: IncomingMessage, resStream: Readable) => {
if (this.responseSent) {
throw new Error('onResponse cannot be called twice')
}

this.responseSent = true

this._onResponse(incomingRes, resStream)
}

private addDefaultSubscriptions () {
if (this.subscriptionsByRoute.length) {
throw new Error('cannot add default subscriptions to non-empty array')
Expand Down
6 changes: 6 additions & 0 deletions packages/net-stubbing/lib/server/middleware/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,11 @@ export const InterceptRequest: RequestMiddleware = async function () {
// @ts-ignore
mergeChanges(request.req, req)

if (request.responseSent) {
// request has been fulfilled with a response already, do not send the request outgoing
// @see https://github.com/cypress-io/cypress/issues/15841
return this.end()
}

return request.continueRequest()
}
2 changes: 0 additions & 2 deletions packages/net-stubbing/lib/server/middleware/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ export const InterceptResponse: ResponseMiddleware = async function () {
return this.next()
}

request.incomingRes = this.incomingRes

request.onResponse = (incomingRes, resStream) => {
this.incomingRes = incomingRes

Expand Down

4 comments on commit dde2f22

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on dde2f22 Apr 12, 2021

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/7.0.2/circle-develop-dde2f226350d1f0e09ba3e7ddde7cc931d81b03f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on dde2f22 Apr 12, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 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/7.0.2/appveyor-develop-dde2f226350d1f0e09ba3e7ddde7cc931d81b03f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on dde2f22 Apr 12, 2021

Choose a reason for hiding this comment

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

AppVeyor 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/7.0.2/appveyor-develop-dde2f226350d1f0e09ba3e7ddde7cc931d81b03f/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on dde2f22 Apr 12, 2021

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/7.0.2/circle-develop-dde2f226350d1f0e09ba3e7ddde7cc931d81b03f/cypress.tgz

Please sign in to comment.