diff options
author | Yusuke Sakurai <kerokerokerop@gmail.com> | 2020-02-11 01:38:48 +0900 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-10 11:38:48 -0500 |
commit | e6f204199b1a1f6c5d963da2b8cdfc5992656147 (patch) | |
tree | 247896064fd1b9dc0edee5a5f373806da3d76049 /std/http/server_test.ts | |
parent | e8f639ce53c5a232c84d499b0d6375a2d2ad7131 (diff) |
feat: Support HTTP trailer headers for response (#3938)
Diffstat (limited to 'std/http/server_test.ts')
-rw-r--r-- | std/http/server_test.ts | 92 |
1 files changed, 90 insertions, 2 deletions
diff --git a/std/http/server_test.ts b/std/http/server_test.ts index c14d63145..10b0fec20 100644 --- a/std/http/server_test.ts +++ b/std/http/server_test.ts @@ -8,14 +8,21 @@ const { Buffer } = Deno; import { TextProtoReader } from "../textproto/mod.ts"; import { test, runIfMain } from "../testing/mod.ts"; -import { assert, assertEquals, assertNotEquals } from "../testing/asserts.ts"; +import { + assert, + assertEquals, + assertNotEquals, + assertThrowsAsync, + AssertionError +} from "../testing/asserts.ts"; import { Response, ServerRequest, writeResponse, serve, readRequest, - parseHTTPVersion + parseHTTPVersion, + writeTrailers } from "./server.ts"; import { BufReader, @@ -426,6 +433,35 @@ test(async function writeStringReaderResponse(): Promise<void> { assertEquals(r.more, false); }); +test("writeResponse with trailer", async () => { + const w = new Buffer(); + const body = new StringReader("Hello"); + await writeResponse(w, { + status: 200, + headers: new Headers({ + "transfer-encoding": "chunked", + trailer: "deno,node" + }), + body, + trailers: () => new Headers({ deno: "land", node: "js" }) + }); + const ret = w.toString(); + const exp = [ + "HTTP/1.1 200 OK", + "transfer-encoding: chunked", + "trailer: deno,node", + "", + "5", + "Hello", + "0", + "", + "deno: land", + "node: js", + "" + ].join("\r\n"); + assertEquals(ret, exp); +}); + test(async function readRequestError(): Promise<void> { const input = `GET / HTTP/1.1 malformedHeader @@ -733,4 +769,56 @@ if (Deno.build.os !== "win") { }); } +test("writeTrailer", async () => { + const w = new Buffer(); + await writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: "deno,node" }), + new Headers({ deno: "land", node: "js" }) + ); + assertEquals(w.toString(), "deno: land\r\nnode: js\r\n"); +}); + +test("writeTrailer should throw", async () => { + const w = new Buffer(); + await assertThrowsAsync( + () => { + return writeTrailers(w, new Headers(), new Headers()); + }, + Error, + 'must have "trailer"' + ); + await assertThrowsAsync( + () => { + return writeTrailers(w, new Headers({ trailer: "deno" }), new Headers()); + }, + Error, + "only allowed" + ); + for (const f of ["content-length", "trailer", "transfer-encoding"]) { + await assertThrowsAsync( + () => { + return writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: f }), + new Headers({ [f]: "1" }) + ); + }, + AssertionError, + "prohibited" + ); + } + await assertThrowsAsync( + () => { + return writeTrailers( + w, + new Headers({ "transfer-encoding": "chunked", trailer: "deno" }), + new Headers({ node: "js" }) + ); + }, + AssertionError, + "Not trailer" + ); +}); + runIfMain(import.meta); |