OLD | NEW |
(Empty) | |
| 1 <!doctype html> |
| 2 <html> |
| 3 <head> |
| 4 <meta charset="utf-8"> |
| 5 <title>Request init: headers and body</title> |
| 6 <meta name="help" href="https://fetch.spec.whatwg.org/#request"> |
| 7 <meta name="author" title="Canon Research France" href="https://www.crf.cano
n.fr"> |
| 8 <script src="/resources/testharness.js"></script> |
| 9 <script src="/resources/testharnessreport.js"></script> |
| 10 </head> |
| 11 <body> |
| 12 <script> |
| 13 test(function() { |
| 14 var headerDict = {"name1": "value1", |
| 15 "name2": "value2", |
| 16 "name3": "value3" |
| 17 }; |
| 18 var headers = new Headers(headerDict); |
| 19 var request = new Request("", { "headers" : headers }) |
| 20 for (var name in headerDict) { |
| 21 assert_equals(request.headers.get(name), headerDict[name], |
| 22 "request's headers has " + name + " : " + headerDict[name]); |
| 23 } |
| 24 }, "Initialize Request with headers values"); |
| 25 |
| 26 function makeRequestInit(body, method) { |
| 27 return {"method": method, "body": body}; |
| 28 } |
| 29 |
| 30 function checkRequestInit(body, bodyType, expectedTextBody) { |
| 31 promise_test(function(test) { |
| 32 var request = new Request("", makeRequestInit(body, "POST")); |
| 33 if (body) { |
| 34 assert_throws(new TypeError(), function() { new Request("", makeRequ
estInit(body, "GET")); }); |
| 35 assert_throws(new TypeError(), function() { new Request("", makeRequ
estInit(body, "HEAD")); }); |
| 36 } else { |
| 37 new Request("", makeRequestInit(body, "GET")); // should not throw |
| 38 } |
| 39 var reqHeaders = request.headers; |
| 40 var mime = reqHeaders.get("Content-Type"); |
| 41 assert_true(!body || (mime && mime.search(bodyType) > -1), "Content-Ty
pe header should be \"" + bodyType + "\", not \"" + mime + "\""); |
| 42 return request.text().then(function(bodyAsText) { |
| 43 //not equals: cannot guess formData exact value |
| 44 assert_true( bodyAsText.search(expectedTextBody) > -1, "Retrieve and
verify request body"); |
| 45 }); |
| 46 }, "Initialize Request's body with " + bodyType); |
| 47 } |
| 48 |
| 49 var blob = new Blob(["This is a blob"], {type: "application/octet-binary"}
); |
| 50 var formaData = new FormData(); |
| 51 formaData.append("name", "value"); |
| 52 var usvString = "This is a USVString" |
| 53 |
| 54 checkRequestInit(undefined, undefined, ""); |
| 55 checkRequestInit(null, null, ""); |
| 56 checkRequestInit(blob, "application/octet-binary", "This is a blob"); |
| 57 checkRequestInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nv
alue"); |
| 58 checkRequestInit(usvString, "text/plain;charset=UTF-8", "This is a USVStri
ng"); |
| 59 |
| 60 // Ensure test does not time out in case of missing URLSearchParams suppor
t. |
| 61 if (window.URLSearchParams) { |
| 62 var urlSearchParams = new URLSearchParams("name=value"); |
| 63 checkRequestInit(urlSearchParams, "application/x-www-form-urlencoded;cha
rset=UTF-8", "name=value"); |
| 64 } else { |
| 65 promise_test(function(test) { |
| 66 return Promise.reject("URLSearchParams not supported"); |
| 67 }, "Initialize Request's body with application/x-www-form-urlencoded;cha
rset=UTF-8"); |
| 68 } |
| 69 </script> |
| 70 </body> |
| 71 </html> |
OLD | NEW |