summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/service-workers/service-worker/resources/client-navigate-worker.js
blob: 6101d5d8f92ccd0ec798c27a2ab1cd33de5fc1bb (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
importScripts("worker-testharness.js");
importScripts("test-helpers.sub.js");
importScripts("/common/get-host-info.sub.js")
importScripts("testharness-helpers.js")

setup({ explicit_done: true });

self.onfetch = function(e) {
  if (e.request.url.indexOf("client-navigate-frame.html") >= 0) {
    return;
  }
  e.respondWith(new Response(e.clientId));
};

function pass(test, url) {
  return { result: test,
           url: url };
}

function fail(test, reason) {
  return { result: "FAILED " + test + " " + reason }
}

self.onmessage = function(e) {
  var port = e.data.port;
  var test = e.data.test;
  var clientId = e.data.clientId;
  var clientUrl = "";
  if (test === "test_client_navigate_success") {
    promise_test(function(t) {
      this.add_cleanup(() => port.postMessage(pass(test, clientUrl)));
      return self.clients.get(clientId)
                 .then(client => client.navigate("client-navigated-frame.html"))
                 .then(client => {
                   clientUrl = client.url;
                   assert_true(client instanceof WindowClient);
                 })
                 .catch(unreached_rejection(t));
    }, "Return value should be instance of WindowClient");
    done();
  } else if (test === "test_client_navigate_cross_origin") {
    promise_test(function(t) {
      this.add_cleanup(() => port.postMessage(pass(test, clientUrl)));
      var path = new URL('client-navigated-frame.html', self.location.href).pathname;
      var url = get_host_info()['HTTPS_REMOTE_ORIGIN'] + path;
      return self.clients.get(clientId)
                 .then(client => client.navigate(url))
                 .then(client => {
                   clientUrl = (client && client.url) || "";
                   assert_equals(client, null,
                                 'cross-origin navigate resolves with null');
                 })
                 .catch(unreached_rejection(t));
    }, "Navigating to different origin should resolve with null");
    done();
  } else if (test === "test_client_navigate_about_blank") {
    promise_test(function(t) {
      this.add_cleanup(function() { port.postMessage(pass(test, "")); });
      return self.clients.get(clientId)
                 .then(client => promise_rejects_js(t, TypeError, client.navigate("about:blank")))
                 .catch(unreached_rejection(t));
    }, "Navigating to about:blank should reject with TypeError");
    done();
  } else if (test === "test_client_navigate_mixed_content") {
    promise_test(function(t) {
      this.add_cleanup(function() { port.postMessage(pass(test, "")); });
      var path = new URL('client-navigated-frame.html', self.location.href).pathname;
      // Insecure URL should fail since the frame is owned by a secure parent
      // and navigating to http:// would create a mixed-content violation.
      var url = get_host_info()['HTTP_REMOTE_ORIGIN'] + path;
      return self.clients.get(clientId)
                 .then(client => promise_rejects_js(t, TypeError, client.navigate(url)))
                 .catch(unreached_rejection(t));
    }, "Navigating to mixed-content iframe should reject with TypeError");
    done();
  } else if (test === "test_client_navigate_redirect") {
    var host_info = get_host_info();
    var url = new URL(host_info['HTTPS_REMOTE_ORIGIN']).toString() +
              new URL("client-navigated-frame.html", location).pathname.substring(1);
    promise_test(function(t) {
      this.add_cleanup(() => port.postMessage(pass(test, clientUrl)));
      return self.clients.get(clientId)
                 .then(client => client.navigate("redirect.py?Redirect=" + url))
                 .then(client => {
                   clientUrl = (client && client.url) || ""
                   assert_equals(client, null);
                 })
                 .catch(unreached_rejection(t));
    }, "Redirecting to another origin should resolve with null");
    done();
  }
};