summaryrefslogtreecommitdiff
path: root/dom/tests/mochitest/fetch/test_headers_common.js
blob: 8b17b6b12a2638ae6d716d8bfa12adb680dc6612 (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
222
223
224
225
226
227
228
229
//
// Utility functions
//

function shouldThrow(func, expected, msg) {
  var err;
  try {
    func();
  } catch(e) {
    err = e;
  } finally {
    ok(err instanceof expected, msg);
  }
}

function recursiveArrayCompare(actual, expected) {
  is(Array.isArray(actual), Array.isArray(expected), "Both should either be arrays, or not");
  if (Array.isArray(actual) && Array.isArray(expected)) {
    var diff = actual.length !== expected.length;

    for (var i = 0, n = actual.length; !diff && i < n; ++i) {
      diff = recursiveArrayCompare(actual[i], expected[i]);
    }

    return diff;
  } else {
    return actual !== expected;
  }
}

function arrayEquals(actual, expected, msg) {
  if (actual === expected) {
    return;
  }

  var diff = recursiveArrayCompare(actual, expected);

  ok(!diff, msg);
  if (diff) {
    is(actual, expected, msg);
  }
}

function checkHas(headers, name, msg) {
  function doCheckHas(n) {
    return headers.has(n);
  }
  return _checkHas(doCheckHas, headers, name, msg);
}

function checkNotHas(headers, name, msg) {
  function doCheckNotHas(n) {
    return !headers.has(n);
  }
  return _checkHas(doCheckNotHas, headers, name, msg);
}

function _checkHas(func, headers, name, msg) {
  ok(func(name), msg);
  ok(func(name.toLowerCase()), msg)
  ok(func(name.toUpperCase()), msg)
}

function checkGet(headers, name, expected, msg) {
  is(headers.get(name), expected, msg);
  is(headers.get(name.toLowerCase()), expected, msg);
  is(headers.get(name.toUpperCase()), expected, msg);
}

//
// Test Cases
//

function TestCoreBehavior(headers, name) {
  var start = headers.get(name);

  headers.append(name, "bar");

  checkHas(headers, name, "Has the header");
  var expected = (start ? start.concat(",bar") : "bar");
  checkGet(headers, name, expected, "Retrieve all headers for name");

  headers.append(name, "baz");
  checkHas(headers, name, "Has the header");
  expected = (start ? start.concat(",bar,baz") : "bar,baz");
  checkGet(headers, name, expected, "Retrieve all headers for name");

  headers.set(name, "snafu");
  checkHas(headers, name, "Has the header after set");
  checkGet(headers, name, "snafu", "Retrieve all headers after set");

  headers.delete(name.toUpperCase());
  checkNotHas(headers, name, "Does not have the header after delete");
  checkGet(headers, name, null, "Retrieve all headers after delete");

  // should be ok to delete non-existent name
  headers.delete(name);

  shouldThrow(function() {
    headers.append("foo,", "bam");
  }, TypeError, "Append invalid header name should throw TypeError.");

  shouldThrow(function() {
    headers.append(name, "bam\n");
  }, TypeError, "Append invalid header value should throw TypeError.");

  shouldThrow(function() {
    headers.append(name, "bam\n\r");
  }, TypeError, "Append invalid header value should throw TypeError.");

  ok(!headers.guard, "guard should be undefined in content");
}

function TestEmptyHeaders() {
  is(typeof Headers, "function", "Headers global constructor exists.");
  var headers = new Headers();
  ok(headers, "Constructed empty Headers object");
  TestCoreBehavior(headers, "foo");
}

function TestFilledHeaders() {
  var source = new Headers();
  var filled = new Headers(source);
  ok(filled, "Fill header from empty header");
  TestCoreBehavior(filled, "foo");

  source = new Headers();
  source.append("abc", "123");
  source.append("def", "456");
  source.append("def", "789");

  filled = new Headers(source);
  checkGet(filled, "abc", source.get("abc"), "Single value header list matches");
  checkGet(filled, "def", source.get("def"), "Multiple value header list matches");
  TestCoreBehavior(filled, "def");

  filled = new Headers({
    "zxy": "987",
    "xwv": "654",
    "uts": "321"
  });
  checkGet(filled, "zxy", "987", "Has first object filled key");
  checkGet(filled, "xwv", "654", "Has second object filled key");
  checkGet(filled, "uts", "321", "Has third object filled key");
  TestCoreBehavior(filled, "xwv");

  filled = new Headers([
    ["zxy", "987"],
    ["xwv", "654"],
    ["xwv", "abc"],
    ["uts", "321"]
  ]);
  checkGet(filled, "zxy", "987", "Has first sequence filled key");
  checkGet(filled, "xwv", "654,abc", "Has second sequence filled key");
  checkGet(filled, "uts", "321", "Has third sequence filled key");
  TestCoreBehavior(filled, "xwv");

  shouldThrow(function() {
    filled = new Headers([
      ["zxy", "987", "654"],
      ["uts", "321"]
    ]);
  }, TypeError, "Fill with non-tuple sequence should throw TypeError.");

  shouldThrow(function() {
    filled = new Headers([
      ["zxy"],
      ["uts", "321"]
    ]);
  }, TypeError, "Fill with non-tuple sequence should throw TypeError.");
}

function iterate(iter) {
  var result = [];
  for (var val = iter.next(); !val.done;) {
    result.push(val.value);
    val = iter.next();
  }
  return result;
}

function iterateForOf(iter) {
  var result = [];
  for (var value of iter) {
    result.push(value);
  }
  return result;
}

function byteInflate(str) {
  var encoder = new TextEncoder("utf-8");
  var encoded = encoder.encode(str);
  var result = "";
  for (var i = 0; i < encoded.length; ++i) {
    result += String.fromCharCode(encoded[i]);
  }
  return result
}

function TestHeadersIterator() {
  var ehsanInflated = byteInflate("احسان");
  var headers = new Headers();
  headers.set("foo0", "bar0");
  headers.append("foo", "bar");
  headers.append("foo", ehsanInflated);
  headers.append("Foo2", "bar2");
  headers.set("Foo2", "baz2");
  headers.set("foo3", "bar3");
  headers.delete("foo0");
  headers.delete("foo3");

  var key_iter = headers.keys();
  var value_iter = headers.values();
  var entries_iter = headers.entries();

  arrayEquals(iterate(key_iter), ["foo", "foo2"], "Correct key iterator");
  arrayEquals(iterate(value_iter), ["bar, " + ehsanInflated, "baz2"], "Correct value iterator");
  arrayEquals(iterate(entries_iter), [["foo", "bar, " + ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");

  arrayEquals(iterateForOf(headers), [["foo", "bar, " + ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");
  arrayEquals(iterateForOf(new Headers(headers)), [["foo", "bar, " + ehsanInflated], ["foo2", "baz2"]], "Correct entries iterator");
}

function runTest() {
  TestEmptyHeaders();
  TestFilledHeaders();
  TestHeadersIterator();
  return Promise.resolve();
}