|
| 1 | +// META: global=window,worker |
| 2 | +// META: script=../resources/utils.js |
| 3 | + |
| 4 | +// Creates a promise_test that fetches a URL that returns a redirect response. |
| 5 | +// |
| 6 | +// |opts| has additional options: |
| 7 | +// |opts.body|: the request body as a string or blob (default is empty body) |
| 8 | +// |opts.expectedBodyAsString|: the expected response body as a string. The |
| 9 | +// server is expected to echo the request body. The default is the empty string |
| 10 | +// if the request after redirection isn't POST; otherwise it's |opts.body|. |
| 11 | +// |opts.expectedRequestContentType|: the expected Content-Type of redirected |
| 12 | +// request. |
| 13 | +function redirectMethod(desc, redirectUrl, redirectLocation, redirectStatus, method, expectedMethod, opts) { |
| 14 | + let url = redirectUrl; |
| 15 | + let urlParameters = "?redirect_status=" + redirectStatus; |
| 16 | + urlParameters += "&location=" + encodeURIComponent(redirectLocation); |
| 17 | + |
| 18 | + let requestHeaders = { |
| 19 | + "Content-Encoding": "Identity", |
| 20 | + "Content-Language": "en-US", |
| 21 | + "Content-Location": "foo", |
| 22 | + }; |
| 23 | + let requestInit = {"method": method, "redirect": "follow", "headers" : requestHeaders}; |
| 24 | + opts = opts || {}; |
| 25 | + if (opts.body) { |
| 26 | + requestInit.body = opts.body; |
| 27 | + } |
| 28 | + |
| 29 | + promise_test(function(test) { |
| 30 | + return fetch(url + urlParameters, requestInit).then(function(resp) { |
| 31 | + let expectedRequestContentType = "NO"; |
| 32 | + if (opts.expectedRequestContentType) { |
| 33 | + expectedRequestContentType = opts.expectedRequestContentType; |
| 34 | + } |
| 35 | + |
| 36 | + assert_equals(resp.status, 200, "Response's status is 200"); |
| 37 | + assert_equals(resp.type, "basic", "Response's type basic"); |
| 38 | + assert_equals( |
| 39 | + resp.headers.get("x-request-method"), |
| 40 | + expectedMethod, |
| 41 | + "Request method after redirection is " + expectedMethod); |
| 42 | + let hasRequestBodyHeader = true; |
| 43 | + if (opts.expectedStripRequestBodyHeader) { |
| 44 | + hasRequestBodyHeader = !opts.expectedStripRequestBodyHeader; |
| 45 | + } |
| 46 | + assert_equals( |
| 47 | + resp.headers.get("x-request-content-type"), |
| 48 | + expectedRequestContentType, |
| 49 | + "Request Content-Type after redirection is " + expectedRequestContentType); |
| 50 | + [ |
| 51 | + "Content-Encoding", |
| 52 | + "Content-Language", |
| 53 | + "Content-Location" |
| 54 | + ].forEach(header => { |
| 55 | + let xHeader = "x-request-" + header.toLowerCase(); |
| 56 | + let expectedValue = hasRequestBodyHeader ? requestHeaders[header] : "NO"; |
| 57 | + assert_equals( |
| 58 | + resp.headers.get(xHeader), |
| 59 | + expectedValue, |
| 60 | + "Request " + header + " after redirection is " + expectedValue); |
| 61 | + }); |
| 62 | + assert_true(resp.redirected); |
| 63 | + return resp.text().then(function(text) { |
| 64 | + let expectedBody = ""; |
| 65 | + if (expectedMethod == "POST") { |
| 66 | + expectedBody = opts.expectedBodyAsString || requestInit.body; |
| 67 | + } |
| 68 | + let expectedContentLength = expectedBody ? expectedBody.length.toString() : "NO"; |
| 69 | + assert_equals(text, expectedBody, "request body"); |
| 70 | + assert_equals( |
| 71 | + resp.headers.get("x-request-content-length"), |
| 72 | + expectedContentLength, |
| 73 | + "Request Content-Length after redirection is " + expectedContentLength); |
| 74 | + }); |
| 75 | + }); |
| 76 | + }, desc); |
| 77 | +} |
| 78 | + |
| 79 | +promise_test(function(test) { |
| 80 | + assert_false(new Response().redirected); |
| 81 | + return fetch(RESOURCES_DIR + "method.py").then(function(resp) { |
| 82 | + assert_equals(resp.status, 200, "Response's status is 200"); |
| 83 | + assert_false(resp.redirected); |
| 84 | + }); |
| 85 | +}, "Response.redirected should be false on not-redirected responses"); |
| 86 | + |
| 87 | +var redirUrl = RESOURCES_DIR + "redirect.py"; |
| 88 | +var locationUrl = "method.py"; |
| 89 | + |
| 90 | +const stringBody = "this is my body"; |
| 91 | +const blobBody = new Blob(["it's me the blob!", " ", "and more blob!"]); |
| 92 | +const blobBodyAsString = "it's me the blob! and more blob!"; |
| 93 | + |
| 94 | +redirectMethod("Redirect 301 with GET", redirUrl, locationUrl, 301, "GET", "GET"); |
| 95 | +redirectMethod("Redirect 301 with POST", redirUrl, locationUrl, 301, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true }); |
| 96 | +redirectMethod("Redirect 301 with HEAD", redirUrl, locationUrl, 301, "HEAD", "HEAD"); |
| 97 | + |
| 98 | +redirectMethod("Redirect 302 with GET", redirUrl, locationUrl, 302, "GET", "GET"); |
| 99 | +redirectMethod("Redirect 302 with POST", redirUrl, locationUrl, 302, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true }); |
| 100 | +redirectMethod("Redirect 302 with HEAD", redirUrl, locationUrl, 302, "HEAD", "HEAD"); |
| 101 | + |
| 102 | +redirectMethod("Redirect 303 with GET", redirUrl, locationUrl, 303, "GET", "GET"); |
| 103 | +redirectMethod("Redirect 303 with POST", redirUrl, locationUrl, 303, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true }); |
| 104 | +redirectMethod("Redirect 303 with HEAD", redirUrl, locationUrl, 303, "HEAD", "HEAD"); |
| 105 | +redirectMethod("Redirect 303 with TESTING", redirUrl, locationUrl, 303, "TESTING", "GET", { expectedStripRequestBodyHeader: true }); |
| 106 | + |
| 107 | +redirectMethod("Redirect 307 with GET", redirUrl, locationUrl, 307, "GET", "GET"); |
| 108 | +redirectMethod("Redirect 307 with POST (string body)", redirUrl, locationUrl, 307, "POST", "POST", { body: stringBody , expectedRequestContentType: "text/plain;charset=UTF-8"}); |
| 109 | +redirectMethod("Redirect 307 with POST (blob body)", redirUrl, locationUrl, 307, "POST", "POST", { body: blobBody, expectedBodyAsString: blobBodyAsString }); |
| 110 | +redirectMethod("Redirect 307 with HEAD", redirUrl, locationUrl, 307, "HEAD", "HEAD"); |
| 111 | + |
| 112 | +done(); |
0 commit comments