netwerk/test/mochitests/test_user_agent_overrides.html

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 <!DOCTYPE HTML>
michael@0 2 <html>
michael@0 3 <!--
michael@0 4 https://bugzilla.mozilla.org/show_bug.cgi?id=782453
michael@0 5 -->
michael@0 6 <head>
michael@0 7 <title>Test for User Agent Overrides</title>
michael@0 8 <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
michael@0 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
michael@0 10 </head>
michael@0 11 <body>
michael@0 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=782453">Mozilla Bug 782453</a>
michael@0 13 <p id="display"></p>
michael@0 14 <div id="content" style="display: none"></div>
michael@0 15 <pre id="test">
michael@0 16 <script class="testbody" type="text/javascript">
michael@0 17
michael@0 18 const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
michael@0 19 const PREF_OVERRIDES_BRANCH = "general.useragent.override.";
michael@0 20
michael@0 21 const DEFAULT_UA = navigator.userAgent;
michael@0 22
michael@0 23 const UA_WHOLE_OVERRIDE = "DummyUserAgent";
michael@0 24 const UA_WHOLE_EXPECTED = UA_WHOLE_OVERRIDE;
michael@0 25
michael@0 26 const UA_PARTIAL_FROM = "\\wozilla"; // /\wozilla
michael@0 27 const UA_PARTIAL_SEP = "#";
michael@0 28 const UA_PARTIAL_TO = UA_WHOLE_OVERRIDE;
michael@0 29 const UA_PARTIAL_OVERRIDE = UA_PARTIAL_FROM + UA_PARTIAL_SEP + UA_PARTIAL_TO;
michael@0 30 const UA_PARTIAL_EXPECTED = DEFAULT_UA.replace(new RegExp(UA_PARTIAL_FROM, 'g'), UA_PARTIAL_TO);
michael@0 31
michael@0 32 function getUA(host) {
michael@0 33 var url = location.pathname;
michael@0 34 url = host + url.slice(0, url.lastIndexOf('/')) + '/user_agent.sjs';
michael@0 35
michael@0 36 var xhr = new XMLHttpRequest();
michael@0 37 xhr.open('GET', url, false); // sync request
michael@0 38 xhr.send();
michael@0 39 is(xhr.status, 200, 'request failed');
michael@0 40 is(typeof xhr.response, 'string', 'invalid response');
michael@0 41 return xhr.response;
michael@0 42 }
michael@0 43
michael@0 44 function testUA(options, callback) {
michael@0 45
michael@0 46 var [domain, override, test_hosts, expected] =
michael@0 47 [options.domain, options.override, options.test_hosts, options.expected];
michael@0 48
michael@0 49 info('Overriding ' + domain + ' with ' + override);
michael@0 50
michael@0 51 function is_subdomain(host) {
michael@0 52 var [test_domain] = host.slice(host.lastIndexOf('/') + 1).split(':', 1);
michael@0 53 return test_domain === domain || test_domain.endsWith('.' + domain);
michael@0 54 }
michael@0 55
michael@0 56 var localhost = location.origin;
michael@0 57 var overrideNavigator = is_subdomain(localhost);
michael@0 58 var navigator_ua, test_ua = [];
michael@0 59
michael@0 60 // store UA before pref change, to be compared later
michael@0 61 if (overrideNavigator) {
michael@0 62 navigator_ua = navigator.userAgent;
michael@0 63 }
michael@0 64 test_hosts.forEach(function (test_host) {
michael@0 65 test_ua.push(getUA(test_host));
michael@0 66 });
michael@0 67 // set the override pref to override the UA
michael@0 68 SpecialPowers.pushPrefEnv({
michael@0 69 set: [[PREF_OVERRIDES_BRANCH + domain, override]],
michael@0 70 }, function () {
michael@0 71 // check that the UA has changed after pref change
michael@0 72 if (overrideNavigator) {
michael@0 73 is(navigator.userAgent, expected,
michael@0 74 'Navigator UA not overridden at step ' + (++step));
michael@0 75 } else {
michael@0 76 is(navigator.userAgent, DEFAULT_UA,
michael@0 77 'Navigator UA should not be overridden at step ' + (++step));
michael@0 78 }
michael@0 79 test_hosts.forEach(function (test_host) {
michael@0 80 is(getUA(test_host), expected,
michael@0 81 'Header UA not overridden at step ' + (++step));
michael@0 82 });
michael@0 83 // clear the override pref to undo overriding the UA
michael@0 84 SpecialPowers.pushPrefEnv({
michael@0 85 clear: [[PREF_OVERRIDES_BRANCH + domain]],
michael@0 86 }, function () {
michael@0 87 // check that the UA has changed back
michael@0 88 if (overrideNavigator) {
michael@0 89 is(navigator.userAgent, navigator_ua,
michael@0 90 'Navigator UA not restored at step ' + (++step));
michael@0 91 }
michael@0 92 test_hosts.forEach(function (test_host, i) {
michael@0 93 is(getUA(test_host), test_ua[i],
michael@0 94 'Header UA not restored at step ' + (++step));
michael@0 95 });
michael@0 96 callback();
michael@0 97 });
michael@0 98 });
michael@0 99 }
michael@0 100
michael@0 101
michael@0 102 var step = 0; // for logging
michael@0 103 var tests = [
michael@0 104 // should override both header and navigator.userAgent
michael@0 105 {
michael@0 106 domain: location.hostname,
michael@0 107 override: UA_WHOLE_OVERRIDE,
michael@0 108 test_hosts: [location.origin],
michael@0 109 expected: UA_WHOLE_EXPECTED
michael@0 110 },
michael@0 111
michael@0 112 // should support partial overrides
michael@0 113 {
michael@0 114 domain: location.hostname,
michael@0 115 override: UA_PARTIAL_OVERRIDE,
michael@0 116 test_hosts: [location.origin],
michael@0 117 expected: UA_PARTIAL_EXPECTED
michael@0 118 },
michael@0 119
michael@0 120 // should match domain and subdomains
michael@0 121 {
michael@0 122 domain: 'example.org',
michael@0 123 override: UA_WHOLE_OVERRIDE,
michael@0 124 test_hosts: ['http://example.org',
michael@0 125 'http://test1.example.org',
michael@0 126 'http://sub1.test1.example.org'],
michael@0 127 expected: UA_WHOLE_EXPECTED
michael@0 128 },
michael@0 129
michael@0 130 // should not match superdomains
michael@0 131 {
michael@0 132 domain: 'sub1.test1.example.org',
michael@0 133 override: UA_WHOLE_OVERRIDE,
michael@0 134 test_hosts: ['http://example.org',
michael@0 135 'http://test1.example.org'],
michael@0 136 expected: DEFAULT_UA
michael@0 137 },
michael@0 138
michael@0 139 // should work with https
michael@0 140 {
michael@0 141 domain: 'example.com',
michael@0 142 override: UA_WHOLE_OVERRIDE,
michael@0 143 test_hosts: ['https://example.com',
michael@0 144 'https://test1.example.com',
michael@0 145 'https://sub1.test1.example.com'],
michael@0 146 expected: UA_WHOLE_EXPECTED
michael@0 147 },
michael@0 148 ];
michael@0 149
michael@0 150 // test that UA is not overridden when the 'site_specific_overrides' pref is off
michael@0 151 function testInactive(callback) {
michael@0 152 SpecialPowers.pushPrefEnv({
michael@0 153 set: [
michael@0 154 [PREF_OVERRIDES_ENABLED, false],
michael@0 155 [PREF_OVERRIDES_BRANCH + location.hostname, UA_WHOLE_OVERRIDE]
michael@0 156 ]
michael@0 157 }, function () {
michael@0 158 isnot(navigator.userAgent, UA_WHOLE_OVERRIDE,
michael@0 159 'Failed to disable navigator UA override');
michael@0 160 isnot(getUA(location.origin), UA_WHOLE_OVERRIDE,
michael@0 161 'Failed to disable header UA override');
michael@0 162 SpecialPowers.pushPrefEnv({
michael@0 163 clear: [
michael@0 164 [PREF_OVERRIDES_ENABLED],
michael@0 165 [PREF_OVERRIDES_BRANCH + location.hostname]
michael@0 166 ]
michael@0 167 }, callback);
michael@0 168 });
michael@0 169 }
michael@0 170
michael@0 171 function testPriority(callback) {
michael@0 172 // foo.bar.com override should have priority over bar.com override
michael@0 173 var tests = [
michael@0 174 ['example.org', 'test1.example.org', 'sub1.test1.example.org'],
michael@0 175 ['example.org', 'test1.example.org', 'sub2.test1.example.org'],
michael@0 176 ['example.org', 'test2.example.org', 'sub1.test2.example.org'],
michael@0 177 ['example.org', 'test2.example.org', 'sub2.test2.example.org'],
michael@0 178 ];
michael@0 179 (function nextTest() {
michael@0 180 var [level0, level1, level2] = tests.shift();
michael@0 181 var host = 'http://' + level2;
michael@0 182 SpecialPowers.pushPrefEnv({
michael@0 183 set: [
michael@0 184 [PREF_OVERRIDES_ENABLED, true],
michael@0 185 [PREF_OVERRIDES_BRANCH + level1, UA_WHOLE_OVERRIDE]
michael@0 186 ]
michael@0 187 }, function () {
michael@0 188 // should use first override at this point
michael@0 189 is(getUA(host),
michael@0 190 UA_WHOLE_EXPECTED, 'UA not overridden');
michael@0 191 // add a second override that should be used
michael@0 192 testUA({
michael@0 193 domain: level2,
michael@0 194 override: UA_PARTIAL_OVERRIDE,
michael@0 195 test_hosts: [host],
michael@0 196 expected: UA_PARTIAL_EXPECTED
michael@0 197 }, function () {
michael@0 198 // add a third override that should not be used
michael@0 199 testUA({
michael@0 200 domain: level0,
michael@0 201 override: UA_PARTIAL_OVERRIDE,
michael@0 202 test_hosts: [host],
michael@0 203 expected: UA_WHOLE_EXPECTED
michael@0 204 }, tests.length ? nextTest : callback);
michael@0 205 });
michael@0 206 });
michael@0 207 })();
michael@0 208 }
michael@0 209
michael@0 210 function testOverrides(callback) {
michael@0 211 SpecialPowers.pushPrefEnv({
michael@0 212 set: [[PREF_OVERRIDES_ENABLED, true]]
michael@0 213 }, function nextTest() {
michael@0 214 testUA(tests.shift(), function () tests.length ? nextTest() : callback());
michael@0 215 });
michael@0 216 }
michael@0 217
michael@0 218 SpecialPowers.Cu.import('resource://gre/modules/UserAgentOverrides.jsm', window);
michael@0 219 SpecialPowers.wrap(UserAgentOverrides).init();
michael@0 220
michael@0 221 SimpleTest.waitForExplicitFinish();
michael@0 222
michael@0 223 testOverrides(function ()
michael@0 224 testInactive(function ()
michael@0 225 testPriority(SimpleTest.finish)
michael@0 226 )
michael@0 227 );
michael@0 228
michael@0 229 </script>
michael@0 230 </pre>
michael@0 231 </body>
michael@0 232 </html>

mercurial