OLD | NEW |
(Empty) | |
| 1 if (this.document === undefined) { |
| 2 importScripts("/resources/testharness.js"); |
| 3 importScripts("../resources/utils.js"); |
| 4 } |
| 5 |
| 6 function checkContentType(contentType, body) |
| 7 { |
| 8 if (self.FormData && body instanceof self.FormData) { |
| 9 assert_true(contentType.startsWith("multipart/form-data; boundary="), "R
equest should have header content-type starting with multipart/form-data; bounda
ry=, but got " + contentType); |
| 10 return; |
| 11 } |
| 12 |
| 13 var expectedContentType = "text/plain;charset=UTF-8"; |
| 14 if(body === null || body instanceof ArrayBuffer || body.buffer instanceof Ar
rayBuffer) |
| 15 expectedContentType = null; |
| 16 else if (body instanceof Blob) |
| 17 expectedContentType = body.type ? body.type : null; |
| 18 else if (body instanceof URLSearchParams) |
| 19 expectedContentType = "application/x-www-form-urlencoded;charset=UTF-8"; |
| 20 |
| 21 assert_equals(contentType , expectedContentType, "Request should have header
content-type: " + expectedContentType); |
| 22 } |
| 23 |
| 24 function requestHeaders(desc, url, method, body, expectedOrigin, expectedContent
Length) { |
| 25 var urlParameters = "?headers=origin|user-agent|accept-charset|content-length|
content-type"; |
| 26 var requestInit = {"method": method} |
| 27 promise_test(function(test){ |
| 28 if (typeof body === "function") |
| 29 body = body(); |
| 30 if (body) |
| 31 requestInit["body"] = body; |
| 32 return fetch(url + urlParameters, requestInit).then(function(resp) { |
| 33 assert_equals(resp.status, 200, "HTTP status is 200"); |
| 34 assert_equals(resp.type , "basic", "Response's type is basic"); |
| 35 assert_true(resp.headers.has("x-request-user-agent"), "Request has header
user-agent"); |
| 36 assert_false(resp.headers.has("accept-charset"), "Request has header accep
t-charset"); |
| 37 assert_equals(resp.headers.get("x-request-origin") , expectedOrigin, "Requ
est should have header origin: " + expectedOrigin); |
| 38 if (expectedContentLength !== undefined) |
| 39 assert_equals(resp.headers.get("x-request-content-length") , expectedCon
tentLength, "Request should have header content-length: " + expectedContentLengt
h); |
| 40 checkContentType(resp.headers.get("x-request-content-type"), body); |
| 41 }); |
| 42 }, desc); |
| 43 } |
| 44 |
| 45 var url = RESOURCES_DIR + "inspect-headers.py" |
| 46 |
| 47 requestHeaders("Fetch with GET", url, "GET", null, null, null); |
| 48 requestHeaders("Fetch with HEAD", url, "HEAD", null, null, null); |
| 49 requestHeaders("Fetch with PUT without body", url, "POST", null, location.origin
, "0"); |
| 50 requestHeaders("Fetch with PUT with body", url, "PUT", "Request's body", locatio
n.origin, "14"); |
| 51 requestHeaders("Fetch with POST without body", url, "POST", null, location.origi
n, "0"); |
| 52 requestHeaders("Fetch with POST with text body", url, "POST", "Request's body",
location.origin, "14"); |
| 53 requestHeaders("Fetch with POST with FormData body", url, "POST", function() { r
eturn new FormData(); }, location.origin); |
| 54 requestHeaders("Fetch with POST with URLSearchParams body", url, "POST", functio
n() { return new URLSearchParams("name=value"); }, location.origin, "10"); |
| 55 requestHeaders("Fetch with POST with Blob body", url, "POST", new Blob(["Test"])
, location.origin, "4"); |
| 56 requestHeaders("Fetch with POST with ArrayBuffer body", url, "POST", new ArrayBu
ffer(4), location.origin, "4"); |
| 57 requestHeaders("Fetch with POST with Uint8Array body", url, "POST", new Uint8Arr
ay(4), location.origin, "4"); |
| 58 requestHeaders("Fetch with POST with Int8Array body", url, "POST", new Int8Array
(4), location.origin, "4"); |
| 59 requestHeaders("Fetch with POST with Float32Array body", url, "POST", new Float3
2Array(1), location.origin, "4"); |
| 60 requestHeaders("Fetch with POST with Float64Array body", url, "POST", new Float6
4Array(1), location.origin, "8"); |
| 61 requestHeaders("Fetch with POST with DataView body", url, "POST", new DataView(n
ew ArrayBuffer(8), 0, 4), location.origin, "4"); |
| 62 requestHeaders("Fetch with POST with Blob body with mime type", url, "POST", new
Blob(["Test"], { type: "text/maybe" }), location.origin, "4"); |
| 63 requestHeaders("Fetch with Chicken", url, "Chicken", null, location.origin, null
); |
| 64 requestHeaders("Fetch with Chicken with body", url, "Chicken", "Request's body",
location.origin, "14"); |
| 65 |
| 66 function requestOriginHeader(method, mode, needsOrigin) { |
| 67 promise_test(function(test){ |
| 68 return fetch(url + "?headers=origin", {method:method, mode:mode}).then(funct
ion(resp) { |
| 69 assert_equals(resp.status, 200, "HTTP status is 200"); |
| 70 assert_equals(resp.type , "basic", "Response's type is basic"); |
| 71 if(needsOrigin) |
| 72 assert_equals(resp.headers.get("x-request-origin") , location.origin, "R
equest should have an Origin header with origin: " + location.origin); |
| 73 else |
| 74 assert_equals(resp.headers.get("x-request-origin"), null, "Request shoul
d not have an Origin header") |
| 75 }); |
| 76 }, "Fetch with " + method + " and mode \"" + mode + "\" " + (needsOrigin ? "ne
eds" : "does not need") + " an Origin header"); |
| 77 } |
| 78 |
| 79 requestOriginHeader("GET", "cors", false); |
| 80 requestOriginHeader("POST", "same-origin", true); |
| 81 requestOriginHeader("POST", "no-cors", true); |
| 82 requestOriginHeader("PUT", "same-origin", true); |
| 83 requestOriginHeader("TacO", "same-origin", true); |
| 84 requestOriginHeader("TacO", "cors", true); |
| 85 |
| 86 done(); |
OLD | NEW |