Skip to content

Commit

Permalink
test: http2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mastercuber committed Sep 14, 2023
1 parent e8988b9 commit 84c4880
Showing 1 changed file with 87 additions and 1 deletion.
88 changes: 87 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,66 @@
import { resolve } from "node:path";
import type { IncomingMessage, ServerResponse } from "node:http";
import { request } from "node:http";
import { request as httpsRequest } from "node:https";
import { connect } from "node:http2";
import { describe, afterEach, test, expect } from "vitest";
import { listen, Listener } from "../src";

// eslint-disable-next-line no-console
// console.log = fn()

function handle(request: IncomingMessage, response: ServerResponse) {
response.end(request.url);
response.end(
JSON.stringify({
path: request.url,
httpVersion: request.httpVersion,
}),
);
}

// disable TLS certificate checks
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

function sendRequest(url: string, https = false) {
return new Promise((resolve) => {
(https ? httpsRequest : request)(url, (res) => {
const data: any[] = [];
res.on("data", (chunk) => {
data.push(chunk);
});
res.on("end", () => {
resolve(data.join(""));
});
}).end();
});
}

function sendHttp2Request(url: string) {
// eslint-disable-next-line promise/param-names
return new Promise((resolve1, reject) => {
const client = connect(url);

client.on("error", (err: Error) => {
reject(err);
client.close();
});

const req = client.request({
":path": "/",
});

let data = "";
req.on("data", (chunk) => {
data += chunk;
});

req.on("end", () => {
resolve1(data);
client.close();
});

req.end();
});
}

describe("listhen", () => {
Expand Down Expand Up @@ -45,9 +98,42 @@ describe("listhen", () => {
expect(listener.url.endsWith("/foo/bar")).toBe(true);
// eslint-disable-next-line no-console
// expect(console.log).toHaveBeenCalledWith(expect.stringMatching('\n > Local: http://localhost:3000/foo/bar'))
const response = (await sendRequest(listener.url)) as string;
expect(JSON.parse(response)).toEqual({
path: "/foo/bar",
httpVersion: "1.1",
});
});

// see https://http2.github.io/faq/#does-http2-require-encryption
test("listen (http2)", async () => {
listener = await listen(handle);
expect(listener.url.startsWith("http://")).toBeTruthy();
await expect(sendHttp2Request(listener.url)).rejects.toThrowError(
"Protocol error",
);
});

describe("https", () => {
test("listen (http2)", async () => {
listener = await listen(handle, {
https: true,
});
expect(listener.url.startsWith("https:")).toBeTruthy();

let response = (await sendRequest(listener.url, true)) as string;
expect(JSON.parse(response)).toEqual({
path: "/",
httpVersion: "1.1",
});

response = (await sendHttp2Request(listener.url)) as string;
expect(JSON.parse(response)).toEqual({
path: "/",
httpVersion: "2.0",
});
});

test("listen (https - selfsigned)", async () => {
listener = await listen(handle, { https: true, hostname: "localhost" });
expect(listener.url.startsWith("https://")).toBe(true);
Expand Down

0 comments on commit 84c4880

Please sign in to comment.