Skip to content

Commit

Permalink
Worker: Add credentials tests for dedicated workers
Browse files Browse the repository at this point in the history
This CL adds web-platform-tests for the "credentials" option of WorkerOptions:
https://html.spec.whatwg.org/multipage/workers.html#workeroptions

The current spec defines that the default value of this option is "omit", but
there is an ongoing spec discussion to change it to "same-origin":
whatwg/html#3656

This CL adds the tests based on the current spec, and a subsequent CL will
update them based on the decision.

Bug: 843875
Change-Id: I50eb0c7971587b9d84865498d67abef8ed2d8fc6
  • Loading branch information
nhiroki authored and chromium-wpt-export-bot committed May 18, 2018
1 parent b47939b commit fa1b281
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions workers/modules/dedicated-worker-options-credentials.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<title>DedicatedWorker: WorkerOptions 'credentials'</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

// Run a credentials test with the given 'credentials' option.
async function runCredentialsTest(credentials_option) {
// Start a dedicated worker with the 'credentials' option if specified. The
// 'type' option must be 'module' because the spec defines the 'credentials'
// option takes effect only for module workers.
let options = { type: 'module' };
if (credentials_option)
options.credentials = credentials_option;
const worker = new Worker('resources/credentials.py', options);

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

// The worker sends 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 ("dedicated-worker-options-credentials.html.headers").
let expectedCookieValue = '';
if (credentials_option == 'same-origin' || credentials_option == 'include')
expectedCookieValue = 'COOKIE_VALUE';
assert_equals(msg_event.data, expectedCookieValue);
}

promise_test(() => runCredentialsTest(null),
'new Worker() with the default credentials option should not send ' +
'the credentials');

promise_test(() => runCredentialsTest('omit'),
'new Worker() with credentials=omit should not send the credentials');

promise_test(() => runCredentialsTest('same-origin'),
'new Worker() with credentials=same-origin should send the credentials');

promise_test(() => runCredentialsTest('include'),
'new Worker() with credentials=include should send the credentials');

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Set-Cookie: COOKIE_NAME=COOKIE_VALUE
Access-Control-Allow-Credentials: true
10 changes: 10 additions & 0 deletions workers/modules/resources/credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def main(request, response):
cookie = request.cookies.first("COOKIE_NAME", None)

response_headers = [("Content-Type", "text/javascript"),
("Access-Control-Allow-Credentials", "true")]

cookie_value = '';
if cookie:
cookie_value = cookie.value;
return (200, response_headers, "postMessage('"+cookie_value+"');")

0 comments on commit fa1b281

Please sign in to comment.