Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that requests to the same resource will be properly intercepted even if they are sent in rapid succession #30435

Merged
merged 4 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
ryanthemanuel marked this conversation as resolved.
Show resolved Hide resolved

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)
})
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need a done handler here or something similar in the case the fake for some reason is not called and the test passes when it shouldn't?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You can't mix and match promises and done, but I figured a better way to ensure this is getting called: 339a450 (#30435)


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

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