|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 var httpserver = new HttpServer(); |
|
4 var index = 0; |
|
5 var tests = [ |
|
6 // Initial request. Cached variant will have no cookie |
|
7 { url : "/bug468426", server : "0", expected : "0", cookie: null}, |
|
8 |
|
9 // Cache now contains a variant with no value for cookie. If we don't |
|
10 // set cookie we expect to receive the cached variant |
|
11 { url : "/bug468426", server : "1", expected : "0", cookie: null}, |
|
12 |
|
13 // Cache still contains a variant with no value for cookie. If we |
|
14 // set a value for cookie we expect a fresh value |
|
15 { url : "/bug468426", server : "2", expected : "2", cookie: "c=2"}, |
|
16 |
|
17 // Cache now contains a variant with cookie "c=2". If the request |
|
18 // also set cookie "c=2", we expect to receive the cached variant. |
|
19 { url : "/bug468426", server : "3", expected : "2", cookie: "c=2"}, |
|
20 |
|
21 // Cache still contains a variant with cookie "c=2". When setting |
|
22 // cookie "c=4" in the request we expect a fresh value |
|
23 { url : "/bug468426", server : "4", expected : "4", cookie: "c=4"}, |
|
24 |
|
25 // Cache now contains a variant with cookie "c=4". When setting |
|
26 // cookie "c=4" in the request we expect the cached variant |
|
27 { url : "/bug468426", server : "5", expected : "4", cookie: "c=4"}, |
|
28 |
|
29 // Cache still contains a variant with cookie "c=4". When setting |
|
30 // no cookie in the request we expect a fresh value |
|
31 { url : "/bug468426", server : "6", expected : "6", cookie: null}, |
|
32 |
|
33 ]; |
|
34 |
|
35 function setupChannel(suffix, value, cookie) { |
|
36 var ios = Components.classes["@mozilla.org/network/io-service;1"] |
|
37 .getService(Ci.nsIIOService); |
|
38 var chan = ios.newChannel("http://localhost:" + |
|
39 httpserver.identity.primaryPort + suffix, "", null); |
|
40 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); |
|
41 httpChan.requestMethod = "GET"; |
|
42 httpChan.setRequestHeader("x-request", value, false); |
|
43 if (cookie != null) |
|
44 httpChan.setRequestHeader("Cookie", cookie, false); |
|
45 return httpChan; |
|
46 } |
|
47 |
|
48 function triggerNextTest() { |
|
49 var channel = setupChannel(tests[index].url, tests[index].server, tests[index].cookie); |
|
50 channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null), null); |
|
51 } |
|
52 |
|
53 function checkValueAndTrigger(request, data, ctx) { |
|
54 do_check_eq(tests[index].expected, data); |
|
55 |
|
56 if (index < tests.length - 1) { |
|
57 index++; |
|
58 // This call happens in onStopRequest from the channel. Opening a new |
|
59 // channel to the same url here is no good idea! Post it instead... |
|
60 do_timeout(1, triggerNextTest); |
|
61 } else { |
|
62 httpserver.stop(do_test_finished); |
|
63 } |
|
64 } |
|
65 |
|
66 function run_test() { |
|
67 httpserver.registerPathHandler("/bug468426", handler); |
|
68 httpserver.start(-1); |
|
69 |
|
70 // Clear cache and trigger the first test |
|
71 evict_cache_entries(); |
|
72 triggerNextTest(); |
|
73 |
|
74 do_test_pending(); |
|
75 } |
|
76 |
|
77 function handler(metadata, response) { |
|
78 var body = "unset"; |
|
79 try { |
|
80 body = metadata.getHeader("x-request"); |
|
81 } catch(e) { } |
|
82 response.setStatusLine(metadata.httpVersion, 200, "Ok"); |
|
83 response.setHeader("Content-Type", "text/plain", false); |
|
84 response.setHeader("Last-Modified", getDateString(-1), false); |
|
85 response.setHeader("Vary", "Cookie", false); |
|
86 response.bodyOutputStream.write(body, body.length); |
|
87 } |
|
88 |
|
89 function getDateString(yearDelta) { |
|
90 var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', |
|
91 'Sep', 'Oct', 'Nov', 'Dec' ]; |
|
92 var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; |
|
93 |
|
94 var d = new Date(); |
|
95 return days[d.getUTCDay()] + ", " + d.getUTCDate() + " " |
|
96 + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta) |
|
97 + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" |
|
98 + d.getUTCSeconds() + " UTC"; |
|
99 } |