testing/web-platform/tests/fetch/api/request/request-error.html
author Tom Tung <shes050117@gmail.com>
Tue, 29 Aug 2017 16:01:58 +0800
changeset 657392 d36e0b2393c68c2c1711bbf1e30025b078dd7e67
parent 576458 1ee09646e328f0d43feb567f209ab99f123776af
child 660772 20ca9381933394f96a736f5fd214b3c3b3efbdcb
permissions -rw-r--r--
Bug 1393439 - P3: Remove the wpt test for assuming to throw no-cors request with integrity attribute. r=bkelly MozReview-Commit-ID: 1DblOV82nEd

<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Request error</title>
    <meta name="help" href="https://fetch.spec.whatwg.org/#request">
    <meta name="help" href="https://fetch.spec.whatwg.org/#body-mixin">
    <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() {
        assert_throws(new TypeError() , function() { new Request("", {"window" : "http://test.url"}); },
          "Expect TypeError exception");
      },"RequestInit's window is not null");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("http://:not a valid URL"); },
          "Expect TypeError exception");
      },"Input URL is not valid")

      test(function() {
        assert_throws(new TypeError() , function() { new Request("http://user:pass@test.url"); },
          "Expect TypeError exception");
      },"Input URL has credentials");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("", {"mode" : "navigate"}); },
          "Expect TypeError exception");
      },"RequestInit's mode is navigate");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("", {"referrer" : "http://:not a valid URL"}); },
          "Expect TypeError exception");
      },"RequestInit's referrer is invalid");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("", {"method" : "IN VALID"}); },
          "Expect TypeError exception");
      }, "RequestInit's method is invalid");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("", {"method" : "TRACE"}); },
          "Expect TypeError exception");
      }, "RequestInit's method is forbidden");

      test(function() {
        assert_throws(new TypeError() , function() { new Request("", {"mode" : "no-cors", "method" : "PUT"}); },
          "Expect TypeError exception");
      },"RequestInit's mode is no-cors and method is not simple");

      test(function() {
        assert_throws(new TypeError() ,
                      function() { new Request("", {"mode" : "cors", "cache" : "only-if-cached"}); },
                      "Expect TypeError exception");
      },"RequestInit's cache mode is only-if-cached and mode is not same-origin");

      test(function() {
        var initialHeaders = new Headers([["Content-Type", "potato"]]);
        var initialRequest = new Request("", {"headers" : initialHeaders});
        var request = new Request(initialRequest);
        assert_equals(request.headers.get("Content-Type"), "potato");
      }, "Request should get its content-type from the init request");

      test(function() {
        var initialHeaders = new Headers([["Content-Type", "potato"]]);
        var initialRequest = new Request("", {"headers" : initialHeaders});
        var headers = new Headers([]);
        var request = new Request(initialRequest, {"headers" : headers});
        assert_false(request.headers.has("Content-Type"));
      }, "Request should not get its content-type from the init request if init headers are provided");

      test(function() {
        var initialHeaders = new Headers([["Content-Type-Extra", "potato"]]);
        var initialRequest = new Request("", {"headers" : initialHeaders, "body" : "this is my plate", "method" : "POST"});
        var request = new Request(initialRequest);
        assert_equals(request.headers.get("Content-Type"), "text/plain;charset=UTF-8");
      }, "Request should get its content-type from the body if none is provided");

      test(function() {
        var initialHeaders = new Headers([["Content-Type", "potato"]]);
        var initialRequest = new Request("", {"headers" : initialHeaders, "body" : "this is my plate", "method" : "POST"});
        var request = new Request(initialRequest);
        assert_equals(request.headers.get("Content-Type"), "potato");
      }, "Request should get its content-type from init headers if one is provided");

      var parameters = ["referrerPolicy", "mode", "credentials", "cache", "redirect"];
      parameters.forEach(function(parameter) {
        test(function() {
          var options = { };
          options[parameter] = "BAD";
          assert_throws(new TypeError(), function() { new Request("", options); });
        },"Bad " + parameter +" init parameter value");
      });

      function testOnlyIfCachedMode(fetchMode, ok) {
        test(function() {
          var options = {"cache": "only-if-cached", "mode": fetchMode};
          if (ok)
            new Request("test", options);
          else
            assert_throws(new TypeError(), function() { new Request("test", options); });
        }, "Request with cache mode: only-if-cached and fetch mode: " + fetchMode);
      }
      testOnlyIfCachedMode("same-origin", true);
      testOnlyIfCachedMode("cors", false);
      testOnlyIfCachedMode("no-cors", false);

    </script>
  </body>
</html>