From c42667e707c89aeaf9a46cc3afa3d9b3f2c2422d Mon Sep 17 00:00:00 2001 From: Zach Bloomquist Date: Thu, 3 Dec 2020 11:16:33 -0500 Subject: [PATCH] fix(net-stubbing): do not fireChangeEvent for non-existing routes (#9425) --- .../integration/commands/net_stubbing_spec.ts | 54 +++++++++++++++++++ .../net-stubbing/events/request-received.ts | 9 ++-- 2 files changed, 58 insertions(+), 5 deletions(-) diff --git a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts index a06dac019245..b777e761a517 100644 --- a/packages/driver/cypress/integration/commands/net_stubbing_spec.ts +++ b/packages/driver/cypress/integration/commands/net_stubbing_spec.ts @@ -472,6 +472,60 @@ describe('network stubbing', { retries: 2 }, function () { }) }) + context('events', function () { + // @see https://github.com/cypress-io/cypress/issues/9170 + it('gracefully handles request received without a known route', function () { + cy.intercept('/valid.json', (req) => { + req.reply({ bad: 'should not be received' }) + }) + .then(() => { + const routeIds = _.keys(state('routes')) + + // delete the driver-side route - the server-side route will still exist and cause an event + // to be emitted to the driver + delete state('routes')[routeIds[0]] + expect(state('routes')).to.deep.eq({}) + + return $.get('/fixtures/valid.json') + }) + .then((body) => { + expect(body).to.include({ foo: 1 }) + }) + }) + + it('gracefully handles response received without a known route', function () { + cy.intercept('/valid.json', (req) => { + state('routes', {}) + + req.reply((res) => { + res.send({ bad: 'should not be received' }) + }) + }) + .then(() => { + return $.get('/fixtures/valid.json') + }) + .then((body) => { + expect(body).to.include({ foo: 1 }) + }) + }) + + it('gracefully handles response completed without a known route', function () { + cy.intercept('/valid.json', (req) => { + req.reply((res) => { + state('routes', {}) + + res.send({ bar: 2 }) + }) + }) + .then(() => { + return $.get('/fixtures/valid.json') + }) + .then((body) => { + expect(body).to.include({ bar: 2 }) + }) + }) + }) + context('network handling', function () { // @see https://github.com/cypress-io/cypress/issues/8497 it('can load transfer-encoding: chunked redirects', function () { diff --git a/packages/driver/src/cy/net-stubbing/events/request-received.ts b/packages/driver/src/cy/net-stubbing/events/request-received.ts index 7a71328ad85b..001e7301246e 100644 --- a/packages/driver/src/cy/net-stubbing/events/request-received.ts +++ b/packages/driver/src/cy/net-stubbing/events/request-received.ts @@ -129,10 +129,6 @@ export const onRequestReceived: HandlerFn = continueSent = true - if (request) { - request.state = 'Intercepted' - } - if (continueFrame) { // copy changeable attributes of userReq to req in frame // @ts-ignore @@ -149,7 +145,10 @@ export const onRequestReceived: HandlerFn = emitNetEvent('http:request:continue', continueFrame) } - request.log.fireChangeEvent() + if (request) { + request.state = 'Intercepted' + request.log && request.log.fireChangeEvent() + } } if (!route) {