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
|
// Verifies that non-local HTTP(S) ports are open and serve correctly.
//
// See the corresponding WPT RFC:
// https://github.com/web-platform-tests/rfcs/blob/master/rfcs/address_space_overrides.md
//
// These ports are used to test the Private Network Access specification:
// https://wicg.github.io/private-network-access/
//
// More tests can be found in `fetch/private-network-access/`.
const alternatePorts = {
httpPrivate: "{{ports[http-private][0]}}",
httpsPrivate: "{{ports[https-private][0]}}",
httpPublic: "{{ports[http-public][0]}}",
httpsPublic: "{{ports[https-public][0]}}",
};
// Resolves a URL relative to the current location, returning an absolute URL.
//
// `url` specifies the relative URL, e.g. "foo.html" or "http://foo.example".
// `options.protocol` and `options.port`, if defined, override the respective
// properties of the returned URL object.
function resolveUrl(url, options) {
const result = new URL(url, window.location);
if (options === undefined) {
return result;
}
const { port, protocol } = options;
if (port !== undefined) {
result.port = port;
}
if (protocol !== undefined) {
result.protocol = protocol;
}
return result;
}
const alternateOrigins = {
httpPrivate: {
protocol: "http:",
port: alternatePorts.httpPrivate,
},
httpsPrivate: {
protocol: "https:",
port: alternatePorts.httpsPrivate,
},
httpPublic: {
protocol: "http:",
port: alternatePorts.httpPublic,
},
httpsPublic: {
protocol: "https:",
port: alternatePorts.httpsPublic,
},
};
promise_test(async () => {
const url =
resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsPrivate);
const response = await fetch(url);
assert_true(response.ok);
}, "Fetch from https-private port works.");
promise_test(async () => {
const url =
resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpPrivate);
const response = await fetch(url);
assert_true(response.ok);
}, "Fetch from http-private port works.");
promise_test(async () => {
const url =
resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpsPublic);
const response = await fetch(url);
assert_true(response.ok);
}, "Fetch from https-public port works.");
promise_test(async () => {
const url =
resolveUrl("/common/blank-with-cors.html", alternateOrigins.httpPublic);
const response = await fetch(url);
assert_true(response.ok);
}, "Fetch from http-public port works.");
promise_test(async (t) => {
const futureMessage = new Promise((resolve) => {
window.addEventListener("message", resolve);
});
const iframe = await new Promise((resolve, reject) => {
const iframe = document.createElement("iframe");
iframe.src = resolveUrl("resources/fetch-and-post-result.html",
alternateOrigins.httpPublic);
iframe.onload = () => { resolve(iframe); };
iframe.onerror = reject;
document.body.appendChild(iframe);
t.add_cleanup(() => {
document.body.removeChild(iframe);
});
});
iframe.contentWindow.postMessage(
resolveUrl("/common/blank-with-cors.html").toString(), "*");
const evt = await futureMessage;
assert_equals(evt.data, "failure: error = TypeError");
}, "Fetch from http-public to local http fails.");
|