|
| 1 | +// META: timeout=long |
| 2 | + |
| 3 | +promise_test(() => { |
| 4 | + return Promise.all([ |
| 5 | + fetch("resources/mime-types.json"), |
| 6 | + fetch("resources/generated-mime-types.json") |
| 7 | + ]).then(([res, res2]) => res.json().then(runTests).then(() => res2.json().then(runTests))); |
| 8 | +}, "Loading data…"); |
| 9 | + |
| 10 | +function isByteCompatible(str) { |
| 11 | + // see https://fetch.spec.whatwg.org/#concept-header-value-normalize |
| 12 | + if(/^[\u0009\u0020\u000A\u000D]+|[\u0009\u0020\u000A\u000D]+$/.test(str)) { |
| 13 | + return "header-value-incompatible"; |
| 14 | + } |
| 15 | + |
| 16 | + for(let i = 0; i < str.length; i++) { |
| 17 | + const charCode = str.charCodeAt(i); |
| 18 | + // See https://fetch.spec.whatwg.org/#concept-header-value |
| 19 | + if(charCode > 0xFF) { |
| 20 | + return "incompatible"; |
| 21 | + } else if(charCode === 0x00 || charCode === 0x0A || charCode === 0x0D) { |
| 22 | + return "header-value-error"; |
| 23 | + } |
| 24 | + } |
| 25 | + return "compatible"; |
| 26 | +} |
| 27 | + |
| 28 | +function runTests(tests) { |
| 29 | + tests.forEach(val => { |
| 30 | + if(typeof val === "string") { |
| 31 | + return; |
| 32 | + } |
| 33 | + const output = val.output === null ? "" : val.output |
| 34 | + test(() => { |
| 35 | + assert_equals(new Blob([], { type: val.input}).type, output, "Blob"); |
| 36 | + assert_equals(new File([], "noname", { type: val.input}).type, output, "File"); |
| 37 | + }, val.input + " (Blob/File)"); |
| 38 | + |
| 39 | + const compatibleNess = isByteCompatible(val.input); |
| 40 | + if(compatibleNess === "header-value-incompatible") { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + promise_test(() => { |
| 45 | + if(compatibleNess === "incompatible" || compatibleNess === "header-value-error") { |
| 46 | + assert_throws_js(TypeError, () => new Request("about:blank", { headers: [["Content-Type", val.input]] })); |
| 47 | + assert_throws_js(TypeError, () => new Response(null, { headers: [["Content-Type", val.input]] })); |
| 48 | + return Promise.resolve(); |
| 49 | + } else { |
| 50 | + return Promise.all([ |
| 51 | + new Request("about:blank", { headers: [["Content-Type", val.input]] }).blob().then(blob => assert_equals(blob.type, output)), |
| 52 | + new Response(null, { headers: [["Content-Type", val.input]] }).blob().then(blob => assert_equals(blob.type, output)) |
| 53 | + ]); |
| 54 | + } |
| 55 | + }, val.input + " (Request/Response)"); |
| 56 | + }); |
| 57 | +} |
0 commit comments