Skip to content

Commit 3a48a22

Browse files
authored
Fetch: parsing Content-Length
For whatwg/fetch#1183.
1 parent 964d489 commit 3a48a22

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

Diff for: fetch/content-length/parsing.window.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
promise_test(() => {
2+
return fetch("resources/content-lengths.json").then(res => res.json()).then(runTests);
3+
}, "Loading JSON…");
4+
5+
function runTests(testUnits) {
6+
testUnits.forEach(testUnit => {
7+
const input = encodeURIComponent(testUnit.input);
8+
promise_test(t => {
9+
const result = fetch("resources/content-length.py?length=" + input);
10+
if (testUnit.output === null) {
11+
return promise_rejects_js(t, TypeError, result);
12+
} else {
13+
return result.then(res => res.text()).then(text => {
14+
assert_equals(text.length, testUnit.output);
15+
});
16+
}
17+
}, input);
18+
});
19+
}

Diff for: fetch/content-length/resources/content-length.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def main(request, response):
2+
response.add_required_headers = False
3+
output = b"HTTP/1.1 200 OK\r\n"
4+
output += b"Content-Type: text/plain;charset=UTF-8\r\n"
5+
output += request.GET.first(b"length") + b"\r\n"
6+
output += b"\r\n"
7+
output += b"Fact: this is really forty-two bytes long."
8+
response.writer.write(output)
9+
response.close_connection = True

Diff for: fetch/content-length/resources/content-lengths.json

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
[
2+
{
3+
"input": "Content-Length: 42",
4+
"output": 42
5+
},
6+
{
7+
"input": "Content-Length: 42,42",
8+
"output": 42
9+
},
10+
{
11+
"input": "Content-Length: 42\r\nContent-Length: 42",
12+
"output": 42
13+
},
14+
{
15+
"input": "Content-Length: 42\r\nContent-Length: 42,42",
16+
"output": 42
17+
},
18+
{
19+
"input": "Content-Length: 30",
20+
"output": 30
21+
},
22+
{
23+
"input": "Content-Length: 30,30",
24+
"output": 30
25+
},
26+
{
27+
"input": "Content-Length: 30\r\nContent-Length: 30",
28+
"output": 30
29+
},
30+
{
31+
"input": "Content-Length: 30\r\nContent-Length: 30,30",
32+
"output": 30
33+
},
34+
{
35+
"input": "Content-Length: 42,30",
36+
"output": null
37+
},
38+
{
39+
"input": "Content-Length: 30,42",
40+
"output": null
41+
},
42+
{
43+
"input": "Content-Length: 42\r\nContent-Length: 30",
44+
"output": null
45+
},
46+
{
47+
"input": "Content-Length: 30\r\nContent-Length: 42",
48+
"output": null
49+
},
50+
{
51+
"input": "Content-Length: 30,",
52+
"output": null
53+
},
54+
{
55+
"input": "Content-Length: ,30",
56+
"output": null
57+
},
58+
{
59+
"input": "Content-Length: 30\r\nContent-Length: \t",
60+
"output": null
61+
},
62+
{
63+
"input": "Content-Length: \r\nContent-Length: 30",
64+
"output": null
65+
},
66+
{
67+
"input": "Content-Length: aaaah\r\nContent-Length: nah",
68+
"output": null
69+
},
70+
{
71+
"input": "Content-Length: aaaah, nah",
72+
"output": null
73+
},
74+
{
75+
"input": "Content-Length: aaaah\r\nContent-Length: aaaah",
76+
"output": 42
77+
},
78+
{
79+
"input": "Content-Length: aaaah, aaaah",
80+
"output": 42
81+
},
82+
{
83+
"input": "Content-Length: aaaah",
84+
"output": 42
85+
},
86+
{
87+
"input": "Content-Length: 42s",
88+
"output": 42
89+
},
90+
{
91+
"input": "Content-Length: 30s",
92+
"output": 42
93+
},
94+
{
95+
"input": "Content-Length: -1",
96+
"output": 42
97+
},
98+
{
99+
"input": "Content-Length: 0x20",
100+
"output": 42
101+
},
102+
{
103+
"input": "Content-Length: 030",
104+
"output": 30
105+
},
106+
{
107+
"input": "Content-Length: 030\r\nContent-Length: 30",
108+
"output": null
109+
},
110+
{
111+
"input": "Content-Length: 030, 30",
112+
"output": null
113+
},
114+
{
115+
"input": "Content-Length: \"30\"",
116+
"output": 42
117+
},
118+
{
119+
"input": "Content-Length:30\r\nContent-Length:,\r\nContent-Length:30",
120+
"output": null
121+
},
122+
{
123+
"input": "Content-Length: ",
124+
"output": 42
125+
}
126+
]

Diff for: fetch/content-length/too-long.window.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
promise_test(async t => {
2+
const result = await fetch(`resources/content-length.py?length=${encodeURIComponent("Content-Length: 50")}`);
3+
await promise_rejects_js(t, TypeError, result.text());
4+
}, "Content-Length header value of network response exceeds response body");

0 commit comments

Comments
 (0)