summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/workers/modules/shared-worker-options-credentials.html
blob: 221dd01a376f1d33d900fc650ab423e3bc7b88e4 (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<!DOCTYPE html>
<title>SharedWorker: WorkerOptions 'credentials'</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script>
host_info = get_host_info();

// Determines the expected cookie value to be reported by a shared worker
// based on the given option. The worker reports an empty string as the actual
// cookie value if the cookie wasn't sent to the server. Otherwise, it's the
// value set by the headers file:
// "shared-worker-options-credentials.html.headers"
function DetermineExpectedCookieValue(options, config) {
  // Valid WorkerOptions and test config checking.
  if (config.origin !== 'same' && config.origin !== 'remote')
    assert_unreached('Invalid config.origin was specified: ' + config.origin);
  if (options.credentials && options.credentials !== 'omit' &&
      options.credentials !== 'same-origin' &&
      options.credentials !== 'include') {
    assert_unreached('Invalid credentials option was specified: ' +
                      options.credentials);
  }
  if (options.type !== 'classic' && options.type !== 'module')
    assert_unreached('Invalid type option was specified: ' + options.type);

  if (options.type === 'classic')
    return (config.origin === 'same') ? '1' : '';

  if (options.credentials === 'omit')
    return '';
  else if (options.credentials === 'include')
    return '1';
  else
    return (config.origin === 'same') ? '1' : '';
}

// Runs a credentials test with the given WorkerOptions.
//
// |options| is a WorkerOptions dict.
// |config| has options as follows:
//
//   config = {
//     fetchType: 'top-level' or 'descendant-static' or 'descendant-dynamic'
//     origin: 'remote' or 'same'
//   };
//
// - |config.fetchType| indicates the type of script to load for the test.
// - |config.origin| indicates same-origin-ness of the script to load.
function credentials_test(options, config, description) {
  promise_test(async () => {
    let workerURL, origin = config.origin;
    if (config.fetchType === 'top-level') {
      workerURL = 'resources/postmessage-credentials.py';
    } else if (config.fetchType === 'descendant-static') {
      workerURL =
        `resources/static-import-${origin}-origin-credentials-checker-worker.${origin === 'same' ? '' : 'sub.'}js`;
    } else if (config.fetchType === 'descendant-dynamic') {
      workerURL =
        `resources/dynamic-import-${origin}-origin-credentials-checker-worker.${origin === 'same' ? '' : 'sub.'}js`;
    } else {
      assert_unreached('Invalid config.fetchType: ' + config.fetchType);
    }

    // Name idetically for each test cases so that it connects to the shared
    // worker with specified type and credentials.
    options.name = `${options.type}_${options.credentials || 'default'}_${config.fetchType}_${config.origin}`;

    const worker = new SharedWorker(workerURL, options);

    // Wait until the worker sends the actual cookie value.
    const msg_event = await new Promise(resolve => worker.port.onmessage = resolve);

    const expectedCookieValue = DetermineExpectedCookieValue(options, config);
    assert_equals(msg_event.data, expectedCookieValue);
  }, description);
}

function init() {
  // Same-origin cookie is set up in the .headers file in this directory.
  promise_test(async () => {
    return fetch(
      `${host_info.HTTP_REMOTE_ORIGIN}/cookies/resources/set-cookie.py?name=COOKIE_NAME&path=/workers/modules/`,
      {
        mode: 'no-cors',
        credentials: 'include'
      });
  }, 'Test initialization: setting up cross-origin cookie');
}

init();

// Tests for module workers.

credentials_test(
    { type: 'module' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=module and default credentials option ' +
    'should behave as credentials=same-origin and send the credentials');

credentials_test(
    { credentials: 'omit', type: 'module' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=module and credentials=omit should not ' +
    'send the credentials');

credentials_test(
    { credentials: 'same-origin', type: 'module' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=module and credentials=same-origin should ' +
    'send the credentials');

credentials_test(
    { credentials: 'include', type: 'module' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=module and credentials=include should send ' +
    'the credentials');

// Tests for module worker static imports.

credentials_test(
    { type: 'module' },
    { fetchType: 'descendant-static', origin: 'same' },
    'new SharedWorker() with type=module and default credentials option ' +
    'should behave as credentials=same-origin and send the credentials for ' +
    'same-origin static imports');

credentials_test(
    { credentials: 'omit', type: 'module' },
    { fetchType: 'descendant-static', origin: 'same' },
    'new SharedWorker() with type=module and credentials=omit should not ' +
    'send the credentials for same-origin static imports');

credentials_test(
    { credentials: 'same-origin', type: 'module' },
    { fetchType: 'descendant-static', origin: 'same' },
    'new SharedWorker() with type=module and credentials=same-origin should ' +
    'send the credentials for same-origin static imports');

credentials_test(
    { credentials: 'include', type: 'module' },
    { fetchType: 'descendant-static', origin: 'same' },
    'new SharedWorker() with type=module and credentials=include should send ' +
    'the credentials for same-origin static imports');

credentials_test(
    { type: 'module' },
    { fetchType: 'descendant-static', origin: 'remote' },
    'new SharedWorker() with type=module and default credentials option ' +
    'should behave as credentials=same-origin and not send the credentials ' +
    'for cross-origin static imports');

credentials_test(
    { credentials: 'omit', type: 'module' },
    { fetchType: 'descendant-static', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=omit should not ' +
    'send the credentials for cross-origin static imports');

credentials_test(
    { credentials: 'same-origin', type: 'module' },
    { fetchType: 'descendant-static', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=same-origin should ' +
    'not send the credentials for cross-origin static imports');

credentials_test(
    { credentials: 'include', type: 'module' },
    { fetchType: 'descendant-static', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=include should send ' +
    'the credentials for cross-origin static imports');

// Tests for module worker dynamic imports.

credentials_test(
    { type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=module and default credentials option ' +
    'should behave as credentials=same-origin and send the credentials for ' +
    'same-origin dynamic imports');

credentials_test(
    { credentials: 'omit', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=module and credentials=omit should not ' +
    'send the credentials for same-origin dynamic imports');

credentials_test(
    { credentials: 'same-origin', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=module and credentials=same-origin should ' +
    'send the credentials for same-origin dynamic imports');

credentials_test(
    { credentials: 'include', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=module and credentials=include should send ' +
    'the credentials for same-origin dynamic imports');

credentials_test(
    { type: 'module'},
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=module and default credentials option ' +
    'should behave as credentials=same-origin and not send the credentials ' +
    'for cross-origin dynamic imports');

credentials_test(
    { credentials: 'omit', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=omit should not ' +
    'send the credentials for cross-origin dynamic imports');

credentials_test(
    { credentials: 'same-origin', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=same-origin should ' +
    'not send the credentials for cross-origin dynamic imports');

credentials_test(
    { credentials: 'include', type: 'module' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=module and credentials=include should send ' +
    'the credentials for cross-origin dynamic imports');

// Tests for classic workers.
// TODO(domfarolino): Maybe move classic worker tests up a directory?

credentials_test(
    { type: 'classic' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'regardless of the credentials option (default).');

credentials_test(
    { credentials: 'omit', type: 'classic' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'regardless of the credentials option (omit).');

credentials_test(
    { credentials: 'same-origin', type: 'classic' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'regardless of the credentials option (same-origin).');

credentials_test(
    { credentials: 'include', type: 'classic' },
    { fetchType: 'top-level', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'regardless of the credentials option (include).');

// Tests for classic worker dynamic imports.

credentials_test(
    { type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'for same-origin dynamic imports regardless of the credentials option ' +
    '(default).');

credentials_test(
    { credentials: 'omit', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'for same-origin dynamic imports regardless of the credentials option ' +
    '(omit).');

credentials_test(
    { credentials: 'same-origin', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'for same-origin dynamic imports regardless of the credentials option ' +
    '(same-origin).');

credentials_test(
    { credentials: 'include', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'same' },
    'new SharedWorker() with type=classic should always send the credentials ' +
    'for same-origin dynamic imports regardless of the credentials option ' +
    '(include).');

credentials_test(
    { type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=classic should never send the credentials ' +
    'for cross-origin dynamic imports regardless of the credentials option ' +
    '(default).');

credentials_test(
    { credentials: 'omit', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=classic should never send the credentials ' +
    'for cross-origin dynamic imports regardless of the credentials option ' +
    '(omit).');

credentials_test(
    { credentials: 'same-origin', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=classic should never send the credentials ' +
    'for cross-origin dynamic imports regardless of the credentials option ' +
    '(same-origin).');

credentials_test(
    { credentials: 'include', type: 'classic' },
    { fetchType: 'descendant-dynamic', origin: 'remote' },
    'new SharedWorker() with type=classic should never send the credentials ' +
    'for cross-origin dynamic imports regardless of the credentials option ' +
    '(include).');

</script>