summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/request/request-init-002.html
blob: 5d92b09c568ef4f3f65bf963897b24c5456f998a (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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Request init: headers and body</title>
    <meta name="help" href="https://fetch.spec.whatwg.org/#request">
    <meta name="author" title="Canon Research France" href="https://www.crf.canon.fr">
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
      test(function() {
        var headerDict = {"name1": "value1",
                          "name2": "value2",
                          "name3": "value3"
                         };
        var headers = new Headers(headerDict);
        var request = new Request("", { "headers" : headers })
        for (var name in headerDict) {
          assert_equals(request.headers.get(name), headerDict[name],
           "request's headers has " + name + " : " + headerDict[name]);
        }
      }, "Initialize Request with headers values");

      function makeRequestInit(body, method) {
        return {"method": method, "body": body};
      }

      function checkRequestInit(body, bodyType, expectedTextBody) {
        promise_test(function(test) {
          var request = new Request("", makeRequestInit(body, "POST"));
          if (body) {
            assert_throws(new TypeError(), function() { new Request("", makeRequestInit(body, "GET")); });
            assert_throws(new TypeError(), function() { new Request("", makeRequestInit(body, "HEAD")); });
          } else {
            new Request("", makeRequestInit(body, "GET")); // should not throw
          }
          var reqHeaders = request.headers;
          var mime = reqHeaders.get("Content-Type");
          assert_true(!body || (mime && mime.search(bodyType) > -1), "Content-Type header should be \"" + bodyType + "\", not \"" + mime + "\"");
          return request.text().then(function(bodyAsText) {
            //not equals: cannot guess formData exact value
            assert_true( bodyAsText.search(expectedTextBody) > -1, "Retrieve and verify request body");
          });
        }, "Initialize Request's body with " + bodyType);
      }

      var blob = new Blob(["This is a blob"], {type: "application/octet-binary"});
      var formaData = new FormData();
      formaData.append("name", "value");
      var usvString = "This is a USVString"

      checkRequestInit(undefined, undefined, "");
      checkRequestInit(null, null, "");
      checkRequestInit(blob, "application/octet-binary", "This is a blob");
      checkRequestInit(formaData, "multipart/form-data", "name=\"name\"\r\n\r\nvalue");
      checkRequestInit(usvString, "text/plain;charset=UTF-8", "This is a USVString");

      // Ensure test does not time out in case of missing URLSearchParams support.
      if (window.URLSearchParams) {
        var urlSearchParams = new URLSearchParams("name=value");
        checkRequestInit(urlSearchParams, "application/x-www-form-urlencoded;charset=UTF-8", "name=value");
      } else {
        promise_test(function(test) {
          return Promise.reject("URLSearchParams not supported");
        }, "Initialize Request's body with application/x-www-form-urlencoded;charset=UTF-8");
      }
    </script>
  </body>
</html>