Source code

Revision control

Other Tools

1
<!DOCTYPE html>
2
<meta charset=utf-8>
3
<title>Test workers self.origin</title>
4
<script src="/resources/testharness.js"></script>
5
<script src="/resources/testharnessreport.js"></script>
6
<div id="log"></div>
7
<script>
8
function assertOriginWorker(workerSource, expectedOrigin, testName) {
9
async_test(function(t) {
10
w = new Worker(workerSource);
11
w.onmessage = t.step_func(function(e) {
12
assert_equals(e.data, expectedOrigin);
13
t.done();
14
});
15
}, testName + ' Worker');
16
}
17
18
function assertOriginSharedWorker(workerSource, expectedOrigin, testName) {
19
async_test(function(t) {
20
w = new SharedWorker(workerSource);
21
w.port.start();
22
w.port.onmessage = t.step_func(function(e) {
23
assert_equals(e.data, expectedOrigin);
24
t.done();
25
});
26
}, testName + ' SharedWorker');
27
}
28
29
// Test same-origin workers
30
assertOriginWorker("./support/WorkerSelfOriginWorker.js", self.origin, "Same Origin");
31
assertOriginSharedWorker("./support/WorkerSelfOriginSharedWorker.js", self.origin, "Same Origin");
32
33
// Test data url workers have opaque origin
34
assertOriginWorker("data:application/javascript,postMessage(self.origin);", "null", "Data Url");
35
assertOriginSharedWorker("data:application/javascript,onconnect = function(e) { e.ports[0].postMessage(self.origin); }", "null", "Data Url");
36
37
// Test blob url workers
38
blob = new Blob(["postMessage(self.origin);"]);
39
blobUrl = URL.createObjectURL(blob);
40
assertOriginWorker(blobUrl, self.origin, "Blob Url");
41
42
blob = new Blob(["onconnect = function(e) { e.ports[0].postMessage(self.origin); }"]);
43
blobUrl = URL.createObjectURL(blob);
44
assertOriginSharedWorker(blobUrl, self.origin, "Blob Url");
45
</script>