summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/headers/headers-errors.html
blob: 194ff32f1559f2dd9b5903eb3738c17c061c7172 (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
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Headers errors</title>
    <meta name="help" href="https://fetch.spec.whatwg.org/#headers">
    <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 Headers([["name"]]); });
      }, "Create headers giving an array having one string as init argument");

      test(function() {
        assert_throws(new TypeError() , function() { new Headers([["invalid", "invalidValue1", "invalidValue2"]]); });
      }, "Create headers giving an array having three strings as init argument");

      test(function() {
        assert_throws(new TypeError() , function() { new Headers([["invalidĀ", "Value1"]]); });
      }, "Create headers giving bad header name as init argument");

      test(function() {
        assert_throws(new TypeError() , function() { new Headers([["name", "invalidValueĀ"]]); });
      }, "Create headers giving bad header value as init argument");

      var badNames = ["invalidĀ", {}];
      var badValues = ["invalidĀ"];

      badNames.forEach(function(name) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.get(name); });
        }, "Check headers get with an invalid name " + name);
      });

      badNames.forEach(function(name) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.delete(name); });
        }, "Check headers delete with an invalid name " + name);
      });

      badNames.forEach(function(name) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.has(name); });
        }, "Check headers has with an invalid name " + name);
      });

      badNames.forEach(function(name) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.set(name, "Value1"); });
        }, "Check headers set with an invalid name " + name);
      });

      badValues.forEach(function(value) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.set("name", value); });
        }, "Check headers set with an invalid value " + value);
      });

      badNames.forEach(function(name) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.append("invalidĀ", "Value1"); });
        }, "Check headers append with an invalid name " + name);
      });

      badValues.forEach(function(value) {
        test(function() {
          var headers = new Headers();
          assert_throws(new TypeError() , function() { headers.append("name", value); });
        }, "Check headers append with an invalid value " + value);
      });

      test(function() {
        var headers = new Headers([["name", "value"]]);
        assert_throws(new TypeError() , function() { headers.forEach(); });
        assert_throws(new TypeError() , function() { headers.forEach(undefined); });
        assert_throws(new TypeError() , function() { headers.forEach(1); });
      }, "Headers forEach throws if argument is not callable");

      test(function() {
        var headers = new Headers([["name1", "value1"], ["name2", "value2"], ["name3", "value3"]]);
        var counter = 0;
        try {
            headers.forEach(function(value, name) {
                counter++;
                if (name == "name2")
                     throw "error";
            });
        } catch (e) {
            assert_equals(counter, 2);
            assert_equals(e, "error");
            return;
        }
        assert_unreached();
      }, "Headers forEach loop should stop if callback is throwing exception");
   </script>
  </body>
</html>