mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 15:49:44 -05:00
fix(std/http): Fix respond error test on Windows (#4408)
This commit is contained in:
parent
a6d8098b35
commit
b7e6a31a42
3 changed files with 15 additions and 50 deletions
|
@ -242,7 +242,7 @@ export async function writeResponse(
|
|||
const statusText = STATUS_TEXT.get(statusCode);
|
||||
const writer = BufWriter.create(w);
|
||||
if (!statusText) {
|
||||
throw Error("bad status code");
|
||||
throw new Deno.errors.InvalidData("Bad status code");
|
||||
}
|
||||
if (!r.body) {
|
||||
r.body = new Uint8Array();
|
||||
|
|
|
@ -10,11 +10,12 @@ import {
|
|||
assert,
|
||||
assertEquals,
|
||||
assertNotEOF,
|
||||
assertStrContains
|
||||
assertStrContains,
|
||||
assertThrowsAsync
|
||||
} from "../testing/asserts.ts";
|
||||
import { Response, ServerRequest, Server, serve } from "./server.ts";
|
||||
import { BufReader, BufWriter } from "../io/bufio.ts";
|
||||
import { delay, deferred } from "../util/async.ts";
|
||||
import { delay } from "../util/async.ts";
|
||||
import { encode, decode } from "../strings/mod.ts";
|
||||
import { mockConn } from "./mock.ts";
|
||||
|
||||
|
@ -488,57 +489,23 @@ test({
|
|||
}
|
||||
});
|
||||
|
||||
// TODO(kevinkassimo): create a test that works on Windows.
|
||||
// The following test is to ensure that if an error occurs during respond
|
||||
// would result in connection closed. (such that fd/resource is freed).
|
||||
// On *nix, a delayed second attempt to write to a CLOSE_WAIT connection would
|
||||
// receive a RST and thus trigger an error during response for us to test.
|
||||
// We need to find a way to similarly trigger an error on Windows so that
|
||||
// we can test if connection is closed.
|
||||
test({
|
||||
ignore: Deno.build.os == "win",
|
||||
name: "respond error handling",
|
||||
name: "respond error closes connection",
|
||||
async fn(): Promise<void> {
|
||||
const connClosedPromise = deferred();
|
||||
const serverRoutine = async (): Promise<void> => {
|
||||
let reqCount = 0;
|
||||
const server = serve(":8124");
|
||||
// @ts-ignore
|
||||
const serverRid = server.listener["rid"];
|
||||
let connRid = -1;
|
||||
for await (const req of server) {
|
||||
connRid = req.conn.rid;
|
||||
reqCount++;
|
||||
await Deno.readAll(req.body);
|
||||
await connClosedPromise;
|
||||
try {
|
||||
await assertThrowsAsync(async () => {
|
||||
await req.respond({
|
||||
status: 12345,
|
||||
body: new TextEncoder().encode("Hello World")
|
||||
});
|
||||
await delay(100);
|
||||
req.done = deferred();
|
||||
// This duplicate respond is to ensure we get a write failure from the
|
||||
// other side. Our client would enter CLOSE_WAIT stage after close(),
|
||||
// meaning first server .send (.respond) after close would still work.
|
||||
// However, a second send would fail under RST, which is similar
|
||||
// to the scenario where a failure happens during .respond
|
||||
await req.respond({
|
||||
body: new TextEncoder().encode("Hello World")
|
||||
});
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}, Deno.errors.InvalidData);
|
||||
// The connection should be destroyed
|
||||
assert(!(req.conn.rid in Deno.resources()));
|
||||
server.close();
|
||||
}
|
||||
server.close();
|
||||
// Let event loop do another turn so server
|
||||
// finishes all pending ops.
|
||||
await delay(0);
|
||||
const resources = Deno.resources();
|
||||
assert(reqCount === 1);
|
||||
// Server should be gone
|
||||
assert(!(serverRid in resources));
|
||||
// The connection should be destroyed
|
||||
assert(!(connRid in resources));
|
||||
};
|
||||
const p = serverRoutine();
|
||||
const conn = await Deno.connect({
|
||||
|
@ -549,9 +516,7 @@ test({
|
|||
conn,
|
||||
new TextEncoder().encode("GET / HTTP/1.1\r\n\r\n")
|
||||
);
|
||||
conn.close(); // abruptly closing connection before response.
|
||||
// conn on server side enters CLOSE_WAIT state.
|
||||
connClosedPromise.resolve();
|
||||
conn.close();
|
||||
await p;
|
||||
}
|
||||
});
|
||||
|
|
|
@ -348,9 +348,9 @@ export async function assertThrowsAsync(
|
|||
await fn();
|
||||
} catch (e) {
|
||||
if (ErrorClass && !(Object.getPrototypeOf(e) === ErrorClass.prototype)) {
|
||||
msg = `Expected error to be instance of "${ErrorClass.name}"${
|
||||
msg ? `: ${msg}` : "."
|
||||
}`;
|
||||
msg = `Expected error to be instance of "${ErrorClass.name}", but got "${
|
||||
e.name
|
||||
}"${msg ? `: ${msg}` : "."}`;
|
||||
throw new AssertionError(msg);
|
||||
}
|
||||
if (msgIncludes && !e.message.includes(msgIncludes)) {
|
||||
|
|
Loading…
Reference in a new issue