feat: add bypassCustomProtocolHandlers option to net.request (#48882)

* feat: add bypassCustomProtocolHandlers option to net.request

Co-authored-by: Kai <udbmnm@163.com>

* style: fix lint errors in api-protocol-spec

Co-authored-by: Kai <udbmnm@163.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Kai <udbmnm@163.com>
This commit is contained in:
trop[bot] 2025-11-13 10:35:06 -05:00 committed by GitHub
commit b5f19ce974
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 1 deletions

View file

@ -15,6 +15,7 @@ import * as webStream from 'node:stream/web';
import { setTimeout } from 'node:timers/promises';
import * as url from 'node:url';
import { collectStreamBody, getResponse } from './lib/net-helpers';
import { listen, defer, ifit } from './lib/spec-helpers';
import { WebmGenerator } from './lib/video-helpers';
import { closeAllWindows, closeWindow } from './lib/window-helpers';
@ -1578,6 +1579,22 @@ describe('protocol module', () => {
expect(await net.fetch(url, { bypassCustomProtocolHandlers: true }).then(r => r.text())).to.equal('default');
});
it('can bypass intercepted protocol handlers with net.request', async () => {
protocol.handle('http', () => new Response('custom'));
defer(() => { protocol.unhandle('http'); });
const server = http.createServer((req, res) => {
res.end('default');
});
defer(() => server.close());
const { url } = await listen(server);
// Make a request using net.request with bypassCustomProtocolHandlers: true
const request = net.request({ method: 'GET', url, bypassCustomProtocolHandlers: true });
const response = await getResponse(request);
const body = await collectStreamBody(response);
expect(response.statusCode).to.equal(200);
expect(body).to.equal('default');
});
it('bypassing custom protocol handlers also bypasses new protocols', async () => {
protocol.handle('app', () => new Response('custom'));
defer(() => { protocol.unhandle('app'); });