Skip to content

Commit

Permalink
fix cypress-in-cypress tests for chrome 119 and up
Browse files Browse the repository at this point in the history
  • Loading branch information
AtofStryker committed Apr 26, 2024
1 parent a6dde2e commit 324475e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/proxy/lib/http/response-middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,27 @@ const OmitProblematicHeaders: ResponseMiddleware = function () {
this.next()
}

const MaybeSetOriginAgentClusterHeader: ResponseMiddleware = function () {
if (process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT) {
const origin = new URL(this.req.proxiedUrl).origin

if (process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS && process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS === origin) {
// For cypress-in-cypress tests exclusively, we need to bucket all origin-agent-cluster requests
// from HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS to include Origin-Agent-Cluster=false. This has to due with changed
// behavior starting in Chrome 119. The new behavior works like the following:
// If a page did not request an origin-keyed agent cluster, chrome will place it in one
// anyway because a previous request went through without the Origin-Agent-Cluster=false header set.
// At time of writing, documentation detailing this behavior has not been found.

// To work around this, any request that matches the origin of HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS
// should set the Origin-Agent-Cluster=false header to avoid origin-keyed agent clusters for
this.res.setHeader('Origin-Agent-Cluster', '?0')
}
}

this.next()
}

const SetInjectionLevel: ResponseMiddleware = function () {
const span = telemetry.startSpan({ name: 'set:injection:level', parentSpan: this.resMiddlewareSpan, isVerbose })

Expand Down Expand Up @@ -933,6 +954,7 @@ export default {
InterceptResponse,
PatchExpressSetHeader,
OmitProblematicHeaders, // Since we might modify CSP headers, this middleware needs to come BEFORE SetInjectionLevel
MaybeSetOriginAgentClusterHeader, // NOTE: only used in cypress-in-cypress testing. this is otherwise a no-op
SetInjectionLevel,
MaybePreventCaching,
MaybeStripDocumentDomainFeaturePolicy,
Expand Down
60 changes: 60 additions & 0 deletions packages/proxy/test/unit/http/response-middleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('http/response-middleware', function () {
'InterceptResponse',
'PatchExpressSetHeader',
'OmitProblematicHeaders',
'MaybeSetOriginAgentClusterHeader',
'SetInjectionLevel',
'MaybePreventCaching',
'MaybeStripDocumentDomainFeaturePolicy',
Expand Down Expand Up @@ -448,6 +449,65 @@ describe('http/response-middleware', function () {
}
})

describe('MaybeSetOriginAgentClusterHeader', function () {
const { MaybeSetOriginAgentClusterHeader } = ResponseMiddleware

let CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT
let PREVIOUS_HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS
let ctx

beforeEach(function () {
CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT = process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT
PREVIOUS_HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS = process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS
ctx = {
req: {
proxiedUrl: 'http://localhost:4455',
},
res: {
setHeader: sinon.stub(),
on: (event, listener) => {},
off: (event, listener) => {},
},
}
})

this.afterEach(function () {
beforeEach(function () {
process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT = CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT
process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS = PREVIOUS_HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS
})
})

it('doesn\'t set the Origin-Agent-Cluster for the request if cypress-in-cypress testing is off', function () {
delete process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT

return testMiddleware([MaybeSetOriginAgentClusterHeader], ctx)
.then(() => {
expect(ctx.res.setHeader).not.to.be.called
})
})

it('doesn\'t set the Origin-Agent-Cluster for the request if cypress-in-cypress testing is on but url does NOT match http proxy', function () {
process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT = '1'
process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS = 'http://localhost:4456'

return testMiddleware([MaybeSetOriginAgentClusterHeader], ctx)
.then(() => {
expect(ctx.res.setHeader).not.to.be.called
})
})

it('sets the Origin-Agent-Cluster for the request if cypress-in-cypress testing is on and url matches http proxy', function () {
process.env.CYPRESS_INTERNAL_E2E_TESTING_SELF_PARENT_PROJECT = '1'
process.env.HTTP_PROXY_TARGET_FOR_ORIGIN_REQUESTS = 'http://localhost:4455'

return testMiddleware([MaybeSetOriginAgentClusterHeader], ctx)
.then(() => {
expect(ctx.res.setHeader).to.be.calledWith('Origin-Agent-Cluster', '?0')
})
})
})

describe('SetInjectionLevel', function () {
const { SetInjectionLevel } = ResponseMiddleware
let ctx
Expand Down

0 comments on commit 324475e

Please sign in to comment.