1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-24 16:19:12 -05:00

fix(ext/node): http2.createServer (#22708)

This commit is contained in:
Satya Rohith 2024-03-07 19:28:46 +05:30 committed by GitHub
parent 0fb67ce43e
commit 588dd5e669
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 1034 additions and 241 deletions

File diff suppressed because it is too large Load diff

View file

@ -2248,6 +2248,16 @@ export class ERR_FALSY_VALUE_REJECTION extends NodeError {
this.reason = reason;
}
}
export class ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS extends NodeError {
constructor() {
super(
"ERR_HTTP2_TOO_MANY_CUSTOM_SETTINGS",
"Number of custom settings exceeds MAX_ADDITIONAL_SETTINGS",
);
}
}
export class ERR_HTTP2_INVALID_SETTING_VALUE extends NodeRangeError {
actual: unknown;
min?: number;

View file

@ -8,6 +8,8 @@ import {
assertEquals,
assertStringIncludes,
assertThrows,
curlRequest,
curlRequestWithStdErr,
execCode,
fail,
tmpUnixSocketPath,
@ -3793,32 +3795,6 @@ Deno.test(
},
);
async function curlRequest(args: string[]) {
const { success, stdout, stderr } = await new Deno.Command("curl", {
args,
stdout: "piped",
stderr: "piped",
}).output();
assert(
success,
`Failed to cURL ${args}: stdout\n\n${stdout}\n\nstderr:\n\n${stderr}`,
);
return new TextDecoder().decode(stdout);
}
async function curlRequestWithStdErr(args: string[]) {
const { success, stdout, stderr } = await new Deno.Command("curl", {
args,
stdout: "piped",
stderr: "piped",
}).output();
assert(
success,
`Failed to cURL ${args}: stdout\n\n${stdout}\n\nstderr:\n\n${stderr}`,
);
return [new TextDecoder().decode(stdout), new TextDecoder().decode(stderr)];
}
Deno.test("Deno.HttpServer is not thenable", async () => {
// deno-lint-ignore require-await
async function serveTest() {

View file

@ -1,6 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
import * as colors from "@std/fmt/colors.ts";
import { assert } from "@std/assert/mod.ts";
export { colors };
import { join, resolve } from "@std/path/mod.ts";
export {
@ -85,3 +86,35 @@ export function tmpUnixSocketPath(): string {
const folder = Deno.makeTempDirSync();
return join(folder, "socket");
}
export async function curlRequest(args: string[]) {
const { success, stdout, stderr } = await new Deno.Command("curl", {
args,
stdout: "piped",
stderr: "piped",
}).output();
const decoder = new TextDecoder();
assert(
success,
`Failed to cURL ${args}: stdout\n\n${
decoder.decode(stdout)
}\n\nstderr:\n\n${decoder.decode(stderr)}`,
);
return decoder.decode(stdout);
}
export async function curlRequestWithStdErr(args: string[]) {
const { success, stdout, stderr } = await new Deno.Command("curl", {
args,
stdout: "piped",
stderr: "piped",
}).output();
const decoder = new TextDecoder();
assert(
success,
`Failed to cURL ${args}: stdout\n\n${
decoder.decode(stdout)
}\n\nstderr:\n\n${decoder.decode(stderr)}`,
);
return [decoder.decode(stdout), decoder.decode(stderr)];
}

View file

@ -3,6 +3,7 @@
import * as http2 from "node:http2";
import * as net from "node:net";
import { assert, assertEquals } from "@std/assert/mod.ts";
import { curlRequest } from "../unit/test_util.ts";
for (const url of ["http://127.0.0.1:4246", "https://127.0.0.1:4247"]) {
Deno.test(`[node/http2 client] ${url}`, {
@ -108,35 +109,6 @@ Deno.test(`[node/http2 client createConnection]`, {
assertEquals(receivedData, "hello world\n");
});
// TODO(bartlomieju): reenable sanitizers
Deno.test("[node/http2 server]", { sanitizeOps: false }, async () => {
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)
);
await new Promise((resolve) => stream.on("headers", resolve));
await new Promise((resolve) => stream.on("data", resolve));
await new Promise((resolve) => stream.on("end", resolve));
stream.respond();
stream.end();
const resp = await responsePromise;
await resp.text();
await new Promise((resolve) => server.close(resolve));
});
Deno.test("[node/http2 client GET https://www.example.com]", async () => {
const clientSession = http2.connect("https://www.example.com");
const req = clientSession.request({
@ -165,3 +137,30 @@ Deno.test("[node/http2 client GET https://www.example.com]", async () => {
assertEquals(status, 200);
assert(chunk.length > 0);
});
Deno.test("[node/http2.createServer()]", {
// TODO(satyarohith): enable the test on windows.
ignore: Deno.build.os === "windows",
}, async () => {
const server = http2.createServer((_req, res) => {
res.setHeader("Content-Type", "text/html");
res.setHeader("X-Foo", "bar");
res.writeHead(200, { "Content-Type": "text/plain; charset=utf-8" });
res.write("Hello, World!");
res.end();
});
server.listen(0);
const port = (<net.AddressInfo> server.address()).port;
const endpoint = `http://localhost:${port}`;
const response = await curlRequest([
endpoint,
"--http2-prior-knowledge",
]);
assertEquals(response, "Hello, World!");
server.close();
// Wait to avoid leaking the timer from here
// https://github.com/denoland/deno/blob/749b6e45e58ac87188027f79fe403d130f86bd73/ext/node/polyfills/net.ts#L2396-L2402
// Issue: https://github.com/denoland/deno/issues/22764
await new Promise<void>((resolve) => server.on("close", resolve));
});