Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/dev-middleware/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"chromium-edge-launcher": "^0.2.0",
"connect": "^3.6.5",
"debug": "^2.2.0",
"node-fetch": "^2.2.0",
"nullthrows": "^1.1.1",
"open": "^7.0.3",
"selfsigned": "^2.4.1",
Expand Down
49 changes: 27 additions & 22 deletions packages/dev-middleware/src/__tests__/FetchUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
*/

import type {JSONSerializable} from '../inspector-proxy/types';
import typeof * as NodeFetch from 'node-fetch';

import https from 'https';
import {Agent} from 'undici';

/**
Expand Down Expand Up @@ -46,25 +44,32 @@ export async function fetchJson<T: JSONSerializable>(url: string): Promise<T> {
return response.json();
}

export function allowSelfSignedCertsInNodeFetch(): void {
jest.mock('node-fetch', () => {
const originalModule = jest.requireActual<NodeFetch>('node-fetch');
const nodeFetch = originalModule.default;
return {
__esModule: true,
...originalModule,
default: (url, options) => {
if (
(url instanceof URL && url.protocol === 'https:') ||
(typeof url === 'string' && url.startsWith('https:'))
) {
const agent = new https.Agent({
rejectUnauthorized: false,
});
return nodeFetch(url.toString(), {agent, ...options});
}
return nodeFetch(url, options);
},
};
/**
* Change the global fetch dispatcher to allow self-signed certificates.
* This runs with Jest's `beforeAll` and `afterAll`, and restores the original dispatcher.
*/
export function withFetchSelfSignedCertsForAllTests() {
const fetchOriginal = globalThis.fetch;
const selfSignedCertDispatcher = new Agent({
connect: {
rejectUnauthorized: false,
},
});

let fetchSpy;

beforeAll(() => {
// For some reason, setting the `selfSignedCertDispatcher` with `setGlobalDispatcher` doesn't work.
// Instead of using `setGlobalDispatcher`, we'll use a spy to intercept the fetch calls and add the dispatcher.
fetchSpy = jest.spyOn(globalThis, 'fetch').mockImplementation((url, options) => (
fetchOriginal(url, {
...options,
dispatcher: options?.dispatcher ?? selfSignedCertDispatcher,
})
));
});

afterAll(() => {
fetchSpy.mockRestore();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
* @format
* @oncall react_native
*/

import {allowSelfSignedCertsInNodeFetch} from './FetchUtils';
import {withFetchSelfSignedCertsForAllTests} from './FetchUtils';
import {
createAndConnectTarget,
parseJsonFromDataUri,
Expand All @@ -33,16 +32,14 @@ jest.useRealTimers();

jest.setTimeout(10000);

beforeAll(() => {
// inspector-proxy uses node-fetch for source map fetching.
allowSelfSignedCertsInNodeFetch();

jest.resetModules();
});

describe.each(['HTTP', 'HTTPS'])(
'inspector proxy CDP rewriting hacks over %s',
protocol => {
// Inspector proxy tests are using a self-signed certificate for HTTPS tests.
if (protocol === 'HTTPS') {
withFetchSelfSignedCertsForAllTests();
}

const serverRef = withServerForEachTest({
logger: undefined,
projectRoot: __dirname,
Expand Down
1 change: 0 additions & 1 deletion packages/dev-middleware/src/inspector-proxy/Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type {
import DeviceEventReporter from './DeviceEventReporter';
import * as fs from 'fs';
import invariant from 'invariant';
import fetch from 'node-fetch';
import * as path from 'path';
import WS from 'ws';

Expand Down