summaryrefslogtreecommitdiff
path: root/textproto/test.ts
diff options
context:
space:
mode:
Diffstat (limited to 'textproto/test.ts')
-rw-r--r--textproto/test.ts98
1 files changed, 98 insertions, 0 deletions
diff --git a/textproto/test.ts b/textproto/test.ts
new file mode 100644
index 000000000..0f8bee227
--- /dev/null
+++ b/textproto/test.ts
@@ -0,0 +1,98 @@
+// Based on https://github.com/golang/go/blob/891682/src/net/textproto/
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+import { BufReader } from "../io/bufio.ts";
+import { TextProtoReader, append } from "./mod.ts";
+import { stringsReader } from "../io/util.ts";
+import { test, assert, assertEqual } from "../testing/mod.ts";
+
+function reader(s: string): TextProtoReader {
+ return new TextProtoReader(new BufReader(stringsReader(s)));
+}
+
+test(async function textprotoReader() {
+ let r = reader("line1\nline2\n");
+ let [s, err] = await r.readLine();
+ assertEqual(s, "line1");
+ assert(err == null);
+
+ [s, err] = await r.readLine();
+ assertEqual(s, "line2");
+ assert(err == null);
+
+ [s, err] = await r.readLine();
+ assertEqual(s, "");
+ assert(err == "EOF");
+});
+
+/*
+test(async function textprotoReadMIMEHeader() {
+ let r = reader("my-key: Value 1 \r\nLong-key: Even \n Longer Value\r\nmy-Key: Value 2\r\n\n");
+ let [m, err] = await r.readMIMEHeader();
+
+ console.log("Got headers", m.toString());
+ want := MIMEHeader{
+ "My-Key": {"Value 1", "Value 2"},
+ "Long-Key": {"Even Longer Value"},
+ }
+ if !reflect.DeepEqual(m, want) || err != nil {
+ t.Fatalf("ReadMIMEHeader: %v, %v; want %v", m, err, want)
+ }
+});
+*/
+
+test(async function textprotoReadMIMEHeaderSingle() {
+ let r = reader("Foo: bar\n\n");
+ let [m, err] = await r.readMIMEHeader();
+ assertEqual(m.get("Foo"), "bar");
+ assert(!err);
+});
+
+// Test that we read slightly-bogus MIME headers seen in the wild,
+// with spaces before colons, and spaces in keys.
+test(async function textprotoReadMIMEHeaderNonCompliant() {
+ // Invalid HTTP response header as sent by an Axis security
+ // camera: (this is handled by IE, Firefox, Chrome, curl, etc.)
+ let r = reader(
+ "Foo: bar\r\n" +
+ "Content-Language: en\r\n" +
+ "SID : 0\r\n" +
+ // TODO Re-enable Currently fails with:
+ // "TypeError: audio mode is not a legal HTTP header name"
+ // "Audio Mode : None\r\n" +
+ "Privilege : 127\r\n\r\n"
+ );
+ let [m, err] = await r.readMIMEHeader();
+ console.log(m.toString());
+ assert(!err);
+ /*
+ let want = MIMEHeader{
+ "Foo": {"bar"},
+ "Content-Language": {"en"},
+ "Sid": {"0"},
+ "Audio Mode": {"None"},
+ "Privilege": {"127"},
+ }
+ if !reflect.DeepEqual(m, want) || err != nil {
+ t.Fatalf("ReadMIMEHeader =\n%v, %v; want:\n%v", m, err, want)
+ }
+ */
+});
+
+test(async function textprotoAppend() {
+ const enc = new TextEncoder();
+ const dec = new TextDecoder();
+ const u1 = enc.encode("Hello ");
+ const u2 = enc.encode("World");
+ const joined = append(u1, u2);
+ assertEqual(dec.decode(joined), "Hello World");
+});
+
+test(async function textprotoReadEmpty() {
+ let r = reader("");
+ let [m, err] = await r.readMIMEHeader();
+ // Should not crash!
+ assertEqual(err, "EOF");
+});