summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/scroll-to-text-fragment/redirects.html
blob: 71bc1be02e65be68eced1ef7e3e492d1bff6cffd (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
<!doctype html>
<title>TextFragment invoked on redirects</title>
<meta charset=utf-8>
<meta name="timeout" content="long">
<link rel="help" href="https://wicg.github.io/ScrollToTextFragment/">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script src="/common/get-host-info.sub.js"></script>
<script src="/common/utils.js"></script>
<script src="stash.js"></script>

<!--See comment in scroll-to-text-fragment.html for why these tests have the
structure they do. -->
<script>
// This test ensure correct operation of text-fragments through both HTTP and
// client side redirects in various scenarios.

// Constructs a URL to either redirect.py or the local client-redirect.html;
// which will cause an HTTP or client based redirect, respectively, to
// |to_url|. |type| provides a numeric 30x code to specify an HTTP redirect,
// "location" for a write to window.location, or "meta" for a <meta> refresh.
function buildRedirectUrl(to_url, type) {
  let dest = "";
  to_url = encodeURIComponent(to_url);

  if (typeof type == "number") {
    // If the type is a number, it's an HTTP response code, use redirect.py to
    // respond with an HTTP redirect.
    const code = type;
    dest = `${get_host_info().ORIGIN}/common/redirect.py?status=${code}&location=${to_url}`;
  } else if (type == 'meta' || type == 'location') {
    // Otherwise we're requesting a client-side redirect, either a <meta> tag
    // or window.location. Use the client-redirect file to bounce to the
    // destination.
    dest = `client-redirect.html?${type}&${to_url}`;
  }
  return dest;
}

// Turns |path| from a relative-to-this-file path into a full URL.
function relativePathToFull(path) {
  const pathname = window.location.toString();
  const base_path = pathname.substring(0, pathname.lastIndexOf('/') + 1);
  return base_path + path;
}

const status_codes = [301, 302, 303, 307, 308];

// Test that an HTTP redirect to a URL with a text fragment invokes the
// fragment.
for (let code of status_codes) {
  promise_test(t => new Promise((resolve, reject) => {
    let key = token();

    const abs_url = relativePathToFull(`redirects-target.html?key=${key}#:~:text=target`);
    const url = buildRedirectUrl(abs_url, code);

    test_driver.bless('Open a URL with a text fragment directive', () => {
      window.open(url, '_blank', 'noopener');
    });

    fetchResults(key, resolve, reject);
  }).then(data => {
    assert_equals(data.scrolled, true);
  }), `Text fragment works from HTTP ${code} redirect.`);
}

// Test that a URL with a text fragment that causes an HTTP redirect preserves
// the fragment and invokes it on the destination page.
for (let code of status_codes) {
  promise_test(t => new Promise((resolve, reject) => {
    let key = token();

    const abs_url = relativePathToFull(`redirects-target.html?key=${key}`);
    const url = buildRedirectUrl(abs_url, code) + "#:~:text=target";

    test_driver.bless('Open a URL with a text fragment directive', () => {
      window.open(url, '_blank', 'noopener');
    });

    fetchResults(key, resolve, reject);
  }).then(data => {
    assert_equals(data.scrolled, true);
  }), `Text fragment propagated through HTTP ${code} redirect.`);
}

// Test that client-side redirects (using script) to a URL with a text fragment
// cause the text fragment to be invoked.
for (let type of ['location', 'meta']) {
  promise_test(t => new Promise((resolve, reject) => {
    let key = token();

    const to_url = `redirects-target.html?key=${key}#:~:text=target`
    const url = buildRedirectUrl(to_url, type);

    test_driver.bless('Open a URL with a text fragment directive', () => {
      window.open(url, '_blank', 'noopener');
    });

    fetchResults(key, resolve, reject);
  }).then(data => {
    assert_equals(data.scrolled, true);
  }), `Text fragment works on client-side ${type} redirect.`);
}

// Test that client-side redirects (using script) to a URL with a text fragment
// cause the text fragment to be invoked only the first time. A further
// redirect without a user gesture is blocked.
for (let type of ['location', 'meta']) {
  promise_test(t => new Promise((resolve, reject) => {
    let key = token();

    const to_url = `redirects-target.html?twice&key=${key}#:~:text=target`
    const url = buildRedirectUrl(to_url, type);

    test_driver.bless('Open a URL with a text fragment directive', () => {
      window.open(url, '_blank', 'noopener');
    });

    fetchResults(key, resolve, reject);
  }).then(data => {
    assert_equals(data.scrolled, false);
  }), `One text fragment per user gesture allowed in client-side ${type} redirect.`);
}
</script>