content/base/test/test_bug475156.html

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/test/test_bug475156.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,286 @@
     1.4 +<!DOCTYPE HTML>
     1.5 +<html>
     1.6 +<!--
     1.7 +https://bugzilla.mozilla.org/show_bug.cgi?id=475156
     1.8 +-->
     1.9 +<head>
    1.10 +  <title>Test for Bug 475156</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 onload="drive(tests.shift());">
    1.15 +<script class="testbody" type="text/javascript">
    1.16 +
    1.17 +SimpleTest.waitForExplicitFinish();
    1.18 +
    1.19 +var path = "http://mochi.test:8888/tests/content/base/test/";
    1.20 +
    1.21 +function fromCache(xhr)
    1.22 +{
    1.23 +  var ch = SpecialPowers.wrap(xhr).channel.QueryInterface(SpecialPowers.Ci.nsICacheInfoChannel);
    1.24 +  return ch.isFromCache();  
    1.25 +}
    1.26 +
    1.27 +var tests = [
    1.28 +  // First just init the file with an ETag
    1.29 +  {
    1.30 +    init: function(xhr) 
    1.31 +    {
    1.32 +      xhr.open("GET", path + "bug475156.sjs?etag=a1");
    1.33 +    },
    1.34 +    
    1.35 +    loading: function(xhr)
    1.36 +    {
    1.37 +    },
    1.38 +
    1.39 +    done: function(xhr)
    1.40 +    {
    1.41 +    },
    1.42 +  },
    1.43 +
    1.44 +  // Try to load the file the first time regularly, we have to get 200 OK
    1.45 +  {    
    1.46 +    init: function(xhr)
    1.47 +    {
    1.48 +      xhr.open("GET", path + "bug475156.sjs");
    1.49 +    },
    1.50 +
    1.51 +    loading: function(xhr)
    1.52 +    {
    1.53 +      is(fromCache(xhr), false, "Not coming from the cache");
    1.54 +    },
    1.55 +
    1.56 +    done: function(xhr)
    1.57 +    {
    1.58 +      is(xhr.status, 200, "We get a fresh version of the file");
    1.59 +      is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag");
    1.60 +      is(xhr.responseText, "a1", "We got the expected file body");
    1.61 +    },
    1.62 +  },
    1.63 +
    1.64 +  // Try to load the file the second time regularly, we have to get 304 Not Modified
    1.65 +  {    
    1.66 +    init: function(xhr)
    1.67 +    {
    1.68 +      xhr.open("GET", path + "bug475156.sjs");
    1.69 +      xhr.setRequestHeader("If-Match", "a1");
    1.70 +    },
    1.71 +
    1.72 +    loading: function(xhr)
    1.73 +    {
    1.74 +      is(fromCache(xhr), true, "Coming from the cache");
    1.75 +    },
    1.76 +
    1.77 +    done: function(xhr)
    1.78 +    {
    1.79 +      is(xhr.status, 200, "We got cached version");
    1.80 +      is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag");
    1.81 +      is(xhr.responseText, "a1", "We got the expected file body");
    1.82 +    },
    1.83 +  },
    1.84 +
    1.85 +  // Try to load the file the third time regularly, we have to get 304 Not Modified
    1.86 +  {    
    1.87 +    init: function(xhr)
    1.88 +    {
    1.89 +      xhr.open("GET", path + "bug475156.sjs");
    1.90 +      xhr.setRequestHeader("If-Match", "a1");
    1.91 +    },
    1.92 +
    1.93 +    loading: function(xhr)
    1.94 +    {
    1.95 +      is(fromCache(xhr), true, "Coming from the cache");
    1.96 +    },
    1.97 +
    1.98 +    done: function(xhr)
    1.99 +    {
   1.100 +      is(xhr.status, 200, "We got cached version");
   1.101 +      is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag");
   1.102 +      is(xhr.responseText, "a1", "We got the expected file body");
   1.103 +    },
   1.104 +  },
   1.105 +
   1.106 +  // Now modify the ETag
   1.107 +  {
   1.108 +    init: function(xhr) 
   1.109 +    {
   1.110 +      xhr.open("GET", path + "bug475156.sjs?etag=a2");
   1.111 +    },
   1.112 +
   1.113 +    loading: function(xhr)
   1.114 +    {
   1.115 +    },
   1.116 +
   1.117 +    done: function(xhr)
   1.118 +    {
   1.119 +    },
   1.120 +  },
   1.121 +
   1.122 +  // Try to load the file, we have to get 200 OK with the new content
   1.123 +  {    
   1.124 +    init: function(xhr)
   1.125 +    {
   1.126 +      xhr.open("GET", path + "bug475156.sjs");
   1.127 +      xhr.setRequestHeader("If-Match", "a2");
   1.128 +    },
   1.129 +
   1.130 +    loading: function(xhr)
   1.131 +    {
   1.132 +      is(fromCache(xhr), false, "Not coming from the cache");
   1.133 +    },
   1.134 +
   1.135 +    done: function(xhr)
   1.136 +    {
   1.137 +      is(xhr.status, 200, "We get a fresh version of the file");
   1.138 +      is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag");
   1.139 +      is(xhr.responseText, "a2", "We got the expected file body");
   1.140 +    },
   1.141 +  },
   1.142 +
   1.143 +  // Try to load the file the second time regularly, we have to get 304 Not Modified
   1.144 +  {    
   1.145 +    init: function(xhr)
   1.146 +    {
   1.147 +      xhr.open("GET", path + "bug475156.sjs");
   1.148 +      xhr.setRequestHeader("If-Match", "a2");
   1.149 +    },
   1.150 +
   1.151 +    loading: function(xhr)
   1.152 +    {
   1.153 +      is(fromCache(xhr), true, "Coming from the cache");
   1.154 +    },
   1.155 +
   1.156 +    done: function(xhr)
   1.157 +    {
   1.158 +      is(xhr.status, 200, "We got cached version");
   1.159 +      is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag");
   1.160 +      is(xhr.responseText, "a2", "We got the expected file body");
   1.161 +    },
   1.162 +  },
   1.163 +
   1.164 +  // Try to load the file the third time regularly, we have to get 304 Not Modified
   1.165 +  {    
   1.166 +    init: function(xhr)
   1.167 +    {
   1.168 +      xhr.open("GET", path + "bug475156.sjs");
   1.169 +      xhr.setRequestHeader("If-Match", "a2");
   1.170 +    },
   1.171 +
   1.172 +    loading: function(xhr)
   1.173 +    {
   1.174 +      is(fromCache(xhr), true, "Coming from the cache");
   1.175 +    },
   1.176 +
   1.177 +    done: function(xhr)
   1.178 +    {
   1.179 +      is(xhr.status, 200, "We got cached version");
   1.180 +      is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag");
   1.181 +      is(xhr.responseText, "a2", "We got the expected file body");
   1.182 +    },
   1.183 +  },
   1.184 +
   1.185 +  // Now modify the ETag ones more
   1.186 +  {
   1.187 +    init: function(xhr) 
   1.188 +    {
   1.189 +      xhr.open("GET", path + "bug475156.sjs?etag=a3");
   1.190 +    },
   1.191 +
   1.192 +    loading: function(xhr)
   1.193 +    {
   1.194 +    },
   1.195 +
   1.196 +    done: function(xhr)
   1.197 +    {
   1.198 +    },
   1.199 +  },
   1.200 +
   1.201 +  // Try to load the file, we have to get 200 OK with the new content
   1.202 +  {    
   1.203 +    init: function(xhr)
   1.204 +    {
   1.205 +      xhr.open("GET", path + "bug475156.sjs");
   1.206 +      xhr.setRequestHeader("If-Match", "a3");
   1.207 +    },
   1.208 +
   1.209 +    loading: function(xhr)
   1.210 +    {
   1.211 +      is(fromCache(xhr), false, "Not coming from the cache");
   1.212 +    },
   1.213 +
   1.214 +    done: function(xhr)
   1.215 +    {
   1.216 +      is(xhr.status, 200, "We get a fresh version of the file");
   1.217 +      is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag");
   1.218 +      is(xhr.responseText, "a3", "We got the expected file body");
   1.219 +    },
   1.220 +  },
   1.221 +
   1.222 +  // Try to load the file the second time regularly, we have to get 304 Not Modified
   1.223 +  {    
   1.224 +    init: function(xhr)
   1.225 +    {
   1.226 +      xhr.open("GET", path + "bug475156.sjs");
   1.227 +      xhr.setRequestHeader("If-Match", "a3");
   1.228 +    },
   1.229 +
   1.230 +    loading: function(xhr)
   1.231 +    {
   1.232 +      is(fromCache(xhr), true, "Coming from the cache");
   1.233 +    },
   1.234 +
   1.235 +    done: function(xhr)
   1.236 +    {
   1.237 +      is(xhr.status, 200, "We got cached version");
   1.238 +      is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag");
   1.239 +      is(xhr.responseText, "a3", "We got the expected file body");
   1.240 +    },
   1.241 +  },
   1.242 +
   1.243 +  // Try to load the file the third time regularly, we have to get 304 Not Modified
   1.244 +  {    
   1.245 +    init: function(xhr)
   1.246 +    {
   1.247 +      xhr.open("GET", path + "bug475156.sjs");
   1.248 +      xhr.setRequestHeader("If-Match", "a3");
   1.249 +    },
   1.250 +
   1.251 +    loading: function(xhr)
   1.252 +    {
   1.253 +      is(fromCache(xhr), true, "Coming from the cache");
   1.254 +    },
   1.255 +
   1.256 +    done: function(xhr)
   1.257 +    {
   1.258 +      is(xhr.status, 200, "We got cached version");
   1.259 +      is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag");
   1.260 +      is(xhr.responseText, "a3", "We got the expected file body");
   1.261 +    },
   1.262 +  },
   1.263 +]
   1.264 +
   1.265 +
   1.266 +function drive(test)
   1.267 +{  
   1.268 +  var xhr = new XMLHttpRequest();
   1.269 +  test.init(xhr);
   1.270 +  xhr.onreadystatechange = function() {
   1.271 +    if (this.readyState == 3) {
   1.272 +      test.loading(this);
   1.273 +    }
   1.274 +    if (this.readyState == 4) {
   1.275 +      test.done(this);
   1.276 +      if (tests.length == 0)
   1.277 +        SimpleTest.finish();
   1.278 +      else
   1.279 +        drive(tests.shift());
   1.280 +    }
   1.281 +  }
   1.282 +  xhr.send();
   1.283 +}
   1.284 +
   1.285 +</script>
   1.286 +</pre>
   1.287 +</body>
   1.288 +</html>
   1.289 +

mercurial