mirror of
https://github.com/hanzoai/puppeteer.git
synced 2026-09-24 17:02:14 +00:00
fix: forward headers to browserURL HTTP discovery and WebSocket connection (#15238)
Co-authored-by: Aliaksandr Sibiryakov <[email protected]>
This commit is contained in:
committed by
GitHub
co-authored by
Aliaksandr Sibiryakov
parent
274126dd8f
commit
a8ebc5c2bd
@@ -4,13 +4,52 @@
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
import {describe, it} from 'node:test';
|
||||
import {describe, it, mock, afterEach} from 'node:test';
|
||||
|
||||
import expect from 'expect';
|
||||
|
||||
import {_connectToBrowser} from './BrowserConnector.js';
|
||||
|
||||
describe('BrowserConnector', () => {
|
||||
describe('getWSEndpoint via browserURL', () => {
|
||||
afterEach(() => {
|
||||
mock.restoreAll();
|
||||
});
|
||||
|
||||
it('should forward headers to the /json/version HTTP request', async () => {
|
||||
const capturedRequests: Array<{url: string; init?: RequestInit}> = [];
|
||||
|
||||
mock.method(
|
||||
globalThis,
|
||||
'fetch',
|
||||
async (url: string | URL | Request, init?: RequestInit) => {
|
||||
capturedRequests.push({url: String(url), init});
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
webSocketDebuggerUrl: 'ws://localhost:1234/devtools/browser/1',
|
||||
}),
|
||||
{status: 200, headers: {'Content-Type': 'application/json'}},
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
// We expect this to fail when trying to open the WebSocket, which is fine —
|
||||
// we only care that fetch was called with the right headers.
|
||||
await _connectToBrowser({
|
||||
browserURL: 'http://localhost:1234',
|
||||
headers: {Authorization: 'Bearer test-token'},
|
||||
}).catch(() => {});
|
||||
|
||||
expect(capturedRequests).toHaveLength(1);
|
||||
expect(capturedRequests[0]!.url).toContain('/json/version');
|
||||
expect(
|
||||
(capturedRequests[0]!.init?.headers as Record<string, string>)?.[
|
||||
'Authorization'
|
||||
],
|
||||
).toBe('Bearer test-token');
|
||||
});
|
||||
});
|
||||
|
||||
describe('_connectToBrowser', () => {
|
||||
it('should throw an error when both blocklist and allowlist are specified', async () => {
|
||||
await expect(
|
||||
|
||||
@@ -115,10 +115,10 @@ async function getConnectionTransport(
|
||||
endpointUrl: browserWSEndpoint,
|
||||
};
|
||||
} else if (browserURL) {
|
||||
const connectionURL = await getWSEndpoint(browserURL);
|
||||
const connectionURL = await getWSEndpoint(browserURL, headers);
|
||||
const WebSocketClass = await getWebSocketTransportClass();
|
||||
const connectionTransport: ConnectionTransport =
|
||||
await WebSocketClass.create(connectionURL);
|
||||
await WebSocketClass.create(connectionURL, headers);
|
||||
return {
|
||||
connectionTransport: connectionTransport,
|
||||
endpointUrl: connectionURL,
|
||||
@@ -181,12 +181,16 @@ async function getConnectionTransport(
|
||||
throw new Error('Invalid connection options');
|
||||
}
|
||||
|
||||
async function getWSEndpoint(browserURL: string): Promise<string> {
|
||||
async function getWSEndpoint(
|
||||
browserURL: string,
|
||||
headers?: Record<string, string>,
|
||||
): Promise<string> {
|
||||
const endpointURL = new URL('/json/version', browserURL);
|
||||
|
||||
try {
|
||||
const result = await globalThis.fetch(endpointURL.toString(), {
|
||||
method: 'GET',
|
||||
headers,
|
||||
});
|
||||
if (!result.ok) {
|
||||
throw new Error(`HTTP ${result.statusText}`);
|
||||
|
||||
Reference in New Issue
Block a user