Skip to content

Commit

Permalink
fix: support addRequest from custom http agents
Browse files Browse the repository at this point in the history
  • Loading branch information
kettanaito committed Oct 30, 2024
1 parent 2ec79d7 commit ceb649a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
13 changes: 13 additions & 0 deletions src/interceptors/ClientRequest/agents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
declare module 'node:http' {
interface Agent {
createConnection(options: any, callback: any): net.Socket
addRequest(request: http.ClientRequest, options: http.RequestOptions): void
}
}

Expand Down Expand Up @@ -50,6 +51,18 @@ export class MockAgent extends http.Agent {

return socket
}

addRequest(request: http.ClientRequest, options: http.RequestOptions) {
// First, execute the base class method.
super.addRequest.apply(this, [request, options])

// If there's a custom HTTP agent, call its `addRequest` method.
// This way, if the agent has side effects that affect the request,
// those will be applied to the intercepted request instance as well.
if (this.customAgent instanceof http.Agent) {
this.customAgent.addRequest(request, options)
}
}
}

export class MockHttpsAgent extends https.Agent {
Expand Down
56 changes: 56 additions & 0 deletions test/modules/http/regressions/http-agent-add-request.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @see https://github.com/mswjs/msw/issues/2338
*/
// @vitest-environment node
import http from 'node:http'
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
import { waitForClientRequest } from '../../../helpers'
import { DeferredPromise } from '@open-draft/deferred-promise'

const interceptor = new ClientRequestInterceptor()

beforeAll(() => {
interceptor.apply()
})

afterEach(() => {
interceptor.removeAllListeners()
})

afterAll(() => {
interceptor.dispose()
})

/**
* A custom HTTP agent that adds a "cookie" header to
* any outgoing request.
*/
class CustomAgent extends http.Agent {
addRequest(request: http.ClientRequest) {
request.setHeader('cookie', 'key=value')
}
}

it('respects a custom "addRequest" method on the http agent', async () => {
const interceptedRequestPromise = new DeferredPromise<Request>()

interceptor.on('request', ({ request, controller }) => {
interceptedRequestPromise.resolve(request)
controller.respondWith(new Response())
})

const request = http.get('http://localhost/resource', {
agent: new CustomAgent(),
})
await waitForClientRequest(request)

const interceptedRequest = await interceptedRequestPromise

// Must have the cookie header set by the custom agent.
expect(Object.fromEntries(interceptedRequest.headers)).toEqual(
expect.objectContaining({
cookie: 'key=value',
})
)
})
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/**
* @vitest-environment node
*/
// @vitest-environment node
import { it, expect, beforeAll, afterEach, afterAll } from 'vitest'
import { HttpServer } from '@open-draft/test-server/http'
import { ClientRequestInterceptor } from '../../../../src/interceptors/ClientRequest'
Expand Down

0 comments on commit ceb649a

Please sign in to comment.