netwerk/test/unit/test_bug770243.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug770243.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,208 @@
     1.4 +/* this test does the following:
     1.5 + Always requests the same resource, while for each request getting:
     1.6 + 1. 200 + ETag: "one"
     1.7 + 2. 401 followed by 200 + ETag: "two"
     1.8 + 3. 401 followed by 304
     1.9 + 4. 407 followed by 200 + ETag: "three"
    1.10 + 5. 407 followed by 304
    1.11 +*/
    1.12 +
    1.13 +Cu.import("resource://testing-common/httpd.js");
    1.14 +
    1.15 +var httpserv;
    1.16 +
    1.17 +function addCreds(scheme, host)
    1.18 +{
    1.19 +  var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
    1.20 +                          .getService(Ci.nsIHttpAuthManager);
    1.21 +  authMgr.setAuthIdentity(scheme, host, httpserv.identity.primaryPort,
    1.22 +                          "basic", "secret", "/", "", "user", "pass");
    1.23 +}
    1.24 +
    1.25 +function clearCreds()
    1.26 +{
    1.27 +  var authMgr = Components.classes['@mozilla.org/network/http-auth-manager;1']
    1.28 +                          .getService(Ci.nsIHttpAuthManager);
    1.29 +  authMgr.clearAll();
    1.30 +}
    1.31 +
    1.32 +function makeChan() {
    1.33 +  var ios = Cc["@mozilla.org/network/io-service;1"]
    1.34 +                      .getService(Ci.nsIIOService);
    1.35 +  var chan = ios.newChannel("http://localhost:" +
    1.36 +                            httpserv.identity.primaryPort + "/", null, null)
    1.37 +                .QueryInterface(Ci.nsIHttpChannel);
    1.38 +  return chan;
    1.39 +}
    1.40 +
    1.41 +// Array of handlers that are called one by one in response to expected requests
    1.42 +
    1.43 +var handlers = [
    1.44 +  // Test 1
    1.45 +  function(metadata, response) {
    1.46 +    do_check_eq(metadata.hasHeader("Authorization"), false);
    1.47 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
    1.48 +    response.setHeader("ETag", '"one"', false);
    1.49 +    response.setHeader("Cache-control", 'no-cache', false);
    1.50 +    response.setHeader("Content-type", 'text/plain', false);
    1.51 +    var body = "Response body 1";
    1.52 +    response.bodyOutputStream.write(body, body.length);
    1.53 +  },
    1.54 +
    1.55 +  // Test 2
    1.56 +  function(metadata, response) {
    1.57 +    do_check_eq(metadata.hasHeader("Authorization"), false);
    1.58 +    do_check_eq(metadata.getHeader("If-None-Match"), '"one"');
    1.59 +    response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
    1.60 +    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
    1.61 +    addCreds("http", "localhost");
    1.62 +  },
    1.63 +  function(metadata, response) {
    1.64 +    do_check_eq(metadata.hasHeader("Authorization"), true);
    1.65 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
    1.66 +    response.setHeader("ETag", '"two"', false);
    1.67 +    response.setHeader("Cache-control", 'no-cache', false);
    1.68 +    response.setHeader("Content-type", 'text/plain', false);
    1.69 +    var body = "Response body 2";
    1.70 +    response.bodyOutputStream.write(body, body.length);
    1.71 +    clearCreds();
    1.72 +  },
    1.73 +
    1.74 +  // Test 3
    1.75 +  function(metadata, response) {
    1.76 +    do_check_eq(metadata.hasHeader("Authorization"), false);
    1.77 +    do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
    1.78 +    response.setStatusLine(metadata.httpVersion, 401, "Authenticate");
    1.79 +    response.setHeader("WWW-Authenticate", 'Basic realm="secret"', false);
    1.80 +    addCreds("http", "localhost");
    1.81 +  },
    1.82 +  function(metadata, response) {
    1.83 +    do_check_eq(metadata.hasHeader("Authorization"), true);
    1.84 +    do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
    1.85 +    response.setStatusLine(metadata.httpVersion, 304, "OK");
    1.86 +    response.setHeader("ETag", '"two"', false);
    1.87 +    clearCreds();
    1.88 +  },
    1.89 +
    1.90 +  // Test 4
    1.91 +  function(metadata, response) {
    1.92 +    do_check_eq(metadata.hasHeader("Authorization"), false);
    1.93 +    do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
    1.94 +    response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
    1.95 +    response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
    1.96 +    addCreds("http", "localhost");
    1.97 +  },
    1.98 +  function(metadata, response) {
    1.99 +    do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
   1.100 +    do_check_eq(metadata.getHeader("If-None-Match"), '"two"');
   1.101 +    response.setStatusLine(metadata.httpVersion, 200, "OK");
   1.102 +    response.setHeader("ETag", '"three"', false);
   1.103 +    response.setHeader("Cache-control", 'no-cache', false);
   1.104 +    response.setHeader("Content-type", 'text/plain', false);
   1.105 +    var body = "Response body 3";
   1.106 +    response.bodyOutputStream.write(body, body.length);
   1.107 +    clearCreds();
   1.108 +  },
   1.109 +
   1.110 +  // Test 5
   1.111 +  function(metadata, response) {
   1.112 +    do_check_eq(metadata.hasHeader("Proxy-Authorization"), false);
   1.113 +    do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
   1.114 +    response.setStatusLine(metadata.httpVersion, 407, "Proxy Authenticate");
   1.115 +    response.setHeader("Proxy-Authenticate", 'Basic realm="secret"', false);
   1.116 +    addCreds("http", "localhost");
   1.117 +  },
   1.118 +  function(metadata, response) {
   1.119 +    do_check_eq(metadata.hasHeader("Proxy-Authorization"), true);
   1.120 +    do_check_eq(metadata.getHeader("If-None-Match"), '"three"');
   1.121 +    response.setStatusLine(metadata.httpVersion, 304, "OK");
   1.122 +    response.setHeader("ETag", '"three"', false);
   1.123 +    response.setHeader("Cache-control", 'no-cache', false);
   1.124 +    clearCreds();
   1.125 +  }
   1.126 +];
   1.127 +
   1.128 +function handler(metadata, response)
   1.129 +{
   1.130 +  handlers.shift()(metadata, response);
   1.131 +}
   1.132 +
   1.133 +// Array of tests to run, self-driven
   1.134 +
   1.135 +function sync_and_run_next_test()
   1.136 +{
   1.137 +  syncWithCacheIOThread(function() {
   1.138 +    tests.shift()();
   1.139 +  });
   1.140 +}
   1.141 +
   1.142 +var tests = [
   1.143 +  // Test 1: 200 (cacheable)
   1.144 +  function() {
   1.145 +    var ch = makeChan();
   1.146 +    ch.asyncOpen(new ChannelListener(function(req, body) {
   1.147 +      do_check_eq(body, "Response body 1");
   1.148 +      sync_and_run_next_test();
   1.149 +    }, null, CL_NOT_FROM_CACHE), null);
   1.150 +  },
   1.151 +
   1.152 +  // Test 2: 401 and 200 + new content
   1.153 +  function() {
   1.154 +    var ch = makeChan();
   1.155 +    ch.asyncOpen(new ChannelListener(function(req, body) {
   1.156 +      do_check_eq(body, "Response body 2");
   1.157 +      sync_and_run_next_test();
   1.158 +    }, null, CL_NOT_FROM_CACHE), null);
   1.159 +  },
   1.160 +
   1.161 +  // Test 3: 401 and 304
   1.162 +  function() {
   1.163 +    var ch = makeChan();
   1.164 +    ch.asyncOpen(new ChannelListener(function(req, body) {
   1.165 +      do_check_eq(body, "Response body 2");
   1.166 +      sync_and_run_next_test();
   1.167 +    }, null, CL_FROM_CACHE), null);
   1.168 +  },
   1.169 +
   1.170 +  // Test 4: 407 and 200 + new content
   1.171 +  function() {
   1.172 +    var ch = makeChan();
   1.173 +    ch.asyncOpen(new ChannelListener(function(req, body) {
   1.174 +      do_check_eq(body, "Response body 3");
   1.175 +      sync_and_run_next_test();
   1.176 +    }, null, CL_NOT_FROM_CACHE), null);
   1.177 +  },
   1.178 +
   1.179 +  // Test 5: 407 and 304
   1.180 +  function() {
   1.181 +    var ch = makeChan();
   1.182 +    ch.asyncOpen(new ChannelListener(function(req, body) {
   1.183 +      do_check_eq(body, "Response body 3");
   1.184 +      sync_and_run_next_test();
   1.185 +    }, null, CL_FROM_CACHE), null);
   1.186 +  },
   1.187 +
   1.188 +  // End of test run
   1.189 +  function() {
   1.190 +    httpserv.stop(do_test_finished);
   1.191 +  }
   1.192 +];
   1.193 +
   1.194 +function run_test()
   1.195 +{
   1.196 +  do_get_profile();
   1.197 +
   1.198 +  httpserv = new HttpServer();
   1.199 +  httpserv.registerPathHandler("/", handler);
   1.200 +  httpserv.start(-1);
   1.201 +
   1.202 +  const prefs = Cc["@mozilla.org/preferences-service;1"]
   1.203 +                         .getService(Ci.nsIPrefBranch);
   1.204 +  prefs.setCharPref("network.proxy.http", "localhost");
   1.205 +  prefs.setIntPref("network.proxy.http_port", httpserv.identity.primaryPort);
   1.206 +  prefs.setCharPref("network.proxy.no_proxies_on", "");
   1.207 +  prefs.setIntPref("network.proxy.type", 1);
   1.208 +
   1.209 +  tests.shift()();
   1.210 +  do_test_pending();
   1.211 +}

mercurial