|
1 "use strict"; |
|
2 // https://bugzilla.mozilla.org/show_bug.cgi?id=761228 |
|
3 |
|
4 Cu.import("resource://testing-common/httpd.js"); |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "URL", function() { |
|
7 return "http://localhost:" + httpServer.identity.primaryPort; |
|
8 }); |
|
9 |
|
10 var httpServer = null; |
|
11 const testFileName = "test_customConditionalRequest_304"; |
|
12 const basePath = "/" + testFileName + "/"; |
|
13 |
|
14 XPCOMUtils.defineLazyGetter(this, "baseURI", function() { |
|
15 return URL + basePath; |
|
16 }); |
|
17 |
|
18 const unexpected304 = "unexpected304"; |
|
19 const existingCached304 = "existingCached304"; |
|
20 |
|
21 function make_uri(url) { |
|
22 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
23 getService(Ci.nsIIOService); |
|
24 return ios.newURI(url, null, null); |
|
25 } |
|
26 |
|
27 function make_channel(url) { |
|
28 var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService); |
|
29 var chan = ios.newChannel(url, null, null).QueryInterface(Ci.nsIHttpChannel); |
|
30 return chan; |
|
31 } |
|
32 |
|
33 function clearCache() { |
|
34 var service = Components.classes["@mozilla.org/netwerk/cache-storage-service;1"] |
|
35 .getService(Ci.nsICacheStorageService); |
|
36 service.clear(); |
|
37 } |
|
38 |
|
39 function alwaysReturn304Handler(metadata, response) { |
|
40 response.setStatusLine(metadata.httpVersion, 304, "Not Modified"); |
|
41 response.setHeader("Returned-From-Handler", "1"); |
|
42 } |
|
43 |
|
44 function run_test() { |
|
45 evict_cache_entries(); |
|
46 |
|
47 httpServer = new HttpServer(); |
|
48 httpServer.registerPathHandler(basePath + unexpected304, |
|
49 alwaysReturn304Handler); |
|
50 httpServer.registerPathHandler(basePath + existingCached304, |
|
51 alwaysReturn304Handler); |
|
52 httpServer.start(-1); |
|
53 run_next_test(); |
|
54 } |
|
55 |
|
56 function finish_test(request, buffer) { |
|
57 httpServer.stop(do_test_finished); |
|
58 } |
|
59 |
|
60 function consume304(request, buffer) { |
|
61 request.QueryInterface(Components.interfaces.nsIHttpChannel); |
|
62 do_check_eq(request.responseStatus, 304); |
|
63 do_check_eq(request.getResponseHeader("Returned-From-Handler"), "1"); |
|
64 run_next_test(); |
|
65 } |
|
66 |
|
67 // Test that we return a 304 response to the caller when we are not expecting |
|
68 // a 304 response (i.e. when the server shouldn't have sent us one). |
|
69 add_test(function test_unexpected_304() { |
|
70 var chan = make_channel(baseURI + unexpected304); |
|
71 chan.asyncOpen(new ChannelListener(consume304, null), null); |
|
72 }); |
|
73 |
|
74 // Test that we can cope with a 304 response that was (erroneously) stored in |
|
75 // the cache. |
|
76 add_test(function test_304_stored_in_cache() { |
|
77 asyncOpenCacheEntry( |
|
78 baseURI + existingCached304, "disk", Ci.nsICacheStorage.OPEN_NORMALLY, null, |
|
79 function (entryStatus, cacheEntry) { |
|
80 cacheEntry.setMetaDataElement("request-method", "GET"); |
|
81 cacheEntry.setMetaDataElement("response-head", |
|
82 "HTTP/1.1 304 Not Modified\r\n" + |
|
83 "\r\n"); |
|
84 cacheEntry.metaDataReady(); |
|
85 cacheEntry.close(); |
|
86 |
|
87 var chan = make_channel(baseURI + existingCached304); |
|
88 |
|
89 // make it a custom conditional request |
|
90 chan.QueryInterface(Components.interfaces.nsIHttpChannel); |
|
91 chan.setRequestHeader("If-None-Match", '"foo"', false); |
|
92 |
|
93 chan.asyncOpen(new ChannelListener(consume304, null), null); |
|
94 }); |
|
95 }); |