|
2 | 2 |
|
3 | 3 | // deno-lint-ignore-file no-console |
4 | 4 |
|
5 | | -import { assertMatch, assertRejects } from "@std/assert"; |
| 5 | +import { assertIsError, assertMatch, assertRejects } from "@std/assert"; |
6 | 6 | import { Buffer, BufReader, BufWriter, type Reader } from "@std/io"; |
7 | 7 | import { TextProtoReader } from "../testdata/run/textproto.ts"; |
8 | 8 | import { |
@@ -4378,3 +4378,46 @@ Deno.test( |
4378 | 4378 | await server.finished; |
4379 | 4379 | }, |
4380 | 4380 | ); |
| 4381 | + |
| 4382 | +Deno.test({ |
| 4383 | + name: |
| 4384 | + "req.body.getReader().read() throws the error with reasonable error message", |
| 4385 | +}, async () => { |
| 4386 | + const { promise, resolve, reject } = Promise.withResolvers<Error>(); |
| 4387 | + const server = Deno.serve({ onListen, port: 0 }, async (req) => { |
| 4388 | + const reader = req.body!.getReader(); |
| 4389 | + |
| 4390 | + try { |
| 4391 | + while (true) { |
| 4392 | + const { done } = await reader.read(); |
| 4393 | + if (done) break; |
| 4394 | + } |
| 4395 | + } catch (e) { |
| 4396 | + // deno-lint-ignore no-explicit-any |
| 4397 | + resolve(e as any); |
| 4398 | + } |
| 4399 | + |
| 4400 | + reject(new Error("Should not reach here")); |
| 4401 | + server.shutdown(); |
| 4402 | + return new Response(); |
| 4403 | + }); |
| 4404 | + |
| 4405 | + async function onListen({ port }: { port: number }) { |
| 4406 | + const body = "a".repeat(1000); |
| 4407 | + const request = `POST / HTTP/1.1\r\n` + |
| 4408 | + `Host: 127.0.0.1:${port}\r\n` + |
| 4409 | + `Content-Length: 1000\r\n` + |
| 4410 | + "\r\n" + body; |
| 4411 | + |
| 4412 | + const connection = await Deno.connect({ hostname: "127.0.0.1", port }); |
| 4413 | + await connection.write(new TextEncoder().encode(request)); |
| 4414 | + connection.close(); |
| 4415 | + } |
| 4416 | + await server.finished; |
| 4417 | + const e = await promise; |
| 4418 | + assertIsError( |
| 4419 | + e, |
| 4420 | + Deno.errors.BadResource, |
| 4421 | + "Cannot read request body as underlying resource unavailable", |
| 4422 | + ); |
| 4423 | +}); |
0 commit comments