netwerk/test/mochitests/test_user_agent_overrides.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

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

mercurial