Skip to content

Commit

Permalink
Client reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
boronine committed Oct 14, 2024
1 parent 139f940 commit bcc15fd
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 102 deletions.
63 changes: 37 additions & 26 deletions src/h2tunnel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import {
ServerOptions,
TunnelClient,
TunnelServer,
TunnelState,
} from "./h2tunnel.js";
import { EventEmitter } from "node:events";
import * as assert from "node:assert";
import { Readable } from "node:stream";

Expand Down Expand Up @@ -109,44 +107,57 @@ const clientOptions: ClientOptions = {
cert: CLIENT_CRT,
localHttpPort: LOCAL_HTTP_PORT,
demuxListenPort: DEMUX_PORT,
tunnelRestartTimeout: 200,
};

test("http-proxies", async () => {
const abortController = new AbortController();

const stopDummyServer = await createDummyHttpServer();

// Start tunnel -----------------------------

const tunnelServer = new TunnelServer({
...serverOptions,
signal: abortController.signal,
});
await EventEmitter.once(tunnelServer, "listening" satisfies TunnelState);
const tunnelClient = new TunnelClient({
...clientOptions,
signal: abortController.signal,
async function expect503() {
const resp = await fetch(`http://localhost:${REMOTE_HTTP_PORT}`, {
method: "post",
body: "1",
headers: { "x-increment": "2" },
});
await EventEmitter.once(tunnelClient, "connected" satisfies TunnelState);

// ----------------------------------
assert.strictEqual(resp.status, 503);
}

async function expect200() {
const resp = await fetch(`http://localhost:${REMOTE_HTTP_PORT}`, {
method: "post",
body: "1",
headers: { "x-increment": "2" },
});
const body = await resp.text();
assert.strictEqual(body, "2");
console.log(resp.headers.keys());
assert.strictEqual(resp.headers.get("x-incremented"), "3");
}

test("happy-path", async () => {
const stopDummyServer = await createDummyHttpServer();

let tunnelServer = new TunnelServer(serverOptions);

// Make a request too early
await expect503();

await tunnelServer.waitUntilListening();

let tunnelClient = new TunnelClient(clientOptions);

// Make a request too early
await expect503();

// Wait until client is connected and test 200
await tunnelClient.waitUntilConnected();
await expect200();

console.log("HELL YEASHHH");
// Restart server while client is running
await tunnelServer.stop();
tunnelServer = new TunnelServer(serverOptions);

//////////////////////////////////
// Make sure client reconnected
await tunnelClient.waitUntilConnected();
await expect200();

abortController.abort();
await tunnelClient.waitUntilStopped();
await tunnelServer.waitUntilStopped();
await tunnelClient.stop();
await tunnelServer.stop();
await stopDummyServer();
});
Loading

0 comments on commit bcc15fd

Please sign in to comment.