Skip to content

Commit

Permalink
fix: "cannot access variable before initialization" errors in route2 (#…
Browse files Browse the repository at this point in the history
…8978)

Co-authored-by: Jennifer Shehane <jennifer@cypress.io>
  • Loading branch information
sainthkh and jennifer-shehane committed Oct 29, 2020
1 parent 18f4d9a commit 756b6b8
Showing 1 changed file with 42 additions and 41 deletions.
83 changes: 42 additions & 41 deletions packages/driver/src/cy/net-stubbing/events/request-received.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,60 +47,19 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
const route = getRoute(frame.routeHandlerId)
const { req, requestId, routeHandlerId } = frame

const sendContinueFrame = () => {
if (continueSent) {
throw new Error('sendContinueFrame called twice in handler')
}

continueSent = true

if (request) {
request.state = 'Intercepted'
}

if (continueFrame) {
// copy changeable attributes of userReq to req in frame
// @ts-ignore
continueFrame.req = {
..._.pick(userReq, SERIALIZABLE_REQ_PROPS),
}

_.merge(request.request, continueFrame.req)

emitNetEvent('http:request:continue', continueFrame)
}
}

if (!route) {
return sendContinueFrame()
}

const request: Partial<Request> = {
id: requestId,
request: req,
state: 'Received',
}

request.log = getRequestLog(route, request as Omit<Request, 'log'>)

// TODO: this misnomer is a holdover from XHR, should be numRequests
route.log.set('numResponses', (route.log.get('numResponses') || 0) + 1)
route.requests[requestId] = request as Request

if (frame.notificationOnly) {
return
}

const continueFrame: Partial<NetEventFrames.HttpRequestContinue> = {
routeHandlerId,
requestId,
}

let resolved = false
let replyCalled = false
let continueSent = false

route.hitCount++

const userReq: CyHttpMessages.IncomingHttpRequest = {
...req,
Expand Down Expand Up @@ -154,6 +113,48 @@ export const onRequestReceived: HandlerFn<NetEventFrames.HttpRequestReceived> =
},
}

let continueSent = false

const sendContinueFrame = () => {
if (continueSent) {
throw new Error('sendContinueFrame called twice in handler')
}

continueSent = true

if (request) {
request.state = 'Intercepted'
}

if (continueFrame) {
// copy changeable attributes of userReq to req in frame
// @ts-ignore
continueFrame.req = {
..._.pick(userReq, SERIALIZABLE_REQ_PROPS),
}

_.merge(request.request, continueFrame.req)

emitNetEvent('http:request:continue', continueFrame)
}
}

if (!route) {
return sendContinueFrame()
}

request.log = getRequestLog(route, request as Omit<Request, 'log'>)

// TODO: this misnomer is a holdover from XHR, should be numRequests
route.log.set('numResponses', (route.log.get('numResponses') || 0) + 1)
route.requests[requestId] = request as Request

if (frame.notificationOnly) {
return
}

route.hitCount++

if (!_.isFunction(route.handler)) {
return sendContinueFrame()
}
Expand Down

3 comments on commit 756b6b8

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 756b6b8 Oct 29, 2020

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/5.5.1/circle-develop-756b6b8b0ec4abbce3a938e083a5f94f2fecbc68/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 756b6b8 Oct 29, 2020

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/5.5.1/appveyor-develop-756b6b8b0ec4abbce3a938e083a5f94f2fecbc68/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on 756b6b8 Oct 29, 2020

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/5.5.1/appveyor-develop-756b6b8b0ec4abbce3a938e083a5f94f2fecbc68/cypress.tgz

Please sign in to comment.