services/sync/tests/unit/test_resource_header.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_resource_header.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,65 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +"use strict";
     1.8 +
     1.9 +Cu.import("resource://testing-common/httpd.js");
    1.10 +Cu.import("resource://services-sync/resource.js");
    1.11 +
    1.12 +function run_test() {
    1.13 +  initTestLogging("Trace");
    1.14 +  run_next_test();
    1.15 +}
    1.16 +
    1.17 +let httpServer = new HttpServer();
    1.18 +httpServer.registerPathHandler("/content", contentHandler);
    1.19 +httpServer.start(-1);
    1.20 +
    1.21 +const HTTP_PORT = httpServer.identity.primaryPort;
    1.22 +const TEST_URL = "http://localhost:" + HTTP_PORT + "/content";
    1.23 +const BODY = "response body";
    1.24 +
    1.25 +// Keep headers for later inspection.
    1.26 +let auth = null;
    1.27 +let foo  = null;
    1.28 +function contentHandler(metadata, response) {
    1.29 +  _("Handling request.");
    1.30 +  auth = metadata.getHeader("Authorization");
    1.31 +  foo  = metadata.getHeader("X-Foo");
    1.32 +
    1.33 +  _("Extracted headers. " + auth + ", " + foo);
    1.34 +
    1.35 +  response.setHeader("Content-Type", "text/plain");
    1.36 +  response.bodyOutputStream.write(BODY, BODY.length);
    1.37 +}
    1.38 +
    1.39 +// Set a proxy function to cause an internal redirect.
    1.40 +function triggerRedirect() {
    1.41 +  const PROXY_FUNCTION = "function FindProxyForURL(url, host) {"                +
    1.42 +                         "  return 'PROXY a_non_existent_domain_x7x6c572v:80; " +
    1.43 +                                   "PROXY localhost:" + HTTP_PORT + "';"        +
    1.44 +                         "}";
    1.45 +
    1.46 +  let prefsService = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
    1.47 +  let prefs = prefsService.getBranch("network.proxy.");
    1.48 +  prefs.setIntPref("type", 2);
    1.49 +  prefs.setCharPref("autoconfig_url", "data:text/plain," + PROXY_FUNCTION);
    1.50 +}
    1.51 +
    1.52 +add_test(function test_headers_copied() {
    1.53 +  triggerRedirect();
    1.54 +
    1.55 +  _("Issuing request.");
    1.56 +  let resource = new Resource(TEST_URL);
    1.57 +  resource.setHeader("Authorization", "Basic foobar");
    1.58 +  resource.setHeader("X-Foo", "foofoo");
    1.59 +
    1.60 +  let result = resource.get(TEST_URL);
    1.61 +  _("Result: " + result);
    1.62 +
    1.63 +  do_check_eq(result, BODY);
    1.64 +  do_check_eq(auth, "Basic foobar");
    1.65 +  do_check_eq(foo, "foofoo");
    1.66 +
    1.67 +  httpServer.stop(run_next_test);
    1.68 +});

mercurial