summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/redirect/redirect-method.any.js
blob: 9fe086a9db718a5de8756e4b8a3830ae411871e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// META: global=window,worker
// META: script=../resources/utils.js

// Creates a promise_test that fetches a URL that returns a redirect response.
//
// |opts| has additional options:
// |opts.body|: the request body as a string or blob (default is empty body)
// |opts.expectedBodyAsString|: the expected response body as a string. The
// server is expected to echo the request body. The default is the empty string
// if the request after redirection isn't POST; otherwise it's |opts.body|.
// |opts.expectedRequestContentType|: the expected Content-Type of redirected
// request.
function redirectMethod(desc, redirectUrl, redirectLocation, redirectStatus, method, expectedMethod, opts) {
  let url = redirectUrl;
  let urlParameters = "?redirect_status=" + redirectStatus;
  urlParameters += "&location=" + encodeURIComponent(redirectLocation);

  let requestHeaders = {
    "Content-Encoding": "Identity",
    "Content-Language": "en-US",
    "Content-Location": "foo",
  };
  let requestInit = {"method": method, "redirect": "follow", "headers" : requestHeaders};
  opts = opts || {};
  if (opts.body) {
    requestInit.body = opts.body;
  }

  promise_test(function(test) {
    return fetch(url + urlParameters, requestInit).then(function(resp) {
      let expectedRequestContentType = "NO";
      if (opts.expectedRequestContentType) {
        expectedRequestContentType = opts.expectedRequestContentType;
      }

      assert_equals(resp.status, 200, "Response's status is 200");
      assert_equals(resp.type, "basic", "Response's type basic");
      assert_equals(
        resp.headers.get("x-request-method"),
        expectedMethod,
        "Request method after redirection is " + expectedMethod);
      let hasRequestBodyHeader = true;
      if (opts.expectedStripRequestBodyHeader) {
        hasRequestBodyHeader = !opts.expectedStripRequestBodyHeader;
      }
      assert_equals(
        resp.headers.get("x-request-content-type"),
        expectedRequestContentType,
        "Request Content-Type after redirection is " + expectedRequestContentType);
      [
        "Content-Encoding",
        "Content-Language",
        "Content-Location"
      ].forEach(header => {
        let xHeader = "x-request-" + header.toLowerCase();
        let expectedValue = hasRequestBodyHeader ? requestHeaders[header] : "NO";
        assert_equals(
          resp.headers.get(xHeader),
          expectedValue,
          "Request " + header + " after redirection is " + expectedValue);
      });
      assert_true(resp.redirected);
      return resp.text().then(function(text) {
        let expectedBody = "";
        if (expectedMethod == "POST") {
          expectedBody = opts.expectedBodyAsString || requestInit.body;
        }
        let expectedContentLength = expectedBody ? expectedBody.length.toString() : "NO";
        assert_equals(text, expectedBody, "request body");
        assert_equals(
          resp.headers.get("x-request-content-length"),
          expectedContentLength,
          "Request Content-Length after redirection is " + expectedContentLength);
        });
    });
  }, desc);
}

promise_test(function(test) {
  assert_false(new Response().redirected);
  return fetch(RESOURCES_DIR + "method.py").then(function(resp) {
    assert_equals(resp.status, 200, "Response's status is 200");
    assert_false(resp.redirected);
  });
}, "Response.redirected should be false on not-redirected responses");

var redirUrl = RESOURCES_DIR + "redirect.py";
var locationUrl = "method.py";

const stringBody = "this is my body";
const blobBody = new Blob(["it's me the blob!", " ", "and more blob!"]);
const blobBodyAsString = "it's me the blob! and more blob!";

redirectMethod("Redirect 301 with GET", redirUrl, locationUrl, 301, "GET", "GET");
redirectMethod("Redirect 301 with POST", redirUrl, locationUrl, 301, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
redirectMethod("Redirect 301 with HEAD", redirUrl, locationUrl, 301, "HEAD", "HEAD");

redirectMethod("Redirect 302 with GET", redirUrl, locationUrl, 302, "GET", "GET");
redirectMethod("Redirect 302 with POST", redirUrl, locationUrl, 302, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
redirectMethod("Redirect 302 with HEAD", redirUrl, locationUrl, 302, "HEAD", "HEAD");

redirectMethod("Redirect 303 with GET", redirUrl, locationUrl, 303, "GET", "GET");
redirectMethod("Redirect 303 with POST", redirUrl, locationUrl, 303, "POST", "GET", { body: stringBody, expectedStripRequestBodyHeader: true });
redirectMethod("Redirect 303 with HEAD", redirUrl, locationUrl, 303, "HEAD", "HEAD");
redirectMethod("Redirect 303 with TESTING", redirUrl, locationUrl, 303, "TESTING", "GET", { expectedStripRequestBodyHeader: true });

redirectMethod("Redirect 307 with GET", redirUrl, locationUrl, 307, "GET", "GET");
redirectMethod("Redirect 307 with POST (string body)", redirUrl, locationUrl, 307, "POST", "POST", { body: stringBody , expectedRequestContentType: "text/plain;charset=UTF-8"});
redirectMethod("Redirect 307 with POST (blob body)", redirUrl, locationUrl, 307, "POST", "POST", { body: blobBody, expectedBodyAsString: blobBodyAsString });
redirectMethod("Redirect 307 with HEAD", redirUrl, locationUrl, 307, "HEAD", "HEAD");

done();