Skip to content

Commit

Permalink
Merge branch 'main' into Michael/support-fetch-content-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 16, 2024
2 parents 7dc8362 + 2067cb0 commit cdfdc95
Show file tree
Hide file tree
Showing 26 changed files with 922 additions and 187 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@mswjs/interceptors",
"description": "Low-level HTTP/HTTPS/XHR/fetch request interception library.",
"version": "0.35.0",
"version": "0.36.4",
"main": "./lib/node/index.js",
"module": "./lib/node/index.mjs",
"types": "./lib/node/index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions src/interceptors/ClientRequest/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ export class MockAgent extends http.Agent {

const socket = new MockHttpSocket({
connectionOptions: options,
createConnection: createConnection.bind(this, options, callback),
createConnection: createConnection.bind(
this.customAgent || this,
options,
callback
),
onRequest: this.onRequest.bind(this),
onResponse: this.onResponse.bind(this),
})
Expand Down Expand Up @@ -68,7 +72,11 @@ export class MockHttpsAgent extends https.Agent {

const socket = new MockHttpSocket({
connectionOptions: options,
createConnection: createConnection.bind(this, options, callback),
createConnection: createConnection.bind(
this.customAgent || this,
options,
callback
),
onRequest: this.onRequest.bind(this),
onResponse: this.onResponse.bind(this),
})
Expand Down
62 changes: 62 additions & 0 deletions src/interceptors/ClientRequest/utils/recordRawHeaders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,31 @@ it('records raw headers (Request / Headers as init)', () => {
expect(getRawFetchHeaders(request.headers)).toEqual([['X-My-Header', '1']])
})

it('records raw headers (Reqest / Request as init)', () => {
recordRawFetchHeaders()
const init = new Request(url, { headers: [['X-My-Header', '1']] })
const request = new Request(init)

expect(getRawFetchHeaders(request.headers)).toEqual([['X-My-Header', '1']])
})

it('records raw headers (Request / Request+Headers as init)', () => {
recordRawFetchHeaders()
const init = new Request(url, { headers: [['X-My-Header', '1']] })
expect(getRawFetchHeaders(init.headers)).toEqual([['X-My-Header', '1']])

const request = new Request(init, {
headers: new Headers([['X-Another-Header', '2']]),
})

// Must merge the raw headers from the request init
// and the request instance itself.
expect(getRawFetchHeaders(request.headers)).toEqual([
['X-My-Header', '1'],
['X-Another-Header', '2'],
])
})

it('records raw headers (Response / object as init)', () => {
recordRawFetchHeaders()
const response = new Response(null, {
Expand Down Expand Up @@ -157,3 +182,40 @@ it('stops recording once the patches are restored', () => {
// Must return the normalized headers (no access to raw headers).
expect(getRawFetchHeaders(headers)).toEqual([['x-my-header', '1']])
})

it('overrides an existing header when calling ".set()"', () => {
recordRawFetchHeaders()
const headers = new Headers([['a', '1']])
expect(headers.get('a')).toBe('1')

headers.set('a', '2')
expect(headers.get('a')).toBe('2')

const headersClone = new Headers(headers)
expect(headersClone.get('a')).toBe('2')
})

it('overrides an existing multi-value header when calling ".set()"', () => {
recordRawFetchHeaders()
const headers = new Headers([
['a', '1'],
['a', '2'],
])
expect(headers.get('a')).toBe('1, 2')

headers.set('a', '3')
expect(headers.get('a')).toBe('3')
})

it('does not throw on using Headers before recording', () => {
// If the consumer constructs a Headers instance before
// the interceptor is enabled, it will have no internal symbol set.
const headers = new Headers()
recordRawFetchHeaders()
const request = new Request(url, { headers })

expect(getRawFetchHeaders(request.headers)).toEqual([])

request.headers.set('X-My-Header', '1')
expect(getRawFetchHeaders(request.headers)).toEqual([['X-My-Header', '1']])
})
Loading

0 comments on commit cdfdc95

Please sign in to comment.