diff options
Diffstat (limited to 'std/http/server_test.ts')
-rw-r--r-- | std/http/server_test.ts | 41 |
1 files changed, 39 insertions, 2 deletions
diff --git a/std/http/server_test.ts b/std/http/server_test.ts index b4d86c468..d3f36a1d3 100644 --- a/std/http/server_test.ts +++ b/std/http/server_test.ts @@ -6,8 +6,13 @@ // https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go import { TextProtoReader } from "../textproto/mod.ts"; -import { assert, assertEquals, assertNotEOF } from "../testing/asserts.ts"; -import { Response, ServerRequest, serve } from "./server.ts"; +import { + assert, + assertEquals, + assertNotEOF, + assertStrContains +} 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 { encode, decode } from "../strings/mod.ts"; @@ -451,6 +456,38 @@ test("close server while iterating", async (): Promise<void> => { assertEquals(await nextAfterClosing, { value: undefined, done: true }); }); +test({ + name: "[http] close server while connection is open", + async fn(): Promise<void> { + async function iteratorReq(server: Server): Promise<void> { + for await (const req of server) { + req.respond({ body: new TextEncoder().encode(req.url) }); + } + } + + const server = serve(":8123"); + iteratorReq(server); + const conn = await Deno.connect({ hostname: "127.0.0.1", port: 8123 }); + await Deno.writeAll( + conn, + new TextEncoder().encode("GET /hello HTTP/1.1\r\n\r\n") + ); + const res = new Uint8Array(100); + const nread = await conn.read(res); + assert(nread !== Deno.EOF); + const resStr = new TextDecoder().decode(res.subarray(0, nread)); + assertStrContains(resStr, "/hello"); + server.close(); + // Defer to allow async ops to resolve after server has been closed. + await delay(0); + // Client connection should still be open, verify that + // it's visible in resource table. + const resources = Deno.resources(); + assertEquals(resources[conn.rid], "tcpStream"); + conn.close(); + } +}); + // 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). |