1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-05 13:59:01 -05:00

update test cases

This commit is contained in:
Yoshiya Hinosawa 2024-09-26 13:24:55 +09:00
parent 2f9cdaa67d
commit 6301b5831c
No known key found for this signature in database
GPG key ID: 9017DB4559488785
2 changed files with 247 additions and 223 deletions

View file

@ -455,10 +455,6 @@ class ClientRequest extends OutgoingMessage {
await op_node_http_wait_for_connection(connRid);
this.emit("requestReady");
const res = await op_node_http_await_response(rid);
if (this._timeout) {
this._timeout.removeEventListener("abort", this._timeoutCb);
webClearTimeout(this._timeout[timerId]);
}
const incoming = new IncomingMessageForClient(this.socket);
incoming.req = this;
this.res = incoming;

View file

@ -328,74 +328,74 @@ Deno.test("[node/http] IncomingRequest socket has remoteAddress + remotePort", a
await promise;
});
// Deno.test("[node/http] request default protocol", async () => {
// const deferred1 = Promise.withResolvers<void>();
// const deferred2 = Promise.withResolvers<void>();
// const server = http.createServer((_, res) => {
// res.end("ok");
// });
Deno.test("[node/http] request default protocol", async () => {
const deferred1 = Promise.withResolvers<void>();
const deferred2 = Promise.withResolvers<void>();
const server = http.createServer((_, res) => {
res.end("ok");
});
// // @ts-ignore IncomingMessageForClient
// // deno-lint-ignore no-explicit-any
// let clientRes: any;
// // deno-lint-ignore no-explicit-any
// let clientReq: any;
// server.listen(() => {
// clientReq = http.request(
// // deno-lint-ignore no-explicit-any
// { host: "localhost", port: (server.address() as any).port },
// (res) => {
// assert(res.socket instanceof EventEmitter);
// assertEquals(res.complete, false);
// res.on("data", () => {});
// res.on("end", () => {
// server.close();
// });
// clientRes = res;
// assertEquals(res.statusCode, 200);
// deferred2.resolve();
// },
// );
// clientReq.end();
// });
// server.on("close", () => {
// deferred1.resolve();
// });
// await deferred1.promise;
// await deferred2.promise;
// assert(clientReq.socket instanceof EventEmitter);
// assertEquals(clientRes!.complete, true);
// });
// @ts-ignore IncomingMessageForClient
// deno-lint-ignore no-explicit-any
let clientRes: any;
// deno-lint-ignore no-explicit-any
let clientReq: any;
server.listen(() => {
clientReq = http.request(
// deno-lint-ignore no-explicit-any
{ host: "localhost", port: (server.address() as any).port },
(res) => {
assert(res.socket instanceof EventEmitter);
assertEquals(res.complete, false);
res.on("data", () => {});
res.on("end", () => {
server.close();
});
clientRes = res;
assertEquals(res.statusCode, 200);
deferred2.resolve();
},
);
clientReq.end();
});
server.on("close", () => {
deferred1.resolve();
});
await deferred1.promise;
await deferred2.promise;
assert(clientReq.socket instanceof EventEmitter);
assertEquals(clientRes!.complete, true);
});
// Deno.test("[node/http] request with headers", async () => {
// const { promise, resolve } = Promise.withResolvers<void>();
// const server = http.createServer((req, res) => {
// assertEquals(req.headers["x-foo"], "bar");
// res.end("ok");
// });
// server.listen(() => {
// const req = http.request(
// {
// host: "localhost",
// // deno-lint-ignore no-explicit-any
// port: (server.address() as any).port,
// headers: { "x-foo": "bar" },
// },
// (res) => {
// res.on("data", () => {});
// res.on("end", () => {
// server.close();
// });
// assertEquals(res.statusCode, 200);
// },
// );
// req.end();
// });
// server.on("close", () => {
// resolve();
// });
// await promise;
// });
Deno.test("[node/http] request with headers", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
const server = http.createServer((req, res) => {
assertEquals(req.headers["x-foo"], "bar");
res.end("ok");
});
server.listen(() => {
const req = http.request(
{
host: "localhost",
// deno-lint-ignore no-explicit-any
port: (server.address() as any).port,
headers: { "x-foo": "bar" },
},
(res) => {
res.on("data", () => {});
res.on("end", () => {
server.close();
});
assertEquals(res.statusCode, 200);
},
);
req.end();
});
server.on("close", () => {
resolve();
});
await promise;
});
Deno.test("[node/http] non-string buffer response", {
// TODO(kt3k): Enable sanitizer. A "zlib" resource is leaked in this test case.
@ -452,63 +452,68 @@ Deno.test("[node/http] http.IncomingMessage can be created without url", () => {
});
*/
// Deno.test("[node/http] send request with non-chunked body", async () => {
// let requestHeaders: Headers;
// let requestBody = "";
Deno.test(
"[node/http] send request with non-chunked body",
{ ignore: true },
async () => {
let requestHeaders: Headers;
let requestBody = "";
// const hostname = "localhost";
// const port = 4505;
const hostname = "localhost";
const port = 4505;
// const handler = async (req: Request) => {
// requestHeaders = req.headers;
// requestBody = await req.text();
// return new Response("ok");
// };
// const abortController = new AbortController();
// const servePromise = Deno.serve({
// hostname,
// port,
// signal: abortController.signal,
// onListen: undefined,
// }, handler).finished;
const handler = async (req: Request) => {
requestHeaders = req.headers;
requestBody = await req.text();
return new Response("ok");
};
const abortController = new AbortController();
const servePromise = Deno.serve({
// TODO(k3k): Enable this line for better compatibility with Node.js
// hostname,
port,
signal: abortController.signal,
onListen: undefined,
}, handler).finished;
// const opts: RequestOptions = {
// host: hostname,
// port,
// method: "POST",
// headers: {
// "Content-Type": "text/plain; charset=utf-8",
// "Content-Length": "11",
// },
// };
// const req = http.request(opts, (res) => {
// res.on("data", () => {});
// res.on("end", () => {
// abortController.abort();
// });
// assertEquals(res.statusCode, 200);
// assertEquals(requestHeaders.get("content-length"), "11");
// assertEquals(requestHeaders.has("transfer-encoding"), false);
// assertEquals(requestBody, "hello world");
// });
// req.on("socket", (socket) => {
// assert(socket.writable);
// assert(socket.readable);
// socket.setKeepAlive();
// socket.destroy();
// socket.setTimeout(100);
// });
// req.write("hello ");
// req.write("world");
// req.end();
const opts: RequestOptions = {
host: hostname,
port,
method: "POST",
headers: {
"Content-Type": "text/plain; charset=utf-8",
"Content-Length": "11",
},
};
const req = http.request(opts, (res) => {
res.on("data", () => {});
res.on("end", () => {
abortController.abort();
});
assertEquals(res.statusCode, 200);
assertEquals(requestHeaders.get("content-length"), "11");
assertEquals(requestHeaders.has("transfer-encoding"), false);
assertEquals(requestBody, "hello world");
});
req.on("socket", (socket) => {
assert(socket.writable);
assert(socket.readable);
socket.setKeepAlive();
socket.destroy();
socket.setTimeout(100);
});
req.write("hello ");
req.write("world");
req.end();
// await Promise.all([
// servePromise,
// // wait 100ms because of the socket.setTimeout(100) above
// // in order to not cause a flaky test sanitizer failure
// await new Promise((resolve) => setTimeout(resolve, 100)),
// ]);
// });
await Promise.all([
servePromise,
// wait 100ms because of the socket.setTimeout(100) above
// in order to not cause a flaky test sanitizer failure
await new Promise((resolve) => setTimeout(resolve, 100)),
]);
},
);
Deno.test("[node/http] send request with chunked body", async () => {
let requestHeaders: Headers;
@ -665,7 +670,9 @@ Deno.test("[node/http] server unref", async () => {
assertEquals(statusCode, 0);
});
Deno.test("[node/http] ClientRequest handle non-string headers", async () => {
Deno.test("[node/http] ClientRequest handle non-string headers", {
ignore: true,
}, async () => {
// deno-lint-ignore no-explicit-any
let headers: any;
const { promise, resolve, reject } = Promise.withResolvers<void>();
@ -687,26 +694,31 @@ Deno.test("[node/http] ClientRequest handle non-string headers", async () => {
assertEquals(headers!["1"], "2");
});
Deno.test("[node/http] ClientRequest uses HTTP/1.1", async () => {
let body = "";
const { promise, resolve, reject } = Promise.withResolvers<void>();
const req = https.request("https://localhost:5545/http_version", {
method: "POST",
headers: { 1: 2 },
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
Deno.test(
"[node/http] ClientRequest uses HTTP/1.1",
{ ignore: true },
async () => {
let body = "";
const { promise, resolve, reject } = Promise.withResolvers<void>();
const req = https.request("https://localhost:5545/http_version", {
method: "POST",
headers: { 1: 2 },
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
resolve();
resp.on("end", () => {
resolve();
});
});
});
req.once("error", (e) => reject(e));
req.end();
await promise;
assertEquals(body, "HTTP/1.1");
});
req.once("error", (e) => reject(e));
req.end();
await promise;
console.log(body);
assertEquals(body, "HTTP/1.1");
},
);
Deno.test("[node/http] ClientRequest setTimeout", async () => {
let body = "";
@ -794,26 +806,30 @@ Deno.test("[node/http] ClientRequest PUT", async () => {
assertEquals(body, "hello world");
});
Deno.test("[node/http] ClientRequest search params", async () => {
let body = "";
const { promise, resolve, reject } = Promise.withResolvers<void>();
const req = http.request({
host: "localhost:4545",
path: "search_params?foo=bar",
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
Deno.test(
"[node/http] ClientRequest search params",
{ ignore: true },
async () => {
let body = "";
const { promise, resolve, reject } = Promise.withResolvers<void>();
const req = http.request({
host: "localhost:4545",
path: "search_params?foo=bar",
}, (resp) => {
resp.on("data", (chunk) => {
body += chunk;
});
resp.on("end", () => {
resolve();
resp.on("end", () => {
resolve();
});
});
});
req.once("error", (e) => reject(e));
req.end();
await promise;
assertEquals(body, "foo=bar");
});
req.once("error", (e) => reject(e));
req.end();
await promise;
assertEquals(body, "foo=bar");
},
);
Deno.test("[node/http] HTTPS server", async () => {
const deferred = Promise.withResolvers<void>();
@ -850,7 +866,7 @@ Deno.test("[node/http] HTTPS server", async () => {
Deno.test(
"[node/http] client upgrade",
{ permissions: { net: true } },
{ permissions: { net: true }, ignore: true },
async () => {
const { promise: serverClosed, resolve: resolveServer } = Promise
.withResolvers<void>();
@ -1009,6 +1025,7 @@ Deno.test(
Deno.test(
"[node/http] client destroy before sending request should not error",
{ ignore: true },
() => {
const request = http.request("http://localhost:5929/");
// Calling this would throw
@ -1018,6 +1035,7 @@ Deno.test(
Deno.test(
"[node/http] destroyed requests should not be sent",
{ ignore: true },
async () => {
let receivedRequest = false;
const server = Deno.serve(() => {
@ -1058,24 +1076,28 @@ Deno.test("[node/https] node:https exports globalAgent", async () => {
);
});
Deno.test("[node/http] node:http request.setHeader(header, null) doesn't throw", () => {
{
const req = http.request("http://localhost:4545/");
req.on("error", () => {});
// @ts-expect-error - null is not a valid header value
req.setHeader("foo", null);
req.end();
req.destroy();
}
{
const req = https.request("https://localhost:4545/");
req.on("error", () => {});
// @ts-expect-error - null is not a valid header value
req.setHeader("foo", null);
req.end();
req.destroy();
}
});
Deno.test(
"[node/http] node:http request.setHeader(header, null) doesn't throw",
{ ignore: true },
() => {
{
const req = http.request("http://localhost:4545/");
req.on("error", () => {});
// @ts-expect-error - null is not a valid header value
req.setHeader("foo", null);
req.end();
req.destroy();
}
{
const req = https.request("https://localhost:4545/");
req.on("error", () => {});
// @ts-expect-error - null is not a valid header value
req.setHeader("foo", null);
req.end();
req.destroy();
}
},
);
Deno.test("[node/http] ServerResponse getHeader", async () => {
const { promise, resolve } = Promise.withResolvers<void>();
@ -1415,7 +1437,9 @@ Deno.test("[node/http] client closing a streaming request doesn't terminate serv
clearInterval(interval!);
});
Deno.test("[node/http] http.request() post streaming body works", async () => {
Deno.test("[node/http] http.request() post streaming body works", {
ignore: true,
}, async () => {
const server = http.createServer((req, res) => {
if (req.method === "POST") {
let receivedBytes = 0;
@ -1608,12 +1632,15 @@ Deno.test("[node/http] In ClientRequest, option.hostname has precedence over opt
await responseReceived.promise;
});
Deno.test("[node/http] upgraded socket closes when the server closed without closing handshake", async () => {
const clientSocketClosed = Promise.withResolvers<void>();
const serverProcessClosed = Promise.withResolvers<void>();
Deno.test(
"[node/http] upgraded socket closes when the server closed without closing handshake",
{ ignore: true },
async () => {
const clientSocketClosed = Promise.withResolvers<void>();
const serverProcessClosed = Promise.withResolvers<void>();
// Uses the server in different process to shutdown it without closing handshake
const server = `
// Uses the server in different process to shutdown it without closing handshake
const server = `
Deno.serve({ port: 1337 }, (req) => {
if (req.headers.get("upgrade") != "websocket") {
return new Response("ok");
@ -1628,49 +1655,50 @@ Deno.test("[node/http] upgraded socket closes when the server closed without clo
});
`;
const p = new Deno.Command("deno", { args: ["eval", server] }).spawn();
const p = new Deno.Command("deno", { args: ["eval", server] }).spawn();
// Wait for the server to respond
await retry(async () => {
const resp = await fetch("http://localhost:1337");
const _text = await resp.text();
});
const options = {
port: 1337,
host: "127.0.0.1",
headers: {
"Connection": "Upgrade",
"Upgrade": "websocket",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
},
};
http.request(options).on("upgrade", (_res, socket) => {
socket.on("close", () => {
console.log("client socket closed");
clientSocketClosed.resolve();
// Wait for the server to respond
await retry(async () => {
const resp = await fetch("http://localhost:1337");
const _text = await resp.text();
});
socket.on("data", async (data) => {
// receives pong message
assertEquals(data, Buffer.from("8104706f6e67", "hex"));
p.kill();
await p.status;
const options = {
port: 1337,
host: "127.0.0.1",
headers: {
"Connection": "Upgrade",
"Upgrade": "websocket",
"Sec-WebSocket-Key": "dGhlIHNhbXBsZSBub25jZQ==",
},
};
console.log("process closed");
serverProcessClosed.resolve();
http.request(options).on("upgrade", (_res, socket) => {
socket.on("close", () => {
console.log("client socket closed");
clientSocketClosed.resolve();
});
socket.on("data", async (data) => {
// receives pong message
assertEquals(data, Buffer.from("8104706f6e67", "hex"));
// sending some additional message
p.kill();
await p.status;
console.log("process closed");
serverProcessClosed.resolve();
// sending some additional message
socket.write(Buffer.from("81847de88e01", "hex"));
socket.write(Buffer.from("0d81e066", "hex"));
});
// sending ping message
socket.write(Buffer.from("81847de88e01", "hex"));
socket.write(Buffer.from("0d81e066", "hex"));
});
}).end();
// sending ping message
socket.write(Buffer.from("81847de88e01", "hex"));
socket.write(Buffer.from("0d81e066", "hex"));
}).end();
await clientSocketClosed.promise;
await serverProcessClosed.promise;
});
await clientSocketClosed.promise;
await serverProcessClosed.promise;
},
);