зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1596935 - Add tests for rel="dns-prefetch" r=necko-reviewers,valentin
Provide a mechanism to allows dns prefetch via proxy (for mochi browser tests), testing only. And ensure that we resolve the dns-prefetched domain. Differential Revision: https://phabricator.services.mozilla.com/D206550
This commit is contained in:
Родитель
3916243a2b
Коммит
a9a0c7519b
|
@ -12360,6 +12360,12 @@
|
|||
value: false
|
||||
mirror: always
|
||||
|
||||
# For testing purpose only: allow dns prefetch through proxies
|
||||
- name: network.dns.prefetch_via_proxy
|
||||
type: bool
|
||||
value: false
|
||||
mirror: always
|
||||
|
||||
# Max time to shutdown the resolver threads
|
||||
- name: network.dns.resolver_shutdown_timeout_ms
|
||||
type: uint32_t
|
||||
|
|
|
@ -2292,6 +2292,10 @@ void nsProtocolProxyService::MaybeDisableDNSPrefetch(nsIProxyInfo* aProxy) {
|
|||
nsCOMPtr<nsProxyInfo> pi = do_QueryInterface(aProxy);
|
||||
if (!pi || !pi->mType || pi->mType == kProxyType_DIRECT) return;
|
||||
|
||||
if (StaticPrefs::network_dns_prefetch_via_proxy()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// To avoid getting DNS service recursively, we directly use
|
||||
// GetXPCOMSingleton().
|
||||
nsCOMPtr<nsIDNSService> dns = nsDNSService::GetXPCOMSingleton();
|
||||
|
|
|
@ -72,6 +72,7 @@ support-files = [
|
|||
"x_frame_options.html^headers^",
|
||||
"test_1629307.html",
|
||||
"file_link_header.sjs",
|
||||
"file_link_dns_prefetch.sjs",
|
||||
]
|
||||
|
||||
prefs = [
|
||||
|
@ -156,6 +157,8 @@ skip-if = ["os == 'win'"] # Bug 1775761
|
|||
|
||||
["browser_cookie_sync_across_tabs.js"]
|
||||
|
||||
["browser_dns_prefetch_link_header.js"]
|
||||
|
||||
["browser_fetch_lnk.js"]
|
||||
run-if = ["os == 'win'"]
|
||||
support-files = ["file_lnk.lnk",]
|
||||
|
|
|
@ -0,0 +1,281 @@
|
|||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
|
||||
// Test steps:
|
||||
// 1. Load file_link_dns_prefetch.sjs
|
||||
// 2.`<link rel="dns-prefetch" href="https://example.org">` is in
|
||||
// the server-side sjs, so we will make the dns-request.
|
||||
// 3. We verify that the dns request was made
|
||||
|
||||
const gDashboard = Cc["@mozilla.org/network/dashboard;1"].getService(
|
||||
Ci.nsIDashboard
|
||||
);
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
// To observe DNS requests when running via the mochitest proxy we must first take a few steps:
|
||||
//
|
||||
// 1. Update the mochitest proxy pac file to include dns resolution.
|
||||
// We do this by injecting "dnsResolve(host);" into the `FindProxyForURL()` pac function.
|
||||
let existingPACScript = Services.prefs.getCharPref(
|
||||
"network.proxy.autoconfig_url"
|
||||
);
|
||||
|
||||
let findProxyForURLFunction = "function FindProxyForURL(url, host){";
|
||||
let directDnsPacScript = existingPACScript.replace(
|
||||
findProxyForURLFunction,
|
||||
`${findProxyForURLFunction}
|
||||
dnsResolve(host);
|
||||
`
|
||||
);
|
||||
Services.prefs.setStringPref(
|
||||
"network.proxy.autoconfig_url",
|
||||
directDnsPacScript
|
||||
);
|
||||
|
||||
// 2. Ensure we don't disable dns prefetch despite using a proxy (this would otherwise happen after every request that the proxy completed)
|
||||
Services.prefs.setBoolPref("network.dns.prefetch_via_proxy", true);
|
||||
|
||||
// 3. And finally enable dns prefetching via the private dns service api (generally disabled in mochitest proxy)
|
||||
Services.dns.QueryInterface(Ci.nsPIDNSService).prefetchEnabled = true;
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
registerCleanupFunction(function () {
|
||||
// Restore proxy pac and dns prefetch behaviour via proxy
|
||||
Services.prefs.setCharPref("network.proxy.autoconfig_url", existingPACScript);
|
||||
Services.prefs.clearUserPref("network.dns.prefetch_via_proxy");
|
||||
Services.dns.QueryInterface(Ci.nsPIDNSService).prefetchEnabled = false;
|
||||
});
|
||||
|
||||
async function isRecordFound(hostname) {
|
||||
return new Promise(resolve => {
|
||||
gDashboard.requestDNSInfo(function (data) {
|
||||
let found = false;
|
||||
for (let i = 0; i < data.entries.length; i++) {
|
||||
if (data.entries[i].hostname == hostname) {
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
resolve(found);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
let https_requestUrl = `https://example.com/browser/netwerk/test/browser/file_link_dns_prefetch.sjs`;
|
||||
let http_requestUrl = `http://example.com/browser/netwerk/test/browser/file_link_dns_prefetch.sjs`; // eslint-disable-line @microsoft/sdl/no-insecure-url
|
||||
|
||||
// Test dns-prefetch on https
|
||||
add_task(async function test_https_dns_prefetch() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: https_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {}
|
||||
);
|
||||
|
||||
Assert.ok(await isRecordFound("example.com"), "Host record should be found");
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should be found"
|
||||
);
|
||||
});
|
||||
|
||||
// Test dns-prefetch on http
|
||||
add_task(async function test_http_dns_prefetch() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: http_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {}
|
||||
);
|
||||
|
||||
Assert.ok(await isRecordFound("example.com"), "Host record should be found");
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should be found"
|
||||
);
|
||||
});
|
||||
|
||||
// Test dns-prefetch on https with the feature disabled
|
||||
add_task(async function test_https_dns_prefetch_disabled() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
// Disable the feature to verify that it will not prefetch
|
||||
Services.prefs.setBoolPref("network.dns.disablePrefetchFromHTTPS", true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: https_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {}
|
||||
);
|
||||
|
||||
Assert.ok(await isRecordFound("example.com"), "Host record should be found");
|
||||
Assert.ok(
|
||||
!(await isRecordFound("example.org")),
|
||||
"Prefetched dns record should not be found with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
|
||||
Services.prefs.clearUserPref("network.dns.disablePrefetchFromHTTPS");
|
||||
});
|
||||
|
||||
// Test dns-prefetch on http with the feature disabled
|
||||
add_task(async function test_http_dns_prefetch_disabled() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
// Disable the feature to verify, but this test is http, and so prefetch will execute
|
||||
Services.prefs.setBoolPref("network.dns.disablePrefetchFromHTTPS", true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: http_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {}
|
||||
);
|
||||
|
||||
Assert.ok(await isRecordFound("example.com"), "Host record should be found");
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should still be found on http page with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
|
||||
Services.prefs.clearUserPref("network.dns.disablePrefetchFromHTTPS");
|
||||
});
|
||||
|
||||
// Test if we speculatively prefetch dns for anchor elements on https documents
|
||||
add_task(async function test_https_anchor_speculative_dns_prefetch() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: https_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {}
|
||||
);
|
||||
|
||||
Assert.ok(await isRecordFound("example.com"), "Host record should be found");
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should still be found on http page with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
Assert.ok(
|
||||
!(await isRecordFound("www.mozilla.org")),
|
||||
"By default we do not speculatively prefetch dns for anchor elements on https documents"
|
||||
);
|
||||
|
||||
// And enable the pref to verify that it works
|
||||
Services.prefs.setBoolPref(
|
||||
"dom.prefetch_dns_for_anchor_https_document",
|
||||
true
|
||||
);
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: https_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {
|
||||
Assert.ok(
|
||||
await isRecordFound("example.com"),
|
||||
"Host record should be found"
|
||||
);
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should still be found on http page with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
|
||||
// The anchor element prefetchs are sent after pageload event; wait for them
|
||||
await TestUtils.waitForCondition(() => {
|
||||
return isRecordFound("www.mozilla.org");
|
||||
});
|
||||
|
||||
Assert.ok(
|
||||
await isRecordFound("www.mozilla.org"),
|
||||
"Speculatively prefetch dns for anchor elements on https documents"
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Services.prefs.clearUserPref("dom.prefetch_dns_for_anchor_https_document");
|
||||
});
|
||||
|
||||
// Test that we speculatively prefetch dns for anchor elements on http documents
|
||||
add_task(async function test_http_anchor_speculative_dns_prefetch() {
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: http_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {
|
||||
Assert.ok(
|
||||
await isRecordFound("example.com"),
|
||||
"Host record should be found"
|
||||
);
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should still be found on http page with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
|
||||
// The anchor element prefetchs are sent after pageload event; wait for them
|
||||
await TestUtils.waitForCondition(() => {
|
||||
return isRecordFound("www.mozilla.org");
|
||||
});
|
||||
|
||||
Assert.ok(
|
||||
await isRecordFound("www.mozilla.org"),
|
||||
"By default we speculatively prefetch dns for anchor elements on http documents"
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
// And disable the pref to verify that we no longer make the requests
|
||||
Services.prefs.setBoolPref(
|
||||
"dom.prefetch_dns_for_anchor_http_document",
|
||||
false
|
||||
);
|
||||
Services.dns.clearCache(true);
|
||||
|
||||
await BrowserTestUtils.withNewTab(
|
||||
{
|
||||
gBrowser,
|
||||
url: http_requestUrl,
|
||||
waitForLoad: true,
|
||||
},
|
||||
async function () {
|
||||
Assert.ok(
|
||||
await isRecordFound("example.com"),
|
||||
"Host record should be found"
|
||||
);
|
||||
Assert.ok(
|
||||
await isRecordFound("example.org"),
|
||||
"Prefetched dns record should still be found on http page with disablePrefetchFromHTTPS set"
|
||||
);
|
||||
Assert.ok(
|
||||
!(await isRecordFound("www.mozilla.org")),
|
||||
"We disabled speculative prefetch dns for anchor elements on http documents"
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
Services.prefs.clearUserPref("dom.prefetch_dns_for_anchor_http_document");
|
||||
});
|
|
@ -0,0 +1,25 @@
|
|||
"use strict";
|
||||
|
||||
function handleRequest(request, response) {
|
||||
// write to raw socket
|
||||
response.seizePower();
|
||||
let body = `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<link rel="dns-prefetch" href="https://example.org">
|
||||
</head>
|
||||
<body>
|
||||
<h1>Test rel=dns-prefetch<h1>
|
||||
<a href="https://www.mozilla.org"> Test link </a>
|
||||
</body>
|
||||
</html>`;
|
||||
|
||||
response.write("HTTP/1.1 200 OK\r\n");
|
||||
response.write("Content-Type: text/html;charset=utf-8\r\n");
|
||||
response.write("Cache-Control: no-cache\r\n");
|
||||
response.write(`Content-Length: ${body.length}\r\n`);
|
||||
response.write("\r\n");
|
||||
response.write(body);
|
||||
|
||||
response.finish();
|
||||
}
|
Загрузка…
Ссылка в новой задаче