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: "cannot access variable before initialization" errors in route2 #8978

Merged
merged 2 commits into from
Oct 29, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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