fix(ext/http): reading headers with ongoing body reader (#15161)

This commit is contained in:
Divy Srivastava 2022-07-12 23:01:37 +05:30 committed by GitHub
parent dcd05d4cb9
commit 5b26a4a30e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 77 additions and 9 deletions

View file

@ -73,6 +73,68 @@ Deno.test({ permissions: { net: true } }, async function httpServerBasic() {
await promise;
});
// https://github.com/denoland/deno/issues/15107
Deno.test(
{ permissions: { net: true } },
async function httpLazyHeadersIssue15107() {
let headers: Headers;
const promise = (async () => {
const listener = Deno.listen({ port: 2333 });
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const e = await httpConn.nextRequest();
assert(e);
const { request } = e;
request.text();
headers = request.headers;
httpConn.close();
})();
const conn = await Deno.connect({ port: 2333 });
// Send GET request with a body + content-length.
const encoder = new TextEncoder();
const body =
`GET / HTTP/1.1\r\nHost: 127.0.0.1:2333\r\nContent-Length: 5\r\n\r\n12345`;
const writeResult = await conn.write(encoder.encode(body));
assertEquals(body.length, writeResult);
await promise;
conn.close();
assertEquals(headers!.get("content-length"), "5");
},
);
Deno.test(
{ permissions: { net: true } },
async function httpReadHeadersAfterClose() {
const promise = (async () => {
const listener = Deno.listen({ port: 2334 });
const conn = await listener.accept();
listener.close();
const httpConn = Deno.serveHttp(conn);
const e = await httpConn.nextRequest();
assert(e);
const { request, respondWith } = e;
await request.text(); // Read body
await respondWith(new Response("Hello World")); // Closes request
assertThrows(() => request.headers, TypeError, "request closed");
httpConn.close();
})();
const conn = await Deno.connect({ port: 2334 });
// Send GET request with a body + content-length.
const encoder = new TextEncoder();
const body =
`GET / HTTP/1.1\r\nHost: 127.0.0.1:2333\r\nContent-Length: 5\r\n\r\n12345`;
const writeResult = await conn.write(encoder.encode(body));
assertEquals(body.length, writeResult);
await promise;
conn.close();
},
);
Deno.test(
{ permissions: { net: true } },
async function httpServerGetRequestBody() {