Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

outboundHttp.test.ts

97 lines4.1 KB

1// @effect-diagnostics nodeBuiltinImport:off - the test runs a real TLS h2 server to see which protocol fetch negotiates.
2import * as NodeHttp2 from "node:http2";
3import * as NodeNet from "node:net";
4import * as NodeTls from "node:tls";
5​
6import { it } from "@effect/vitest";
7import * as Effect from "effect/Effect";
8import { FetchHttpClient, HttpClient } from "effect/http";
9import * as Undici from "undici";
10import { afterAll, afterEach, beforeAll, describe, expect } from "vite-plus/test";
11​
12import { builtinFetchUsesHttp2, installHttp1FetchDispatcher } from "./outboundHttp.ts";
13​
14// Test-only self-signed P-256 certificate for 127.0.0.1, valid until 2126; not a
15// secret. Node cannot mint X.509 certificates without a library, and a local
16// server only offers h2 over ALPN, which needs TLS.
17const TEST_KEY = `-----BEGIN PRIVATE KEY-----
18MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg3EwCztQysKTvzdcR
19g0LWq+axzUd4tHi9PyMdqh7PlGShRANCAARfQpoLgv/tOsHWCLriDza7Ruaey2b7
20mR/nS6mLu6W7+agkEqDm0mE3F/ngpPjL4Jj0IGadeMcKNKFRSBgiLdfn
21-----END PRIVATE KEY-----
22`;
23const TEST_CERT = `-----BEGIN CERTIFICATE-----
24MIIBsDCCAVagAwIBAgIUWbn2Wt5kTxii1UU57NJMt/wTaUgwCgYIKoZIzj0EAwIw
25JDEiMCAGA1UEAwwZdDNjb2RlIG91dGJvdW5kIGh0dHAgdGVzdDAgFw0yNjEwMTEy
26MjQyMzZaGA8yMTI2MDkxNzIyNDIzNlowJDEiMCAGA1UEAwwZdDNjb2RlIG91dGJv
27dW5kIGh0dHAgdGVzdDBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABF9CmguC/+06
28wdYIuuIPNrtG5p7LZvuZH+dLqYu7pbv5qCQSoObSYTcX+eCk+MvgmPQgZp14xwo0
29oVFIGCIt1+ejZDBiMB0GA1UdDgQWBBSsekAc30Q/zdIE/tvVDQKJL5oiCDAfBgNV
30HSMEGDAWgBSsekAc30Q/zdIE/tvVDQKJL5oiCDAPBgNVHRMBAf8EBTADAQH/MA8G
31A1UdEQQIMAaHBH8AAAEwCgYIKoZIzj0EAwIDSAAwRQIgebUrkyP56242EJB7nCV7
32A+Ke/q0rNPya3fjQa9O5B+kCIQC1SfmdNaFcNJ4JINlus4FfrRnfKiADS2MIJR9f
3399BN0A==
34-----END CERTIFICATE-----
35`;
36​
37// An h2-capable origin, like api.github.com, that answers with the HTTP
38// version the client negotiated.
39const server = NodeHttp2.createSecureServer(
40 { allowHTTP1: true, key: TEST_KEY, cert: TEST_CERT },
41 (request, response) => response.end(request.httpVersion),
42);
43let origin = "";
44const defaultCaCertificates = NodeTls.getCACertificates("default");
45const defaultDispatcher = Undici.getGlobalDispatcher();
46​
47beforeAll(async () => {
48 NodeTls.setDefaultCACertificates([...defaultCaCertificates, TEST_CERT]);
49 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
50 origin = `https://127.0.0.1:${(server.address() as NodeNet.AddressInfo).port}/`;
51});
52​
53afterEach(() => {
54 Undici.setGlobalDispatcher(defaultDispatcher);
55});
56​
57afterAll(async () => {
58 NodeTls.setDefaultCACertificates(defaultCaCertificates);
59 await new Promise<void>((resolve) => server.close(() => resolve()));
60});
61​
62const versionViaGlobalDispatcher = async () => (await Undici.request(origin)).body.text();
63​
64// Node's built-in fetch, the path the server's own requests take.
65const versionViaBuiltinFetch = () =>
66 HttpClient.get(origin).pipe(
67 Effect.flatMap((response) => response.text),
68 Effect.provide(FetchHttpClient.layer),
69 );
70​
71describe("installHttp1FetchDispatcher", () => {
72 it("makes the global dispatcher negotiate HTTP/1.1 and keeps the env proxy agent", async () => {
73 // undici 8's default agent, the one Node 26's fetch uses.
74 Undici.setGlobalDispatcher(new Undici.Agent());
75 expect(await versionViaGlobalDispatcher()).toBe("2.0");
76​
77 installHttp1FetchDispatcher();
78 expect(await versionViaGlobalDispatcher()).toBe("1.1");
79​
80 // Node installs this agent for --use-env-proxy; replacing it would drop the proxy.
81 Undici.setGlobalDispatcher(new Undici.EnvHttpProxyAgent());
82 installHttp1FetchDispatcher();
83 expect(Undici.getGlobalDispatcher()).toBeInstanceOf(Undici.EnvHttpProxyAgent);
84 });
85​
86 // Only a runtime that bundles undici 8 (Node 26+) offers h2 through the
87 // built-in fetch, so only there can this prove fetch reads the new dispatcher.
88 it.effect.runIf(builtinFetchUsesHttp2(process.versions.undici))(
89 "moves the built-in fetch off HTTP/2",
90 () =>
91 Effect.gen(function* () {
92 expect(yield* versionViaBuiltinFetch()).toBe("2.0");
93 installHttp1FetchDispatcher();
94 expect(yield* versionViaBuiltinFetch()).toBe("1.1");
95 }),
96 );
97});