-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add unit test to verify content-length behavior on modified req…
…uests
- Loading branch information
1 parent
a8b0a31
commit 04f03d6
Showing
2 changed files
with
81 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
packages/net-stubbing/test/unit/middleware/request-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) | ||
}) |