test: exercise APNs active proxy state

This commit is contained in:
jesse-merhi
2026-05-04 02:30:39 +10:00
committed by clawsweeper
parent 715893631e
commit ce5d7fe8a6
2 changed files with 9 additions and 6 deletions

View File

@@ -1,5 +1,10 @@
import type http2 from "node:http2";
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
_resetActiveManagedProxyStateForTests,
registerActiveManagedProxyUrl,
stopActiveManagedProxyRegistration,
} from "./net/proxy/active-proxy-state.js";
const { connectSpy, tlsConnectSpy, tunnelSpy, fakeSession, fakeTlsSocket } = vi.hoisted(() => {
const fakeSession = { close: vi.fn(), destroy: vi.fn() };
@@ -32,6 +37,7 @@ describe("connectApnsHttp2Session", () => {
connectSpy.mockClear();
tlsConnectSpy.mockClear();
tunnelSpy.mockClear();
_resetActiveManagedProxyStateForTests();
});
it("uses direct http2.connect when managed proxy is inactive", async () => {
const { connectApnsHttp2Session } = await import("./push-apns-http2.js");
@@ -39,7 +45,6 @@ describe("connectApnsHttp2Session", () => {
const session = await connectApnsHttp2Session({
authority: "https://api.sandbox.push.apple.com",
timeoutMs: 10_000,
getManagedProxyUrl: () => undefined,
});
expect(session).toBe(fakeSession);
@@ -48,13 +53,14 @@ describe("connectApnsHttp2Session", () => {
});
it("uses an HTTP CONNECT tunnel when managed proxy is active", async () => {
const registration = registerActiveManagedProxyUrl("http://proxy.example:8080");
const { connectApnsHttp2Session } = await import("./push-apns-http2.js");
const session = await connectApnsHttp2Session({
authority: "https://api.push.apple.com",
timeoutMs: 10_000,
getManagedProxyUrl: () => "http://proxy.example:8080",
});
stopActiveManagedProxyRegistration(registration);
expect(session).toBe(fakeSession);
expect(tunnelSpy).toHaveBeenCalledWith({
@@ -87,7 +93,6 @@ describe("connectApnsHttp2Session", () => {
const session = await connectApnsHttp2Session({
authority: "https://api.push.apple.com",
timeoutMs: 10_000,
getManagedProxyUrl: () => undefined,
});
expect(session).toBe(fakeSession);
@@ -108,7 +113,6 @@ describe("connectApnsHttp2Session", () => {
connectApnsHttp2Session({
authority: "https://example.com",
timeoutMs: 10_000,
getManagedProxyUrl: () => "http://proxy.example:8080",
}),
).rejects.toThrow("Unsupported APNs authority");
});

View File

@@ -13,7 +13,6 @@ type ApnsAuthority = "https://api.push.apple.com" | "https://api.sandbox.push.ap
export type ConnectApnsHttp2SessionParams = {
authority: string;
timeoutMs: number;
getManagedProxyUrl?: () => string | undefined;
};
function assertApnsAuthority(authority: string): ApnsAuthority {
@@ -34,7 +33,7 @@ export async function connectApnsHttp2Session(
params: ConnectApnsHttp2SessionParams,
): Promise<http2.ClientHttp2Session> {
const authority = assertApnsAuthority(params.authority);
const proxyUrl = (params.getManagedProxyUrl ?? getActiveManagedProxyUrl)();
const proxyUrl = getActiveManagedProxyUrl();
if (!proxyUrl) {
return http2.connect(authority);
}