Skip to content

Commit

Permalink
fix: ensure that requests to the same resource will be properly inter…
Browse files Browse the repository at this point in the history
…cepted even if they are sent in rapid succession (#30435)

* fix: ensure that requests to the same resource will be properly intercepted even if they are sent in rapid succession - run ci

* add changelog

* Update packages/net-stubbing/lib/server/middleware/request.ts

* PR comments
  • Loading branch information
ryanthemanuel authored Oct 22, 2024
1 parent aafac6a commit 4ba72c9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ _Released 10/1/2024 (PENDING)_
**Bugfixes:**

- Patched [find-process](https://github.com/yibn2008/find-process) to fix an issue where trying to clean up browser profiles can throw an error on Windows. Addresses [#30378](https://github.com/cypress-io/cypress/issues/30378).
- Fixed an issue where requests to the same resource in rapid succession may not have the appropriate static response intercept applied if there are multiple intercepts that apply for that resource. Addresses [#30375](https://github.com/cypress-io/cypress/issues/30375).

**Misc:**

Expand Down
8 changes: 5 additions & 3 deletions packages/net-stubbing/lib/server/intercepted-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ export class InterceptedRequest {
this._onResponse = opts.onResponse
this.state = opts.state
this.socket = opts.socket

this.addDefaultSubscriptions()
}

onResponse = (incomingRes: IncomingMessage, resStream: Readable) => {
Expand All @@ -65,7 +63,7 @@ export class InterceptedRequest {
this._onResponse(incomingRes, resStream)
}

private addDefaultSubscriptions () {
addDefaultSubscriptions () {
if (this.subscriptionsByRoute.length) {
throw new Error('cannot add default subscriptions to non-empty array')
}
Expand All @@ -75,6 +73,10 @@ export class InterceptedRequest {
}

for (const route of this.req.matchingRoutes) {
if (route.disabled) {
continue
}

const subscriptionsByRoute = {
routeId: route.id,
immediateStaticResponse: route.staticResponse,
Expand Down
5 changes: 5 additions & 0 deletions packages/net-stubbing/lib/server/middleware/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ export const InterceptRequest: RequestMiddleware = async function () {

await ensureBody()

// Note that this needs to happen after the `ensureBody` call to ensure that we proceed synchronously to
// where we update the state of the routes:
// https://github.com/cypress-io/cypress/blob/aafac6a6104b689a118f4c4f29f948d7d8a35aef/packages/net-stubbing/lib/server/intercepted-request.ts#L167-L169
request.addDefaultSubscriptions()

if (!_.isString(req.body) && !_.isBuffer(req.body)) {
throw new Error('req.body must be a string or a Buffer')
}
Expand Down
63 changes: 62 additions & 1 deletion packages/net-stubbing/test/unit/intercepted-request-spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { expect } from 'chai'
import chai, { expect } from 'chai'
import _ from 'lodash'
import sinon from 'sinon'
import sinonChai from 'sinon-chai'
import { InterceptedRequest } from '../../lib/server/intercepted-request'
import { state as NetStubbingState } from '../../lib/server/state'

chai.use(sinonChai)

describe('InterceptedRequest', () => {
context('handleSubscriptions', () => {
it('handles subscriptions as expected', async () => {
Expand Down Expand Up @@ -32,6 +35,8 @@ describe('InterceptedRequest', () => {
socket,
})

interceptedRequest.addDefaultSubscriptions()

interceptedRequest.addSubscription({
routeId: '1',
eventName: 'before:response',
Expand Down Expand Up @@ -59,6 +64,62 @@ describe('InterceptedRequest', () => {
data,
mergeChanges: _.merge,
})

expect(socket.toDriver).to.be.calledTwice
})

it('ignores disabled subscriptions', async () => {
const socket = {
toDriver: sinon.stub(),
}
const state = NetStubbingState()
const interceptedRequest = new InterceptedRequest({
req: {
matchingRoutes: [
// @ts-ignore
{
id: '1',
hasInterceptor: true,
routeMatcher: {},
disabled: true,
},
// @ts-ignore
{
id: '2',
hasInterceptor: true,
routeMatcher: {},
},
],
},
state,
socket,
})

interceptedRequest.addDefaultSubscriptions()

const data = { foo: 'bar' }

socket.toDriver.callsFake((eventName, subEventName, frame) => {
expect(eventName).to.eq('net:stubbing:event')
expect(subEventName).to.eq('before:request')
expect(frame).to.deep.include({
subscription: {
eventName: 'before:request',
await: true,
routeId: frame.subscription.routeId,
},
})

state.pendingEventHandlers[frame.eventId](frame.data)
})

await interceptedRequest.handleSubscriptions({
eventName: 'before:request',
data,
mergeChanges: _.merge,
})

expect(socket.toDriver).to.be.calledOnce
})
})
})

5 comments on commit 4ba72c9

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4ba72c9 Oct 22, 2024

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.15.1/linux-arm64/develop-4ba72c94aab55d96486d7b3e53d1087a77136b6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4ba72c9 Oct 22, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.15.1/linux-x64/develop-4ba72c94aab55d96486d7b3e53d1087a77136b6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4ba72c9 Oct 22, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.15.1/win32-x64/develop-4ba72c94aab55d96486d7b3e53d1087a77136b6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4ba72c9 Oct 22, 2024

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 arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.15.1/darwin-arm64/develop-4ba72c94aab55d96486d7b3e53d1087a77136b6d/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 4ba72c9 Oct 22, 2024

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 build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/13.15.1/darwin-x64/develop-4ba72c94aab55d96486d7b3e53d1087a77136b6d/cypress.tgz

Please sign in to comment.