Skip to content

Commit

Permalink
test: add unit test to verify content-length behavior on modified req…
Browse files Browse the repository at this point in the history
…uests
  • Loading branch information
AtofStryker committed Mar 22, 2023
1 parent a8b0a31 commit 04f03d6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/net-stubbing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"build-prod": "tsc --project .",
"clean-deps": "rimraf node_modules",
"test": "CYPRESS_INTERNAL_ENV=test mocha -r @packages/ts/register --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json --exit test/unit/*",
"test": "CYPRESS_INTERNAL_ENV=test mocha -r @packages/ts/register --reporter mocha-multi-reporters --reporter-options configFile=../../mocha-reporter-config.json --exit test/unit/**/*",
"lint": "eslint --ext .ts,.json, ."
},
"dependencies": {
Expand Down
80 changes: 80 additions & 0 deletions packages/net-stubbing/test/unit/middleware/request-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { expect } from 'chai'
import sinon from 'sinon'
import { InterceptRequest } from '../../../lib/server/middleware/request'
import { state as NetStubbingState } from '../../../lib/server/state'

describe('request', () => {
context('InterceptedRequest', () => {
// @see https://github.com/cypress-io/cypress/issues/24407
it('does not set the content-length header if the header was not there to begin with on the original request', async () => {
const socket = {
toDriver: sinon.stub(),
}
const state = NetStubbingState()

const beforeRequestData = {
body: 'stubbed_body',
proxiedUrl: 'https://foobar.com',
url: 'https://foobar.com',
}

const afterRequestData = {
...beforeRequestData,
body: '',
headers: {},
}

// using a ES6 proxy to intercept the promise assignment on pendingEventHandlers.
// this way, we can resolve the event immediately
const pendingEventProxy = new Proxy(state.pendingEventHandlers, {
get (target, prop, receiver) {
// @ts-expect-error
return Reflect.get(...arguments)
},
set (obj, prop, value) {
// @ts-expect-error
const setProp = Reflect.set(...arguments)

// invoke the promise function immediately
if (typeof value === 'function') {
value({
changedData: afterRequestData,
stopPropagation: false,
})
}

return setProp
},
})

state.pendingEventHandlers = pendingEventProxy

const request = {
req: {
...beforeRequestData,
headers: {},
matchingRoutes: [
{
id: '1',
hasInterceptor: true,
routeMatcher: {},
},
],
pipe: sinon.stub(),
},
res: {
once: sinon.stub(),
},
socket,
debug: sinon.stub(),
netStubbingState: state,
next: sinon.stub(),
onError: sinon.stub(),
onResponse: sinon.stub(),
}

await InterceptRequest.call(request)
expect(request.req.headers['content-length']).to.be.undefined
})
})
})

0 comments on commit 04f03d6

Please sign in to comment.