summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/fetch/api/headers/header-setcookie.any.js
blob: cafb780c2c75a9ae6a84fb842f0d0fa075aeffd8 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// META: title=Headers set-cookie special cases
// META: global=window,worker

const headerList = [
  ["set-cookie", "foo=bar"],
  ["Set-Cookie", "fizz=buzz; domain=example.com"],
];

const setCookie2HeaderList = [
  ["set-cookie2", "foo2=bar2"],
  ["Set-Cookie2", "fizz2=buzz2; domain=example2.com"],
];

function assert_nested_array_equals(actual, expected) {
  assert_equals(actual.length, expected.length, "Array length is not equal");
  for (let i = 0; i < expected.length; i++) {
    assert_array_equals(actual[i], expected[i]);
  }
}

test(function () {
  const headers = new Headers(headerList);
  assert_equals(
    headers.get("set-cookie"),
    "foo=bar, fizz=buzz; domain=example.com",
  );
}, "Headers.prototype.get combines set-cookie headers in order");

test(function () {
  const headers = new Headers(headerList);
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie", "foo=bar"],
    ["set-cookie", "fizz=buzz; domain=example.com"],
  ]);
}, "Headers iterator does not combine set-cookie headers");

test(function () {
  const headers = new Headers(setCookie2HeaderList);
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie2", "foo2=bar2, fizz2=buzz2; domain=example2.com"],
  ]);
}, "Headers iterator does not special case set-cookie2 headers");

test(function () {
  const headers = new Headers([...headerList, ...setCookie2HeaderList]);
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie", "foo=bar"],
    ["set-cookie", "fizz=buzz; domain=example.com"],
    ["set-cookie2", "foo2=bar2, fizz2=buzz2; domain=example2.com"],
  ]);
}, "Headers iterator does not combine set-cookie & set-cookie2 headers");

test(function () {
  // Values are in non alphabetic order, and the iterator should yield in the
  // headers in the exact order of the input.
  const headers = new Headers([
    ["set-cookie", "z=z"],
    ["set-cookie", "a=a"],
    ["set-cookie", "n=n"],
  ]);
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie", "z=z"],
    ["set-cookie", "a=a"],
    ["set-cookie", "n=n"],
  ]);
}, "Headers iterator preserves set-cookie ordering");

test(
  function () {
    const headers = new Headers([
      ["xylophone-header", "1"],
      ["best-header", "2"],
      ["set-cookie", "3"],
      ["a-cool-header", "4"],
      ["set-cookie", "5"],
      ["a-cool-header", "6"],
      ["best-header", "7"],
    ]);
    const list = [...headers];
    assert_nested_array_equals(list, [
      ["a-cool-header", "4, 6"],
      ["best-header", "2, 7"],
      ["set-cookie", "3"],
      ["set-cookie", "5"],
      ["xylophone-header", "1"],
    ]);
  },
  "Headers iterator preserves per header ordering, but sorts keys alphabetically",
);

test(
  function () {
    const headers = new Headers([
      ["xylophone-header", "7"],
      ["best-header", "6"],
      ["set-cookie", "5"],
      ["a-cool-header", "4"],
      ["set-cookie", "3"],
      ["a-cool-header", "2"],
      ["best-header", "1"],
    ]);
    const list = [...headers];
    assert_nested_array_equals(list, [
      ["a-cool-header", "4, 2"],
      ["best-header", "6, 1"],
      ["set-cookie", "5"],
      ["set-cookie", "3"],
      ["xylophone-header", "7"],
    ]);
  },
  "Headers iterator preserves per header ordering, but sorts keys alphabetically (and ignores value ordering)",
);

test(function () {
  const headers = new Headers([["fizz", "buzz"], ["X-Header", "test"]]);
  const iterator = headers[Symbol.iterator]();
  assert_array_equals(iterator.next().value, ["fizz", "buzz"]);
  headers.append("Set-Cookie", "a=b");
  assert_array_equals(iterator.next().value, ["set-cookie", "a=b"]);
  headers.append("Accept", "text/html");
  assert_array_equals(iterator.next().value, ["set-cookie", "a=b"]);
  assert_array_equals(iterator.next().value, ["x-header", "test"]);
  headers.append("set-cookie", "c=d");
  assert_array_equals(iterator.next().value, ["x-header", "test"]);
  assert_true(iterator.next().done);
}, "Headers iterator is correctly updated with set-cookie changes");

test(function () {
  const headers = new Headers([
    ["set-cookie", "a"],
    ["set-cookie", "b"],
    ["set-cookie", "c"]
  ]);
  const iterator = headers[Symbol.iterator]();
  assert_array_equals(iterator.next().value, ["set-cookie", "a"]);
  headers.delete("set-cookie");
  headers.append("set-cookie", "d");
  headers.append("set-cookie", "e");
  headers.append("set-cookie", "f");
  assert_array_equals(iterator.next().value, ["set-cookie", "e"]);
  assert_array_equals(iterator.next().value, ["set-cookie", "f"]);
  assert_true(iterator.next().done);
}, "Headers iterator is correctly updated with set-cookie changes #2");

test(function () {
  const headers = new Headers(headerList);
  assert_true(headers.has("sEt-cOoKiE"));
}, "Headers.prototype.has works for set-cookie");

test(function () {
  const headers = new Headers(setCookie2HeaderList);
  headers.append("set-Cookie", "foo=bar");
  headers.append("sEt-cOoKiE", "fizz=buzz");
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie", "foo=bar"],
    ["set-cookie", "fizz=buzz"],
    ["set-cookie2", "foo2=bar2, fizz2=buzz2; domain=example2.com"],
  ]);
}, "Headers.prototype.append works for set-cookie");

test(function () {
  const headers = new Headers(headerList);
  headers.set("set-cookie", "foo2=bar2");
  const list = [...headers];
  assert_nested_array_equals(list, [
    ["set-cookie", "foo2=bar2"],
  ]);
}, "Headers.prototype.set works for set-cookie");

test(function () {
  const headers = new Headers(headerList);
  headers.delete("set-Cookie");
  const list = [...headers];
  assert_nested_array_equals(list, []);
}, "Headers.prototype.delete works for set-cookie");

test(function () {
  const headers = new Headers();
  assert_array_equals(headers.getSetCookie(), []);
}, "Headers.prototype.getSetCookie with no headers present");

test(function () {
  const headers = new Headers([headerList[0]]);
  assert_array_equals(headers.getSetCookie(), ["foo=bar"]);
}, "Headers.prototype.getSetCookie with one header");

test(function () {
  const headers = new Headers({ "Set-Cookie": "foo=bar" });
  assert_array_equals(headers.getSetCookie(), ["foo=bar"]);
}, "Headers.prototype.getSetCookie with one header created from an object");

test(function () {
  const headers = new Headers(headerList);
  assert_array_equals(headers.getSetCookie(), [
    "foo=bar",
    "fizz=buzz; domain=example.com",
  ]);
}, "Headers.prototype.getSetCookie with multiple headers");

test(function () {
  const headers = new Headers([["set-cookie", ""]]);
  assert_array_equals(headers.getSetCookie(), [""]);
}, "Headers.prototype.getSetCookie with an empty header");

test(function () {
  const headers = new Headers([["set-cookie", "x"], ["set-cookie", "x"]]);
  assert_array_equals(headers.getSetCookie(), ["x", "x"]);
}, "Headers.prototype.getSetCookie with two equal headers");

test(function () {
  const headers = new Headers([
    ["set-cookie2", "x"],
    ["set-cookie", "y"],
    ["set-cookie2", "z"],
  ]);
  assert_array_equals(headers.getSetCookie(), ["y"]);
}, "Headers.prototype.getSetCookie ignores set-cookie2 headers");

test(function () {
  // Values are in non alphabetic order, and the iterator should yield in the
  // headers in the exact order of the input.
  const headers = new Headers([
    ["set-cookie", "z=z"],
    ["set-cookie", "a=a"],
    ["set-cookie", "n=n"],
  ]);
  assert_array_equals(headers.getSetCookie(), ["z=z", "a=a", "n=n"]);
}, "Headers.prototype.getSetCookie preserves header ordering");

test(function () {
  const headers = new Headers({"Set-Cookie": "  a=b\n"});
  headers.append("set-cookie", "\n\rc=d  ");
  assert_nested_array_equals([...headers], [
    ["set-cookie", "a=b"],
    ["set-cookie", "c=d"]
  ]);
  headers.set("set-cookie", "\te=f ");
  assert_nested_array_equals([...headers], [["set-cookie", "e=f"]]);
}, "Adding Set-Cookie headers normalizes their value");

test(function () {
  assert_throws_js(TypeError, () => {
    new Headers({"set-cookie": "\0"});
  });

  const headers = new Headers();
  assert_throws_js(TypeError, () => {
    headers.append("Set-Cookie", "a\nb");
  });
  assert_throws_js(TypeError, () => {
    headers.set("Set-Cookie", "a\rb");
  });
}, "Adding invalid Set-Cookie headers throws");

test(function () {
  const response = new Response();
  response.headers.append("Set-Cookie", "foo=bar");
  assert_array_equals(response.headers.getSetCookie(), []);
  response.headers.append("sEt-cOokIe", "bar=baz");
  assert_array_equals(response.headers.getSetCookie(), []);
}, "Set-Cookie is a forbidden response header");