2020-03-22 14:49:09 -04:00
|
|
|
// Based on https://github.com/golang/go/blob/master/src/net/textproto/reader_test.go
|
2018-11-08 12:58:43 -05:00
|
|
|
// 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.
|
2020-03-22 14:49:09 -04:00
|
|
|
import { BufReader } from "../io/bufio.ts";
|
|
|
|
import { TextProtoReader } from "./mod.ts";
|
2020-06-06 10:37:52 -04:00
|
|
|
import { StringReader } from "../io/readers.ts";
|
2020-04-28 12:40:43 -04:00
|
|
|
import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
|
2018-12-17 23:14:22 -05:00
|
|
|
|
2020-03-22 14:49:09 -04:00
|
|
|
function reader(s: string): TextProtoReader {
|
2020-06-06 10:37:52 -04:00
|
|
|
return new TextProtoReader(new BufReader(new StringReader(s)));
|
2020-03-22 14:49:09 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
ignore: true,
|
|
|
|
name: "[textproto] Reader : DotBytes",
|
|
|
|
fn(): Promise<void> {
|
|
|
|
const _input =
|
|
|
|
"dotlines\r\n.foo\r\n..bar\n...baz\nquux\r\n\r\n.\r\nanot.her\r\n";
|
|
|
|
return Promise.resolve();
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[textproto] ReadEmpty", async () => {
|
2020-03-22 14:49:09 -04:00
|
|
|
const r = reader("");
|
|
|
|
const m = await r.readMIMEHeader();
|
2020-04-28 12:40:43 -04:00
|
|
|
assertEquals(m, null);
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test("[textproto] Reader", async () => {
|
2020-03-22 14:49:09 -04:00
|
|
|
const r = reader("line1\nline2\n");
|
|
|
|
let s = await r.readLine();
|
|
|
|
assertEquals(s, "line1");
|
|
|
|
|
|
|
|
s = await r.readLine();
|
|
|
|
assertEquals(s, "line2");
|
|
|
|
|
|
|
|
s = await r.readLine();
|
2020-04-28 12:40:43 -04:00
|
|
|
assert(s === null);
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const input =
|
|
|
|
"my-key: Value 1 \r\nLong-key: Even Longer Value\r\nmy-Key: " +
|
|
|
|
"Value 2\r\n\n";
|
|
|
|
const r = reader(input);
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("My-Key"), "Value 1, Value 2");
|
|
|
|
assertEquals(m.get("Long-key"), "Even Longer Value");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header Single",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const input = "Foo: bar\n\n";
|
|
|
|
const r = reader(input);
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("Foo"), "bar");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header No Key",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const input = ": bar\ntest-1: 1\n\n";
|
|
|
|
const r = reader(input);
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("Test-1"), "1");
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : Large MIME Header",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const data: string[] = [];
|
|
|
|
// Go test is 16*1024. But seems it can't handle more
|
|
|
|
for (let i = 0; i < 1024; i++) {
|
|
|
|
data.push("x");
|
|
|
|
}
|
|
|
|
const sdata = data.join("");
|
|
|
|
const r = reader(`Cookie: ${sdata}\r\n\r\n`);
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("Cookie"), sdata);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-04-06 09:58:46 -04:00
|
|
|
// Test that we don't read MIME headers seen in the wild,
|
2020-03-22 14:49:09 -04:00
|
|
|
// with spaces before colons, and spaces in keys.
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header Non compliant",
|
|
|
|
async fn(): Promise<void> {
|
2020-07-14 15:24:17 -04:00
|
|
|
const input = "Foo: bar\r\n" +
|
2020-03-22 14:49:09 -04:00
|
|
|
"Content-Language: en\r\n" +
|
|
|
|
"SID : 0\r\n" +
|
|
|
|
"Audio Mode : None\r\n" +
|
|
|
|
"Privilege : 127\r\n\r\n";
|
|
|
|
const r = reader(input);
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("Foo"), "bar");
|
|
|
|
assertEquals(m.get("Content-Language"), "en");
|
2020-04-06 09:58:46 -04:00
|
|
|
// Make sure we drop headers with trailing whitespace
|
|
|
|
assertEquals(m.get("SID"), null);
|
|
|
|
assertEquals(m.get("Privilege"), null);
|
|
|
|
// Not legal http header
|
2020-03-22 14:49:09 -04:00
|
|
|
assertThrows((): void => {
|
|
|
|
assertEquals(m.get("Audio Mode"), "None");
|
|
|
|
});
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header Malformed",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const input = [
|
|
|
|
"No colon first line\r\nFoo: foo\r\n\r\n",
|
|
|
|
" No colon first line with leading space\r\nFoo: foo\r\n\r\n",
|
|
|
|
"\tNo colon first line with leading tab\r\nFoo: foo\r\n\r\n",
|
|
|
|
" First: line with leading space\r\nFoo: foo\r\n\r\n",
|
|
|
|
"\tFirst: line with leading tab\r\nFoo: foo\r\n\r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
"Foo: foo\r\nNo colon second line\r\n\r\n",
|
2020-03-22 14:49:09 -04:00
|
|
|
];
|
|
|
|
const r = reader(input.join(""));
|
|
|
|
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await r.readMIMEHeader();
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(err instanceof Deno.errors.InvalidData);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] Reader : MIME Header Trim Continued",
|
|
|
|
async fn(): Promise<void> {
|
2020-07-14 15:24:17 -04:00
|
|
|
const input = "a:\n" +
|
2020-03-22 14:49:09 -04:00
|
|
|
" 0 \r\n" +
|
|
|
|
"b:1 \t\r\n" +
|
|
|
|
"c: 2\r\n" +
|
|
|
|
" 3\t\n" +
|
|
|
|
" \t 4 \r\n\n";
|
|
|
|
const r = reader(input);
|
|
|
|
let err;
|
|
|
|
try {
|
|
|
|
await r.readMIMEHeader();
|
|
|
|
} catch (e) {
|
|
|
|
err = e;
|
|
|
|
}
|
|
|
|
assert(err instanceof Deno.errors.InvalidData);
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2020-03-22 14:49:09 -04:00
|
|
|
});
|
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-03-22 14:49:09 -04:00
|
|
|
name: "[textproto] #409 issue : multipart form boundary",
|
|
|
|
async fn(): Promise<void> {
|
|
|
|
const input = [
|
|
|
|
"Accept: */*\r\n",
|
|
|
|
'Content-Disposition: form-data; name="test"\r\n',
|
|
|
|
" \r\n",
|
2020-03-28 13:03:49 -04:00
|
|
|
"------WebKitFormBoundaryimeZ2Le9LjohiUiG--\r\n\n",
|
2020-03-22 14:49:09 -04:00
|
|
|
];
|
|
|
|
const r = reader(input.join(""));
|
2020-04-28 12:40:43 -04:00
|
|
|
const m = await r.readMIMEHeader();
|
|
|
|
assert(m !== null);
|
2020-03-22 14:49:09 -04:00
|
|
|
assertEquals(m.get("Accept"), "*/*");
|
|
|
|
assertEquals(m.get("Content-Disposition"), 'form-data; name="test"');
|
2020-03-28 13:03:49 -04:00
|
|
|
},
|
2018-12-17 23:14:22 -05:00
|
|
|
});
|
2020-05-20 10:34:20 -04:00
|
|
|
|
2020-06-12 15:23:38 -04:00
|
|
|
Deno.test({
|
2020-05-20 10:34:20 -04:00
|
|
|
name: "[textproto] #4521 issue",
|
|
|
|
async fn() {
|
|
|
|
const input = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
const bufSize = 25;
|
|
|
|
const tp = new TextProtoReader(
|
2020-07-14 15:24:17 -04:00
|
|
|
new BufReader(new StringReader(input), bufSize),
|
2020-05-20 10:34:20 -04:00
|
|
|
);
|
|
|
|
const line = await tp.readLine();
|
|
|
|
assertEquals(line, input);
|
|
|
|
},
|
|
|
|
});
|