test_bug475156.html (6626B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=475156 5 --> 6 <head> 7 <title>Test for Bug 475156</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body onload="drive(tests.shift());"> 12 <script class="testbody" type="text/javascript"> 13 14 SimpleTest.waitForExplicitFinish(); 15 16 var path = "http://mochi.test:8888/tests/dom/base/test/"; 17 18 function fromCache(xhr) 19 { 20 var ch = SpecialPowers.wrap(xhr).channel.QueryInterface(SpecialPowers.Ci.nsICacheInfoChannel); 21 return ch.isFromCache(); 22 } 23 24 var tests = [ 25 // First just init the file with an ETag 26 { 27 init(xhr) 28 { 29 xhr.open("GET", path + "bug475156.sjs?etag=a1"); 30 }, 31 32 loading(xhr) 33 { 34 }, 35 36 done(xhr) 37 { 38 }, 39 }, 40 41 // Try to load the file the first time regularly, we have to get 200 OK 42 { 43 init(xhr) 44 { 45 xhr.open("GET", path + "bug475156.sjs"); 46 }, 47 48 loading(xhr) 49 { 50 is(fromCache(xhr), false, "Not coming from the cache"); 51 }, 52 53 done(xhr) 54 { 55 is(xhr.status, 200, "We get a fresh version of the file"); 56 is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag"); 57 is(xhr.responseText, "a1", "We got the expected file body"); 58 }, 59 }, 60 61 // Try to load the file the second time regularly, we have to get 304 Not Modified 62 { 63 init(xhr) 64 { 65 xhr.open("GET", path + "bug475156.sjs"); 66 xhr.setRequestHeader("If-Match", "a1"); 67 }, 68 69 loading(xhr) 70 { 71 is(fromCache(xhr), true, "Coming from the cache"); 72 }, 73 74 done(xhr) 75 { 76 is(xhr.status, 200, "We got cached version"); 77 is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag"); 78 is(xhr.responseText, "a1", "We got the expected file body"); 79 }, 80 }, 81 82 // Try to load the file the third time regularly, we have to get 304 Not Modified 83 { 84 init(xhr) 85 { 86 xhr.open("GET", path + "bug475156.sjs"); 87 xhr.setRequestHeader("If-Match", "a1"); 88 }, 89 90 loading(xhr) 91 { 92 is(fromCache(xhr), true, "Coming from the cache"); 93 }, 94 95 done(xhr) 96 { 97 is(xhr.status, 200, "We got cached version"); 98 is(xhr.getResponseHeader("Etag"), "a1", "We got correct ETag"); 99 is(xhr.responseText, "a1", "We got the expected file body"); 100 }, 101 }, 102 103 // Now modify the ETag 104 { 105 init(xhr) 106 { 107 xhr.open("GET", path + "bug475156.sjs?etag=a2"); 108 }, 109 110 loading(xhr) 111 { 112 }, 113 114 done(xhr) 115 { 116 }, 117 }, 118 119 // Try to load the file, we have to get 200 OK with the new content 120 { 121 init(xhr) 122 { 123 xhr.open("GET", path + "bug475156.sjs"); 124 xhr.setRequestHeader("If-Match", "a2"); 125 }, 126 127 loading(xhr) 128 { 129 is(fromCache(xhr), false, "Not coming from the cache"); 130 }, 131 132 done(xhr) 133 { 134 is(xhr.status, 200, "We get a fresh version of the file"); 135 is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag"); 136 is(xhr.responseText, "a2", "We got the expected file body"); 137 }, 138 }, 139 140 // Try to load the file the second time regularly, we have to get 304 Not Modified 141 { 142 init(xhr) 143 { 144 xhr.open("GET", path + "bug475156.sjs"); 145 xhr.setRequestHeader("If-Match", "a2"); 146 }, 147 148 loading(xhr) 149 { 150 is(fromCache(xhr), true, "Coming from the cache"); 151 }, 152 153 done(xhr) 154 { 155 is(xhr.status, 200, "We got cached version"); 156 is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag"); 157 is(xhr.responseText, "a2", "We got the expected file body"); 158 }, 159 }, 160 161 // Try to load the file the third time regularly, we have to get 304 Not Modified 162 { 163 init(xhr) 164 { 165 xhr.open("GET", path + "bug475156.sjs"); 166 xhr.setRequestHeader("If-Match", "a2"); 167 }, 168 169 loading(xhr) 170 { 171 is(fromCache(xhr), true, "Coming from the cache"); 172 }, 173 174 done(xhr) 175 { 176 is(xhr.status, 200, "We got cached version"); 177 is(xhr.getResponseHeader("Etag"), "a2", "We got correct ETag"); 178 is(xhr.responseText, "a2", "We got the expected file body"); 179 }, 180 }, 181 182 // Now modify the ETag ones more 183 { 184 init(xhr) 185 { 186 xhr.open("GET", path + "bug475156.sjs?etag=a3"); 187 }, 188 189 loading(xhr) 190 { 191 }, 192 193 done(xhr) 194 { 195 }, 196 }, 197 198 // Try to load the file, we have to get 200 OK with the new content 199 { 200 init(xhr) 201 { 202 xhr.open("GET", path + "bug475156.sjs"); 203 xhr.setRequestHeader("If-Match", "a3"); 204 }, 205 206 loading(xhr) 207 { 208 is(fromCache(xhr), false, "Not coming from the cache"); 209 }, 210 211 done(xhr) 212 { 213 is(xhr.status, 200, "We get a fresh version of the file"); 214 is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag"); 215 is(xhr.responseText, "a3", "We got the expected file body"); 216 }, 217 }, 218 219 // Try to load the file the second time regularly, we have to get 304 Not Modified 220 { 221 init(xhr) 222 { 223 xhr.open("GET", path + "bug475156.sjs"); 224 xhr.setRequestHeader("If-Match", "a3"); 225 }, 226 227 loading(xhr) 228 { 229 is(fromCache(xhr), true, "Coming from the cache"); 230 }, 231 232 done(xhr) 233 { 234 is(xhr.status, 200, "We got cached version"); 235 is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag"); 236 is(xhr.responseText, "a3", "We got the expected file body"); 237 }, 238 }, 239 240 // Try to load the file the third time regularly, we have to get 304 Not Modified 241 { 242 init(xhr) 243 { 244 xhr.open("GET", path + "bug475156.sjs"); 245 xhr.setRequestHeader("If-Match", "a3"); 246 }, 247 248 loading(xhr) 249 { 250 is(fromCache(xhr), true, "Coming from the cache"); 251 }, 252 253 done(xhr) 254 { 255 is(xhr.status, 200, "We got cached version"); 256 is(xhr.getResponseHeader("Etag"), "a3", "We got correct ETag"); 257 is(xhr.responseText, "a3", "We got the expected file body"); 258 }, 259 }, 260 261 // Load one last time to reset the state variable in the .sjs file 262 { 263 init (xhr) { 264 xhr.open("GET", path + "bug475156.sjs"); 265 xhr.setRequestHeader("If-Match", "a1"); 266 }, 267 268 loading (xhr) { 269 }, 270 271 done (xhr) { 272 }, 273 }, 274 ] 275 276 277 function drive(test) 278 { 279 SpecialPowers.pushPrefEnv({set: [["network.http.rcwn.enabled", false]]}, _=>{ 280 var xhr = new XMLHttpRequest(); 281 test.init(xhr); 282 xhr.onreadystatechange = function() { 283 if (this.readyState == 3) { 284 test.loading(this); 285 } 286 if (this.readyState == 4) { 287 test.done(this); 288 if (!tests.length) 289 SimpleTest.finish(); 290 else 291 drive(tests.shift()); 292 } 293 } 294 xhr.send(); 295 }); 296 } 297 298 </script> 299 </body> 300 </html>