summaryrefslogtreecommitdiff
path: root/http/server_test.ts
diff options
context:
space:
mode:
authorJun Kato <i@junkato.jp>2019-05-11 01:55:18 +0900
committerRyan Dahl <ry@tinyclouds.org>2019-05-14 17:28:15 -0400
commit7cc80064f0742c9069310fbe8fb42fdf58b7df8d (patch)
tree61f837c73bf0eec8e726b7e50a68995b52e73631 /http/server_test.ts
parent1b359f4106891f2f07c334da683822070b24374f (diff)
fix(http): Skip 0 chunks when writing body (denoland/deno_std#387)
Original: https://github.com/denoland/deno_std/commit/79d5f556ba22d6fa9426825234cc8c49d37caf2c
Diffstat (limited to 'http/server_test.ts')
-rw-r--r--http/server_test.ts68
1 files changed, 66 insertions, 2 deletions
diff --git a/http/server_test.ts b/http/server_test.ts
index 36d704a4d..82a368395 100644
--- a/http/server_test.ts
+++ b/http/server_test.ts
@@ -6,10 +6,11 @@
// https://github.com/golang/go/blob/master/src/net/http/responsewrite_test.go
const { Buffer } = Deno;
-import { test } from "../testing/mod.ts";
+import { test, runIfMain } from "../testing/mod.ts";
import { assertEquals } from "../testing/asserts.ts";
-import { Response, ServerRequest } from "./server.ts";
+import { Response, ServerRequest, writeResponse } from "./server.ts";
import { BufReader, BufWriter } from "../io/bufio.ts";
+import { StringReader } from "../io/readers.ts";
interface ResponseTest {
response: Response;
@@ -261,3 +262,66 @@ test(async function requestBodyStreamWithTransferEncoding(): Promise<void> {
}
}
});
+
+test(async function writeUint8ArrayResponse(): Promise<void> {
+ const shortText = "Hello";
+
+ const body = new TextEncoder().encode(shortText);
+ const res: Response = { body };
+
+ const buf = new Deno.Buffer();
+ await writeResponse(buf, res);
+
+ const decoder = new TextDecoder("utf-8");
+ const reader = new BufReader(buf);
+
+ let line: Uint8Array;
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), "HTTP/1.1 200 OK");
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), `content-length: ${shortText.length}`);
+
+ line = (await reader.readLine())[0];
+ assertEquals(line.byteLength, 0);
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), shortText);
+
+ line = (await reader.readLine())[0];
+ assertEquals(line.byteLength, 0);
+});
+
+test(async function writeStringReaderResponse(): Promise<void> {
+ const shortText = "Hello";
+
+ const body = new StringReader(shortText);
+ const res: Response = { body };
+
+ const buf = new Deno.Buffer();
+ await writeResponse(buf, res);
+
+ const decoder = new TextDecoder("utf-8");
+ const reader = new BufReader(buf);
+
+ let line: Uint8Array;
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), "HTTP/1.1 200 OK");
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), "transfer-encoding: chunked");
+
+ line = (await reader.readLine())[0];
+ assertEquals(line.byteLength, 0);
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), shortText.length.toString());
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), shortText);
+
+ line = (await reader.readLine())[0];
+ assertEquals(decoder.decode(line), "0");
+});
+
+runIfMain(import.meta);