Files
react-native/packages/dev-middleware/src/__tests__/FetchUtils.js
T
Moti Zilberman c6eccda2c7 Add tests for HTTPS-specific behaviour (#41341)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/41341

* Extends `dev-middleware`'s test utilities to enable testing against an HTTPS server with a self-signed cert.
* Runs the CDP transport integration tests (D51002261) using both HTTP and HTTPS.
* Adds a test to explicitly cover the `ws=...` / `wss=...` variation in `devtoolsFrontendUrl` first introduced in D49158227.

Changelog: [Internal]

Reviewed By: blakef

Differential Revision: D51006835

fbshipit-source-id: df3db8cd865898248cd0d8f307f75949a7f313fd
2023-11-08 07:19:40 -08:00

46 lines
1.2 KiB
JavaScript

/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
* @oncall react_native
*/
import type {JSONSerializable} from '../inspector-proxy/types';
import {Agent} from 'undici';
/**
* A version of `fetch` that is usable with the HTTPS server created in
* ServerUtils (which uses a self-signed certificate).
*/
export async function fetchLocal(
url: string,
options?: Parameters<typeof fetch>[1] & {dispatcher?: mixed},
): ReturnType<typeof fetch> {
return await fetch(url, {
...options,
// Node's native `fetch` comes from undici and supports the same options,
// including `dispatcher` which we use to make it accept self-signed
// certificates.
dispatcher:
options?.dispatcher ??
new Agent({
connect: {
rejectUnauthorized: false,
},
}),
});
}
export async function fetchJson<T: JSONSerializable>(url: string): Promise<T> {
const response = await fetchLocal(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
}
return response.json();
}