|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 /* |
|
4 - get 302 with Cache-control: no-store |
|
5 - check cache entry for the 302 response is cached only in memory device |
|
6 - get 302 with Expires: -1 |
|
7 - check cache entry for the 302 response is not cached at all |
|
8 */ |
|
9 |
|
10 var httpserver = null; |
|
11 // Need to randomize, because apparently no one clears our cache |
|
12 var randomPath1 = "/redirect-no-store/" + Math.random(); |
|
13 |
|
14 XPCOMUtils.defineLazyGetter(this, "randomURI1", function() { |
|
15 return "http://localhost:" + httpserver.identity.primaryPort + randomPath1; |
|
16 }); |
|
17 |
|
18 var randomPath2 = "/redirect-expires-past/" + Math.random(); |
|
19 |
|
20 XPCOMUtils.defineLazyGetter(this, "randomURI2", function() { |
|
21 return "http://localhost:" + httpserver.identity.primaryPort + randomPath2; |
|
22 }); |
|
23 |
|
24 function make_channel(url, callback, ctx) { |
|
25 var ios = Cc["@mozilla.org/network/io-service;1"]. |
|
26 getService(Ci.nsIIOService); |
|
27 return ios.newChannel(url, "", null); |
|
28 } |
|
29 |
|
30 const responseBody = "response body"; |
|
31 |
|
32 var redirectHandler_NoStore_calls = 0; |
|
33 function redirectHandler_NoStore(metadata, response) |
|
34 { |
|
35 response.setStatusLine(metadata.httpVersion, 302, "Found"); |
|
36 response.setHeader("Location", "http://localhost:" + |
|
37 httpserver.identity.primaryPort + "/content", false); |
|
38 response.setHeader("Cache-control", "no-store"); |
|
39 ++redirectHandler_NoStore_calls; |
|
40 return; |
|
41 } |
|
42 |
|
43 var redirectHandler_ExpiresInPast_calls = 0; |
|
44 function redirectHandler_ExpiresInPast(metadata, response) |
|
45 { |
|
46 response.setStatusLine(metadata.httpVersion, 302, "Found"); |
|
47 response.setHeader("Location", "http://localhost:" + |
|
48 httpserver.identity.primaryPort + "/content", false); |
|
49 response.setHeader("Expires", "-1"); |
|
50 ++redirectHandler_ExpiresInPast_calls; |
|
51 return; |
|
52 } |
|
53 |
|
54 function contentHandler(metadata, response) |
|
55 { |
|
56 response.setHeader("Content-Type", "text/plain"); |
|
57 response.bodyOutputStream.write(responseBody, responseBody.length); |
|
58 } |
|
59 |
|
60 function check_response(path, request, buffer, expectedExpiration, continuation) |
|
61 { |
|
62 do_check_eq(buffer, responseBody); |
|
63 |
|
64 // Entry is always there, old cache wrapping code does session->SetDoomEntriesIfExpired(false), |
|
65 // just check it's not persisted or is expired (dep on the test). |
|
66 asyncOpenCacheEntry(path, "disk", Ci.nsICacheStorage.OPEN_READONLY, null, function(status, entry) { |
|
67 do_check_eq(status, 0); |
|
68 |
|
69 // Expired entry is on disk, no-store entry is in memory |
|
70 do_check_eq(entry.persistent, expectedExpiration); |
|
71 |
|
72 // Do the request again and check the server handler is called appropriately |
|
73 var chan = make_channel(path); |
|
74 chan.asyncOpen(new ChannelListener(function(request, buffer) { |
|
75 do_check_eq(buffer, responseBody); |
|
76 |
|
77 if (expectedExpiration) { |
|
78 // Handler had to be called second time |
|
79 do_check_eq(redirectHandler_ExpiresInPast_calls, 2); |
|
80 } |
|
81 else { |
|
82 // Handler had to be called second time (no-store forces validate), |
|
83 // and we are just in memory |
|
84 do_check_eq(redirectHandler_NoStore_calls, 2); |
|
85 do_check_true(!entry.persistent); |
|
86 } |
|
87 |
|
88 continuation(); |
|
89 }, null), null); |
|
90 }); |
|
91 } |
|
92 |
|
93 function run_test_no_store() |
|
94 { |
|
95 var chan = make_channel(randomURI1); |
|
96 chan.asyncOpen(new ChannelListener(function(request, buffer) { |
|
97 // Cache-control: no-store response should only be found in the memory cache. |
|
98 check_response(randomURI1, request, buffer, false, run_test_expires_past); |
|
99 }, null), null); |
|
100 } |
|
101 |
|
102 function run_test_expires_past() |
|
103 { |
|
104 var chan = make_channel(randomURI2); |
|
105 chan.asyncOpen(new ChannelListener(function(request, buffer) { |
|
106 // Expires: -1 response should not be found in any cache. |
|
107 check_response(randomURI2, request, buffer, true, finish_test); |
|
108 }, null), null); |
|
109 } |
|
110 |
|
111 function finish_test() |
|
112 { |
|
113 httpserver.stop(do_test_finished); |
|
114 } |
|
115 |
|
116 function run_test() |
|
117 { |
|
118 do_get_profile(); |
|
119 |
|
120 httpserver = new HttpServer(); |
|
121 httpserver.registerPathHandler(randomPath1, redirectHandler_NoStore); |
|
122 httpserver.registerPathHandler(randomPath2, redirectHandler_ExpiresInPast); |
|
123 httpserver.registerPathHandler("/content", contentHandler); |
|
124 httpserver.start(-1); |
|
125 |
|
126 run_test_no_store(); |
|
127 do_test_pending(); |
|
128 } |