2018-11-08 12:58:43 -05:00
|
|
|
// 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 "./bufio.ts";
|
2018-12-17 23:14:22 -05:00
|
|
|
import { TextProtoReader, append } from "./textproto.ts";
|
2018-12-17 22:40:42 -05:00
|
|
|
import { stringsReader } from "./util.ts";
|
2018-11-08 12:58:43 -05:00
|
|
|
import {
|
|
|
|
test,
|
|
|
|
assert,
|
|
|
|
assertEqual
|
2019-01-01 22:46:17 -05:00
|
|
|
} from "../testing/mod.ts";
|
2018-11-08 12:58:43 -05:00
|
|
|
|
|
|
|
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" +
|
2018-12-23 18:49:46 -05:00
|
|
|
// TODO Re-enable Currently fails with:
|
|
|
|
// "TypeError: audio mode is not a legal HTTP header name"
|
|
|
|
// "Audio Mode : None\r\n" +
|
2018-11-08 12:58:43 -05:00
|
|
|
"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)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
});
|
2018-12-17 23:14:22 -05:00
|
|
|
|
|
|
|
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");
|
|
|
|
});
|
2018-12-31 04:00:28 -05:00
|
|
|
|
|
|
|
test(async function textprotoReadEmpty() {
|
|
|
|
let r = reader("");
|
|
|
|
let [m, err] = await r.readMIMEHeader();
|
|
|
|
// Should not crash!
|
|
|
|
assertEqual(err, "EOF");
|
|
|
|
});
|