Skip to content

Commit

Permalink
Differentiate before:response and response
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Mar 23, 2021
1 parent 0664022 commit 042050c
Show file tree
Hide file tree
Showing 10 changed files with 274 additions and 220 deletions.
46 changes: 44 additions & 2 deletions packages/driver/cypress/integration/commands/net_stubbing_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
e.push('mware req handler')
req.on('before:response', (res) => e.push('mware before:response'))
req.on('response', (res) => e.push('mware response'))
req.on('after:response', (res) => e.push('mware after:response'))
})
.intercept('/dump-headers', (req) => {
e.push('normal req handler')
Expand All @@ -285,10 +286,11 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
})
.wrap(e).should('have.all.members', [
'mware req handler',
'mware before:response',
'mware response',
'normal req handler',
'mware before:response',
'normal res handler',
'mware response',
'mware after:response',
])
})

Expand Down Expand Up @@ -1309,6 +1311,46 @@ describe('network stubbing', { retries: { runMode: 2, openMode: 0 } }, function
})
})

context('can end response', () => {
for (const eventName of ['before:response', 'response']) {
it(`in \`${eventName}\``, () => {
const expectBeforeResponse = eventName === 'response'
let beforeResponseCalled = false

cy.intercept('/foo', (req) => {
req.on('response', (res) => {
throw new Error('response should not be reached')
})

req.on('before:response', (res) => {
beforeResponseCalled = true

if (!expectBeforeResponse) {
throw new Error('before:response should not be reached')
}
})
}).as('first')
.intercept('/foo', (req) => {
// @ts-ignore
req.on(eventName, (res) => {
res.send({
statusCode: 200,
fixture: 'valid.json',
})
})
}).as('second')
.then(() => {
return $.getJSON('/foo')
})
.should('include', { 'foo': 1 })
.wait('@first').wait('@second')
.then(() => {
expect(beforeResponseCalled).to.eq(expectBeforeResponse)
})
})
}
})

context('errors', function () {
it('when unknown eventName is passed', function (done) {
cy.on('fail', (err) => {
Expand Down
19 changes: 19 additions & 0 deletions packages/driver/src/cy/net-stubbing/events/after-response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { CyHttpMessages } from '@packages/net-stubbing/lib/types'
import { HandlerFn } from '.'

export const onAfterResponse: HandlerFn<CyHttpMessages.ResponseComplete> = async (Cypress, frame, userHandler, { getRequest, getRoute }) => {
const request = getRequest(frame.subscription.routeId, frame.requestId)

if (!request) {
return null
}

request.state = 'Complete'

request.log.fireChangeEvent()
request.log.end()

userHandler && await userHandler(request.response!)

return null
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const debug = Debug('cypress:driver:net-stubbing:events:before-request')

type Result = HandlerResult<CyHttpMessages.IncomingRequest>

const validEvents = ['before:response', 'response', 'error']
const validEvents = ['before:response', 'response', 'after:response', 'error']

export const onBeforeRequest: HandlerFn<CyHttpMessages.IncomingRequest> = (Cypress, frame, userHandler, { getRoute, getRequest, emitNetEvent, sendStaticResponse }) => {
function getRequestLog (route: Route, request: Omit<Interception, 'log'>) {
Expand Down
165 changes: 0 additions & 165 deletions packages/driver/src/cy/net-stubbing/events/before-response.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/driver/src/cy/net-stubbing/events/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Route, Interception, StaticResponse, NetEvent } from '../types'
import { onBeforeRequest } from './before-request'
import { onBeforeResponse } from './before-response'
import { onResponse } from './response'
import { onAfterResponse } from './after-response'
import { onError } from './error'
import Bluebird from 'bluebird'
import { getBackendStaticResponse } from '../static-response-utils'
Expand All @@ -20,8 +20,9 @@ export type HandlerFn<D> = (Cypress: Cypress.Cypress, frame: NetEvent.ToDriver.E

const netEventHandlers: { [eventName: string]: HandlerFn<any> } = {
'before:request': onBeforeRequest,
'before:response': onBeforeResponse,
'before:response': onResponse,
'response': onResponse,
'after:response': onAfterResponse,
'error': onError,
}

Expand Down
Loading

0 comments on commit 042050c

Please sign in to comment.