Skip to content

Commit

Permalink
Test header values
Browse files Browse the repository at this point in the history
See whatwg/fetch#332 for context.

Once w3c/wptserve#111 is fixed these tests can be further updated.
  • Loading branch information
annevk authored Jan 17, 2017
1 parent 73f4ad3 commit b349224
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
3 changes: 1 addition & 2 deletions XMLHttpRequest/setrequestheader-bogus-value.htm
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
assert_throws("SyntaxError", function() { client.setRequestHeader("x-test", value) }, ' given value ' + value+', ');
});
}
try_value("t\x00t");
try_value("t\rt");
try_value("t\nt");
try_value("t\bt");
try_value("\x7f");
test(function() {
var client = new XMLHttpRequest();
client.open("GET", "...");
Expand Down
64 changes: 64 additions & 0 deletions fetch/api/headers/header-values-normalize.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!doctype html>
<meta charset=utf8>
<meta name=timeout content=long>
<title>Header value normalizing test</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
for(let i = 0; i < 0x21; i++) {
let fail = false,
strip = false

// REMOVE 0x0B/0x0C exception once https://github.com/w3c/wptserve/issues/111 is fixed
if(i === 0x0B || i === 0x0C)
continue

if(i === 0) {
fail = true
}

if(i === 0x09 || i === 0x0A || i === 0x0D || i === 0x20) {
strip = true
}

let val = String.fromCharCode(i),
expectedVal = strip ? "" : val
for(let y = 0; y < 3; y++) {
let useVal = val,
useExpectedVal = expectedVal
if(y === 0) {
useVal = "x" + val
useExpectedVal = "x" + expectedVal
} else if(y === 2) {
useVal = val + "x"
useExpectedVal = expectedVal + "x"
}

async_test((t) => {
let xhr = new XMLHttpRequest()
xhr.open("POST", "../resources/inspect-headers.py?headers=value-test")
if(fail) {
assert_throws("SyntaxError", () => xhr.setRequestHeader("value-test", useVal))
t.done()
} else {
xhr.setRequestHeader("value-test", useVal)
xhr.onload = t.step_func_done(() => {
assert_equals(xhr.getResponseHeader("x-request-value-test"), useExpectedVal)
})
xhr.send()
}
}, "XMLHttpRequest with value " + encodeURI(useVal))

promise_test((t) => {
if(fail) {
return promise_rejects(t, new TypeError(), fetch("about:blank", { headers: {"value-test": useVal} }))
} else {
return fetch("../resources/inspect-headers.py?headers=value-test", { headers: {"value-test": useVal} }).then((res) => {
assert_equals(res.headers.get("x-request-value-test"), useExpectedVal)
})
}
}, "fetch() with value " + encodeURI(useVal))
}
}
</script>
41 changes: 41 additions & 0 deletions fetch/api/headers/header-values.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!doctype html>
<meta charset=utf8>
<meta name=timeout content=long>
<title>Header value test</title>
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<div id=log></div>
<script>
for(let i = 0; i < 0x100; i++) {
let fail = false
if(i === 0 || i === 0x0A || i === 0x0D) {
fail = true
}

let val = "x" + String.fromCharCode(i) + "x"
async_test((t) => {
let xhr = new XMLHttpRequest()
xhr.open("POST", "../resources/inspect-headers.py?headers=value-test")
if(fail) {
assert_throws("SyntaxError", () => xhr.setRequestHeader("value-test", val))
t.done()
} else {
xhr.setRequestHeader("value-test", val)
xhr.onload = t.step_func_done(() => {
assert_equals(xhr.getResponseHeader("x-request-value-test"), val)
})
xhr.send()
}
}, "XMLHttpRequest with value " + encodeURI(val))

promise_test((t) => {
if(fail) {
return promise_rejects(t, new TypeError(), fetch("about:blank", { headers: {"value-test": val} }))
} else {
return fetch("../resources/inspect-headers.py?headers=value-test", { headers: {"value-test": val} }).then((res) => {
assert_equals(res.headers.get("x-request-value-test"), val)
})
}
}, "fetch() with value " + encodeURI(val))
}
</script>

0 comments on commit b349224

Please sign in to comment.