1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/mochitests/test_user_agent_overrides.html Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,232 @@ 1.4 +<!DOCTYPE HTML> 1.5 +<html> 1.6 +<!-- 1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=782453 1.8 +--> 1.9 +<head> 1.10 + <title>Test for User Agent Overrides</title> 1.11 + <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script> 1.12 + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 1.13 +</head> 1.14 +<body> 1.15 +<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=782453">Mozilla Bug 782453</a> 1.16 +<p id="display"></p> 1.17 +<div id="content" style="display: none"></div> 1.18 +<pre id="test"> 1.19 +<script class="testbody" type="text/javascript"> 1.20 + 1.21 +const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides"; 1.22 +const PREF_OVERRIDES_BRANCH = "general.useragent.override."; 1.23 + 1.24 +const DEFAULT_UA = navigator.userAgent; 1.25 + 1.26 +const UA_WHOLE_OVERRIDE = "DummyUserAgent"; 1.27 +const UA_WHOLE_EXPECTED = UA_WHOLE_OVERRIDE; 1.28 + 1.29 +const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla 1.30 +const UA_PARTIAL_SEP = "#"; 1.31 +const UA_PARTIAL_TO = UA_WHOLE_OVERRIDE; 1.32 +const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO; 1.33 +const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO); 1.34 + 1.35 +function getUA(host) { 1.36 + var url = location.pathname; 1.37 + url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs'; 1.38 + 1.39 + var xhr = new XMLHttpRequest(); 1.40 + xhr.open('GET', url, false); // sync request 1.41 + xhr.send(); 1.42 + is(xhr.status, 200, 'request failed'); 1.43 + is(typeof xhr.response, 'string', 'invalid response'); 1.44 + return xhr.response; 1.45 +} 1.46 + 1.47 +function testUA(options, callback) { 1.48 + 1.49 + var [domain, override, test_hosts, expected] = 1.50 + [options.domain, options.override, options.test_hosts, options.expected]; 1.51 + 1.52 + info('Overriding ' + domain + ' with ' + override); 1.53 + 1.54 + function is_subdomain(host) { 1.55 + var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1); 1.56 + return test_domain === domain || test_domain.endsWith('.' + domain); 1.57 + } 1.58 + 1.59 + var localhost = location.origin; 1.60 + var overrideNavigator = is_subdomain(localhost); 1.61 + var navigator_ua, test_ua = []; 1.62 + 1.63 + // store UA before pref change, to be compared later 1.64 + if (overrideNavigator) { 1.65 + navigator_ua = navigator.userAgent; 1.66 + } 1.67 + test_hosts.forEach(function (test_host) { 1.68 + test_ua.push(getUA(test_host)); 1.69 + }); 1.70 + // set the override pref to override the UA 1.71 + SpecialPowers.pushPrefEnv({ 1.72 + set: [[PREF_OVERRIDES_BRANCH + domain, override]], 1.73 + }, function () { 1.74 + // check that the UA has changed after pref change 1.75 + if (overrideNavigator) { 1.76 + is(navigator.userAgent, expected, 1.77 + 'Navigator UA not overridden at step ' + (++step)); 1.78 + } else { 1.79 + is(navigator.userAgent, DEFAULT_UA, 1.80 + 'Navigator UA should not be overridden at step ' + (++step)); 1.81 + } 1.82 + test_hosts.forEach(function (test_host) { 1.83 + is(getUA(test_host), expected, 1.84 + 'Header UA not overridden at step ' + (++step)); 1.85 + }); 1.86 + // clear the override pref to undo overriding the UA 1.87 + SpecialPowers.pushPrefEnv({ 1.88 + clear: [[PREF_OVERRIDES_BRANCH + domain]], 1.89 + }, function () { 1.90 + // check that the UA has changed back 1.91 + if (overrideNavigator) { 1.92 + is(navigator.userAgent, navigator_ua, 1.93 + 'Navigator UA not restored at step ' + (++step)); 1.94 + } 1.95 + test_hosts.forEach(function (test_host, i) { 1.96 + is(getUA(test_host), test_ua[i], 1.97 + 'Header UA not restored at step ' + (++step)); 1.98 + }); 1.99 + callback(); 1.100 + }); 1.101 + }); 1.102 +} 1.103 + 1.104 + 1.105 +var step = 0; // for logging 1.106 +var tests = [ 1.107 + // should override both header and navigator.userAgent 1.108 + { 1.109 + domain: location.hostname, 1.110 + override: UA_WHOLE_OVERRIDE, 1.111 + test_hosts: [location.origin], 1.112 + expected: UA_WHOLE_EXPECTED 1.113 + }, 1.114 + 1.115 + // should support partial overrides 1.116 + { 1.117 + domain: location.hostname, 1.118 + override: UA_PARTIAL_OVERRIDE, 1.119 + test_hosts: [location.origin], 1.120 + expected: UA_PARTIAL_EXPECTED 1.121 + }, 1.122 + 1.123 + // should match domain and subdomains 1.124 + { 1.125 + domain: 'example.org', 1.126 + override: UA_WHOLE_OVERRIDE, 1.127 + test_hosts: ['http://example.org', 1.128 + 'http://test1.example.org', 1.129 + 'http://sub1.test1.example.org'], 1.130 + expected: UA_WHOLE_EXPECTED 1.131 + }, 1.132 + 1.133 + // should not match superdomains 1.134 + { 1.135 + domain: 'sub1.test1.example.org', 1.136 + override: UA_WHOLE_OVERRIDE, 1.137 + test_hosts: ['http://example.org', 1.138 + 'http://test1.example.org'], 1.139 + expected: DEFAULT_UA 1.140 + }, 1.141 + 1.142 + // should work with https 1.143 + { 1.144 + domain: 'example.com', 1.145 + override: UA_WHOLE_OVERRIDE, 1.146 + test_hosts: ['https://example.com', 1.147 + 'https://test1.example.com', 1.148 + 'https://sub1.test1.example.com'], 1.149 + expected: UA_WHOLE_EXPECTED 1.150 + }, 1.151 +]; 1.152 + 1.153 +// test that UA is not overridden when the 'site_specific_overrides' pref is off 1.154 +function testInactive(callback) { 1.155 + SpecialPowers.pushPrefEnv({ 1.156 + set: [ 1.157 + [PREF_OVERRIDES_ENABLED, false], 1.158 + [PREF_OVERRIDES_BRANCH + location.hostname, UA_WHOLE_OVERRIDE] 1.159 + ] 1.160 + }, function () { 1.161 + isnot(navigator.userAgent, UA_WHOLE_OVERRIDE, 1.162 + 'Failed to disable navigator UA override'); 1.163 + isnot(getUA(location.origin), UA_WHOLE_OVERRIDE, 1.164 + 'Failed to disable header UA override'); 1.165 + SpecialPowers.pushPrefEnv({ 1.166 + clear: [ 1.167 + [PREF_OVERRIDES_ENABLED], 1.168 + [PREF_OVERRIDES_BRANCH + location.hostname] 1.169 + ] 1.170 + }, callback); 1.171 + }); 1.172 +} 1.173 + 1.174 +function testPriority(callback) { 1.175 + // foo.bar.com override should have priority over bar.com override 1.176 + var tests = [ 1.177 + ['example.org', 'test1.example.org', 'sub1.test1.example.org'], 1.178 + ['example.org', 'test1.example.org', 'sub2.test1.example.org'], 1.179 + ['example.org', 'test2.example.org', 'sub1.test2.example.org'], 1.180 + ['example.org', 'test2.example.org', 'sub2.test2.example.org'], 1.181 + ]; 1.182 + (function nextTest() { 1.183 + var [level0, level1, level2] = tests.shift(); 1.184 + var host = 'http://' + level2; 1.185 + SpecialPowers.pushPrefEnv({ 1.186 + set: [ 1.187 + [PREF_OVERRIDES_ENABLED, true], 1.188 + [PREF_OVERRIDES_BRANCH + level1, UA_WHOLE_OVERRIDE] 1.189 + ] 1.190 + }, function () { 1.191 + // should use first override at this point 1.192 + is(getUA(host), 1.193 + UA_WHOLE_EXPECTED, 'UA not overridden'); 1.194 + // add a second override that should be used 1.195 + testUA({ 1.196 + domain: level2, 1.197 + override: UA_PARTIAL_OVERRIDE, 1.198 + test_hosts: [host], 1.199 + expected: UA_PARTIAL_EXPECTED 1.200 + }, function () { 1.201 + // add a third override that should not be used 1.202 + testUA({ 1.203 + domain: level0, 1.204 + override: UA_PARTIAL_OVERRIDE, 1.205 + test_hosts: [host], 1.206 + expected: UA_WHOLE_EXPECTED 1.207 + }, tests.length ? nextTest : callback); 1.208 + }); 1.209 + }); 1.210 + })(); 1.211 +} 1.212 + 1.213 +function testOverrides(callback) { 1.214 + SpecialPowers.pushPrefEnv({ 1.215 + set: [[PREF_OVERRIDES_ENABLED, true]] 1.216 + }, function nextTest() { 1.217 + testUA(tests.shift(), function () tests.length ? nextTest() : callback()); 1.218 + }); 1.219 +} 1.220 + 1.221 +SpecialPowers.Cu.import('resource://gre/modules/UserAgentOverrides.jsm', window); 1.222 +SpecialPowers.wrap(UserAgentOverrides).init(); 1.223 + 1.224 +SimpleTest.waitForExplicitFinish(); 1.225 + 1.226 +testOverrides(function () 1.227 + testInactive(function () 1.228 + testPriority(SimpleTest.finish) 1.229 + ) 1.230 +); 1.231 + 1.232 +</script> 1.233 +</pre> 1.234 +</body> 1.235 +</html>