Files
hyperframes/packages/cli/src/capture/upstreamProxy.test.ts
T
Xuanru LiandJerrai 53d3caeac5 feat(cli): capture can route its traffic through an opt-in upstream proxy (#6499)
* feat(cli): capture can route its traffic through an opt-in upstream proxy

HYPERFRAMES_CAPTURE_PROXY=http://host:port sends the capture browser proxy's
forwarded requests and CONNECT tunnels, and every publicRequest download,
to that proxy by hostname instead of dialing a checked public IPv4. Unset,
behaviour is unchanged.

* fix(cli): keep capture comment share flat and close the new CodeQL findings

Test fixtures record what they receive instead of echoing request data, and
capture --video strips line breaks from a failed download's message before
logging it.

* fix(cli): proxied HTTP downloads send no URL fragment, and a malformed proxy credential is refused

The absolute-form request to the upstream used the URL's href, so a fragment such as
sprite.svg#icon went on the request line, which the pinned path never sends and strict proxies
refuse. A malformed percent-escape in the proxy credentials threw a bare URIError instead of the
usual "not a valid proxy URL" error. The test servers label their echoes text/plain.

* fix(cli): strip line breaks from capture --video errors in the form CodeQL recognizes

* fix(cli): capture --video logs errors through a recognized newline sanitizer

---------

Co-authored-by: Jerrai <jerrai@heygen.com>
2026-10-27 02:52:52 +00:00

45 lines
1.8 KiB
TypeScript

import { describe, expect, it } from "vitest";
import { captureUpstreamProxy, connectAuthority } from "./upstreamProxy.js";
describe("captureUpstreamProxy", () => {
it("is off when HYPERFRAMES_CAPTURE_PROXY is unset or blank, whatever HTTPS_PROXY says", () => {
expect(captureUpstreamProxy({ HTTPS_PROXY: "http://proxy:3128" })).toBeUndefined();
expect(captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: " " })).toBeUndefined();
});
it("refuses a value that is not an http:// proxy URL", () => {
expect(() => captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "proxy:3128" })).toThrow(
/must be an http:\/\/ proxy URL/,
);
expect(() => captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "https://proxy:3128" })).toThrow(
/must be an http:\/\/ proxy URL/,
);
expect(() => captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "http://[bad" })).toThrow(
/not a valid proxy URL/,
);
expect(() =>
captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "http://user:%E0%A4%A@proxy:3128" }),
).toThrow(/not a valid proxy URL/);
});
it("reads host and port, and carries proxy credentials as Basic auth", () => {
expect(
captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "http://user:p%40ss@127.0.0.1:3128" }),
).toEqual({
host: "127.0.0.1",
port: 3128,
authorization: `Basic ${Buffer.from("user:p@ss").toString("base64")}`,
});
expect(captureUpstreamProxy({ HYPERFRAMES_CAPTURE_PROXY: "http://[::1]" })).toEqual({
host: "::1",
port: 80,
authorization: undefined,
});
});
it("names the default port in CONNECT authorities", () => {
expect(connectAuthority(new URL("https://example.com/a"))).toBe("example.com:443");
expect(connectAuthority(new URL("https://example.com:8443/a"))).toBe("example.com:8443");
});
});