deno.land / std@0.91.0 / textproto / test.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.// Based on https://github.com/golang/go/blob/master/src/net/textproto/reader_test.go// 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 } from "./mod.ts";import { StringReader } from "../io/readers.ts";import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
function reader(s: string): TextProtoReader { return new TextProtoReader(new BufReader(new StringReader(s)));}
Deno.test({ 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(); },});
Deno.test("[textproto] ReadEmpty", async () => { const r = reader(""); const m = await r.readMIMEHeader(); assertEquals(m, null);});
Deno.test("[textproto] Reader", async () => { 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(); assert(s === null);});
Deno.test({ 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); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("My-Key"), "Value 1, Value 2"); assertEquals(m.get("Long-key"), "Even Longer Value"); },});
Deno.test({ name: "[textproto] Reader : MIME Header Single", async fn(): Promise<void> { const input = "Foo: bar\n\n"; const r = reader(input); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("Foo"), "bar"); },});
Deno.test({ name: "[textproto] Reader : MIME Header No Key", async fn(): Promise<void> { const input = ": bar\ntest-1: 1\n\n"; const r = reader(input); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("Test-1"), "1"); },});
Deno.test({ 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`); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("Cookie"), sdata); },});
// Test that we don't read MIME headers seen in the wild,// with spaces before colons, and spaces in keys.Deno.test({ name: "[textproto] Reader : MIME Header Non compliant", async fn(): Promise<void> { const input = "Foo: bar\r\n" + "Content-Language: en\r\n" + "SID : 0\r\n" + "Audio Mode : None\r\n" + "Privilege : 127\r\n\r\n"; const r = reader(input); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("Foo"), "bar"); assertEquals(m.get("Content-Language"), "en"); // Make sure we drop headers with trailing whitespace assertEquals(m.get("SID"), null); assertEquals(m.get("Privilege"), null); // Not legal http header assertThrows((): void => { assertEquals(m.get("Audio Mode"), "None"); }); },});
Deno.test({ 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", "Foo: foo\r\nNo colon second line\r\n\r\n", ]; const r = reader(input.join(""));
let err; try { await r.readMIMEHeader(); } catch (e) { err = e; } assert(err instanceof Deno.errors.InvalidData); },});
Deno.test({ name: "[textproto] Reader : MIME Header Trim Continued", async fn(): Promise<void> { const input = "a:\n" + " 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); },});
Deno.test({ 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", "------WebKitFormBoundaryimeZ2Le9LjohiUiG--\r\n\n", ]; const r = reader(input.join("")); const m = await r.readMIMEHeader(); assert(m !== null); assertEquals(m.get("Accept"), "*/*"); assertEquals(m.get("Content-Disposition"), 'form-data; name="test"'); },});
Deno.test({ name: "[textproto] #4521 issue", async fn() { const input = "abcdefghijklmnopqrstuvwxyz"; const bufSize = 25; const tp = new TextProtoReader( new BufReader(new StringReader(input), bufSize), ); const line = await tp.readLine(); assertEquals(line, input); },});
std

Version Info

Tagged at
3 years ago

External Dependencies

No external dependencies 🎉