summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/headers/headers-basic.html
blob: 98d71acad9f27fb1d18c71b46e4d8528559b2fb6 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Headers structure</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() {
        new Headers();
      }, "Create headers from no parameter");

      test(function() {
        new Headers(undefined);
      }, "Create headers from undefined parameter");

      test(function() {
        new Headers({});
      }, "Create headers from empty object");

      var parameters = [null, 1];
      parameters.forEach(function(parameter) {
        test(function() {
          assert_throws(new TypeError(), function() { new Headers(parameter) });
        }, "Create headers with " + parameter + " should throw");
      });

      var headerDict = {"name1": "value1",
                        "name2": "value2",
                        "name3": "value3",
                        "name4": null,
                        "name5": undefined,
                        "name6": 1,
                        "Content-Type": "value4"
      };

      var headerSeq = [];
      for (var name in headerDict)
        headerSeq.push([name, headerDict[name]]);

      test(function() {
        var headers = new Headers(headerSeq);
        for (name in headerDict) {
          assert_equals(headers.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);
        }
        assert_equals(headers.get("length"), null, "init should be treated as a sequence, not as a dictionary");
      }, "Create headers with sequence");

      test(function() {
        var headers = new Headers(headerDict);
        for (name in headerDict) {
          assert_equals(headers.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);
        }
      }, "Create headers with OpenEndedDictionary");

      test(function() {
        var headers = new Headers(headerDict);
        var headers2 = new Headers(headers);
        for (name in headerDict) {
          assert_equals(headers2.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);
        }
      }, "Create headers with existing headers");

      test(function() {
        var headers = new Headers();
        for (name in headerDict) {
          headers.append(name, headerDict[name]);
          assert_equals(headers.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);
        }
      }, "Check append method");

      test(function() {
        var headers = new Headers();
        for (name in headerDict) {
          headers.set(name, headerDict[name]);
          assert_equals(headers.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);
        }
      }, "Check set method");

      test(function() {
        var headers = new Headers(headerDict);
        for (name in headerDict)
          assert_true(headers.has(name),"headers has name " + name);

        assert_false(headers.has("nameNotInHeaders"),"headers do not have header: nameNotInHeaders");
      }, "Check has method");

      test(function() {
        var headers = new Headers(headerDict);
        for (name in headerDict) {
          assert_true(headers.has(name),"headers have a header: " + name);
          headers.delete(name)
          assert_true(!headers.has(name),"headers do not have anymore a header: " + name);
        }
      }, "Check delete method");

      test(function() {
        var headers = new Headers(headerDict);
        for (name in headerDict)
          assert_equals(headers.get(name), String(headerDict[name]),
            "name: " + name + " has value: " + headerDict[name]);

        assert_equals(headers.get("nameNotInHeaders"), null, "header: nameNotInHeaders has no value");
      }, "Check get method");

      var headerEntriesDict = {"name1": "value1",
                               "Name2": "value2",
                               "name": "value3",
                               "content-Type": "value4",
                               "Content-Typ": "value5",
                               "Content-Types": "value6"
      };
      var sortedHeaderDict = {};
      var headerValues = [];
      var sortedHeaderKeys = Object.keys(headerEntriesDict).map(function(value) {
        sortedHeaderDict[value.toLowerCase()] = headerEntriesDict[value];
        headerValues.push(headerEntriesDict[value]);
        return value.toLowerCase();
      }).sort();

      var iteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
      function checkIteratorProperties(iterator) {
        var prototype = Object.getPrototypeOf(iterator);
        assert_equals(Object.getPrototypeOf(prototype), iteratorPrototype);

        var descriptor = Object.getOwnPropertyDescriptor(prototype, "next");
        assert_true(descriptor.configurable, "configurable");
        assert_true(descriptor.enumerable, "enumerable");
        assert_true(descriptor.writable, "writable");
      }

      test(function() {
        var headers = new Headers(headerEntriesDict);
        var actual = headers.keys();
        checkIteratorProperties(actual);

        sortedHeaderKeys.forEach(function(key) {
            entry = actual.next();
            assert_false(entry.done);
            assert_equals(entry.value, key);
        });
        assert_true(actual.next().done);
        assert_true(actual.next().done);

        for (key of headers.keys())
            assert_true(sortedHeaderKeys.indexOf(key) != -1);
      }, "Check keys method");

      test(function() {
        var headers = new Headers(headerEntriesDict);
        var actual = headers.values();
        checkIteratorProperties(actual);

        sortedHeaderKeys.forEach(function(key) {
            entry = actual.next();
            assert_false(entry.done);
            assert_equals(entry.value, sortedHeaderDict[key]);
        });
        assert_true(actual.next().done);
        assert_true(actual.next().done);

        for (value of headers.values())
            assert_true(headerValues.indexOf(value) != -1);
      }, "Check values method");

      test(function() {
        var headers = new Headers(headerEntriesDict);
        var actual = headers.entries();
        checkIteratorProperties(actual);

        sortedHeaderKeys.forEach(function(key) {
            entry = actual.next();
            assert_false(entry.done);
            assert_equals(entry.value[0], key);
            assert_equals(entry.value[1], sortedHeaderDict[key]);
        });
        assert_true(actual.next().done);
        assert_true(actual.next().done);

        for (entry of headers.entries())
            assert_equals(entry[1], sortedHeaderDict[entry[0]]);
      }, "Check entries method");

      test(function() {
        var headers = new Headers(headerEntriesDict);
        var actual = headers[Symbol.iterator]();

        sortedHeaderKeys.forEach(function(key) {
            entry = actual.next();
            assert_false(entry.done);
            assert_equals(entry.value[0], key);
            assert_equals(entry.value[1], sortedHeaderDict[key]);
        });
        assert_true(actual.next().done);
        assert_true(actual.next().done);
      }, "Check Symbol.iterator method");

      test(function() {
        var headers = new Headers(headerEntriesDict);
        var reference = sortedHeaderKeys[Symbol.iterator]();
        headers.forEach(function(value, key, container) {
            assert_equals(headers, container);
            entry = reference.next();
            assert_false(entry.done);
            assert_equals(key, entry.value);
            assert_equals(value, sortedHeaderDict[entry.value]);
        });
        assert_true(reference.next().done);
      }, "Check forEach method");
   </script>
  </body>
</html>