-
-
Notifications
You must be signed in to change notification settings - Fork 133
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support
addRequest
from custom http agents
- Loading branch information
1 parent
2ec79d7
commit ceb649a
Showing
3 changed files
with
70 additions
and
3 deletions.
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
56 changes: 56 additions & 0 deletions
56
test/modules/http/regressions/http-agent-add-request.test.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,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', | ||
}) | ||
) | ||
}) |
4 changes: 1 addition & 3 deletions
4
test/modules/http/regressions/http-concurrent-different-response-source.test.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