2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-06-06 06:29:55 -04:00
|
|
|
|
|
|
|
import * as http2 from "node:http2";
|
|
|
|
import * as net from "node:net";
|
2024-02-21 07:43:01 -05:00
|
|
|
import { assert, assertEquals } from "@std/assert/mod.ts";
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
for (const url of ["http://127.0.0.1:4246", "https://127.0.0.1:4247"]) {
|
|
|
|
Deno.test(`[node/http2 client] ${url}`, {
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
}, async () => {
|
|
|
|
// Create a server to respond to the HTTP2 requests
|
|
|
|
const client = http2.connect(url, {});
|
|
|
|
client.on("error", (err) => console.error(err));
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" }, {
|
|
|
|
waitForTrailers: true,
|
|
|
|
});
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
let receivedTrailers;
|
|
|
|
let receivedHeaders;
|
|
|
|
let receivedData = "";
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
req.on("response", (headers, _flags) => {
|
|
|
|
receivedHeaders = headers;
|
|
|
|
});
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
req.write("hello");
|
|
|
|
req.setEncoding("utf8");
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
req.on("wantTrailers", () => {
|
|
|
|
req.sendTrailers({ foo: "bar" });
|
|
|
|
});
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
req.on("trailers", (trailers, _flags) => {
|
|
|
|
receivedTrailers = trailers;
|
|
|
|
});
|
|
|
|
|
|
|
|
req.on("data", (chunk) => {
|
|
|
|
receivedData += chunk;
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const { promise, resolve } = Promise.withResolvers<void>();
|
2023-09-15 15:51:25 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
client.close();
|
|
|
|
} catch (_) {
|
|
|
|
// pass
|
|
|
|
}
|
2023-11-22 06:11:20 -05:00
|
|
|
resolve();
|
2023-09-15 15:51:25 -04:00
|
|
|
}, 2000);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await promise;
|
2023-09-15 15:51:25 -04:00
|
|
|
assertEquals(receivedHeaders, { ":status": 200 });
|
|
|
|
assertEquals(receivedData, "hello world\n");
|
|
|
|
|
|
|
|
assertEquals(receivedTrailers, {
|
|
|
|
"abc": "def",
|
|
|
|
"opr": "stv",
|
|
|
|
"foo": "bar",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2023-06-06 06:29:55 -04:00
|
|
|
|
2023-10-12 10:03:19 -04:00
|
|
|
Deno.test(`[node/http2 client createConnection]`, {
|
|
|
|
ignore: Deno.build.os === "windows",
|
|
|
|
}, async () => {
|
|
|
|
const url = "http://127.0.0.1:4246";
|
2023-11-22 06:11:20 -05:00
|
|
|
const createConnDeferred = Promise.withResolvers<void>();
|
2023-10-12 10:03:19 -04:00
|
|
|
// Create a server to respond to the HTTP2 requests
|
|
|
|
const client = http2.connect(url, {
|
|
|
|
createConnection() {
|
|
|
|
const socket = net.connect({ host: "127.0.0.1", port: 4246 });
|
|
|
|
|
|
|
|
socket.on("connect", () => {
|
2023-11-22 06:11:20 -05:00
|
|
|
createConnDeferred.resolve();
|
2023-10-12 10:03:19 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
return socket;
|
|
|
|
},
|
|
|
|
});
|
|
|
|
client.on("error", (err) => console.error(err));
|
|
|
|
|
|
|
|
const req = client.request({ ":method": "POST", ":path": "/" });
|
|
|
|
|
|
|
|
let receivedData = "";
|
|
|
|
|
|
|
|
req.write("hello");
|
|
|
|
req.setEncoding("utf8");
|
|
|
|
|
|
|
|
req.on("data", (chunk) => {
|
|
|
|
receivedData += chunk;
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
const endPromise = Promise.withResolvers<void>();
|
2023-10-12 10:03:19 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
try {
|
|
|
|
client.close();
|
|
|
|
} catch (_) {
|
|
|
|
// pass
|
|
|
|
}
|
|
|
|
endPromise.resolve();
|
|
|
|
}, 2000);
|
|
|
|
|
2023-11-22 06:11:20 -05:00
|
|
|
await createConnDeferred.promise;
|
|
|
|
await endPromise.promise;
|
2023-10-12 10:03:19 -04:00
|
|
|
assertEquals(receivedData, "hello world\n");
|
|
|
|
});
|
|
|
|
|
2023-09-15 15:51:25 -04:00
|
|
|
// TODO(bartlomieju): reenable sanitizers
|
|
|
|
Deno.test("[node/http2 server]", { sanitizeOps: false }, async () => {
|
2023-06-06 06:29:55 -04:00
|
|
|
const server = http2.createServer();
|
|
|
|
server.listen(0);
|
|
|
|
const port = (<net.AddressInfo> server.address()).port;
|
|
|
|
const sessionPromise = new Promise<http2.Http2Session>((resolve) =>
|
|
|
|
server.on("session", resolve)
|
|
|
|
);
|
|
|
|
|
|
|
|
const responsePromise = fetch(`http://localhost:${port}/path`, {
|
|
|
|
method: "POST",
|
|
|
|
body: "body",
|
|
|
|
});
|
|
|
|
|
|
|
|
const session = await sessionPromise;
|
|
|
|
const stream = await new Promise<http2.ServerHttp2Stream>((resolve) =>
|
|
|
|
session.on("stream", resolve)
|
|
|
|
);
|
2023-11-22 06:11:20 -05:00
|
|
|
await new Promise((resolve) => stream.on("headers", resolve));
|
|
|
|
await new Promise((resolve) => stream.on("data", resolve));
|
|
|
|
await new Promise((resolve) => stream.on("end", resolve));
|
2023-06-06 06:29:55 -04:00
|
|
|
stream.respond();
|
|
|
|
stream.end();
|
|
|
|
const resp = await responsePromise;
|
|
|
|
await resp.text();
|
|
|
|
|
|
|
|
await new Promise((resolve) => server.close(resolve));
|
|
|
|
});
|
2024-02-21 07:43:01 -05:00
|
|
|
|
|
|
|
Deno.test("[node/http2 client GET https://www.example.com]", async () => {
|
|
|
|
const clientSession = http2.connect("https://www.example.com");
|
|
|
|
const req = clientSession.request({
|
|
|
|
":method": "GET",
|
|
|
|
":path": "/",
|
|
|
|
});
|
|
|
|
let headers = {};
|
|
|
|
let status: number | undefined = 0;
|
|
|
|
let chunk = new Uint8Array();
|
|
|
|
const endPromise = Promise.withResolvers<void>();
|
|
|
|
req.on("response", (h) => {
|
|
|
|
status = h[":status"];
|
|
|
|
headers = h;
|
|
|
|
});
|
|
|
|
req.on("data", (c) => {
|
|
|
|
chunk = c;
|
|
|
|
});
|
|
|
|
req.on("end", () => {
|
|
|
|
clientSession.close();
|
|
|
|
req.close();
|
|
|
|
endPromise.resolve();
|
|
|
|
});
|
|
|
|
req.end();
|
|
|
|
await endPromise.promise;
|
|
|
|
assert(Object.keys(headers).length > 0);
|
|
|
|
assertEquals(status, 200);
|
|
|
|
assert(chunk.length > 0);
|
|
|
|
});
|