michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: michael@0: var httpserver = new HttpServer(); michael@0: var index = 0; michael@0: var tests = [ michael@0: // Initial request. Cached variant will have no cookie michael@0: { url : "/bug468426", server : "0", expected : "0", cookie: null}, michael@0: michael@0: // Cache now contains a variant with no value for cookie. If we don't michael@0: // set cookie we expect to receive the cached variant michael@0: { url : "/bug468426", server : "1", expected : "0", cookie: null}, michael@0: michael@0: // Cache still contains a variant with no value for cookie. If we michael@0: // set a value for cookie we expect a fresh value michael@0: { url : "/bug468426", server : "2", expected : "2", cookie: "c=2"}, michael@0: michael@0: // Cache now contains a variant with cookie "c=2". If the request michael@0: // also set cookie "c=2", we expect to receive the cached variant. michael@0: { url : "/bug468426", server : "3", expected : "2", cookie: "c=2"}, michael@0: michael@0: // Cache still contains a variant with cookie "c=2". When setting michael@0: // cookie "c=4" in the request we expect a fresh value michael@0: { url : "/bug468426", server : "4", expected : "4", cookie: "c=4"}, michael@0: michael@0: // Cache now contains a variant with cookie "c=4". When setting michael@0: // cookie "c=4" in the request we expect the cached variant michael@0: { url : "/bug468426", server : "5", expected : "4", cookie: "c=4"}, michael@0: michael@0: // Cache still contains a variant with cookie "c=4". When setting michael@0: // no cookie in the request we expect a fresh value michael@0: { url : "/bug468426", server : "6", expected : "6", cookie: null}, michael@0: michael@0: ]; michael@0: michael@0: function setupChannel(suffix, value, cookie) { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Ci.nsIIOService); michael@0: var chan = ios.newChannel("http://localhost:" + michael@0: httpserver.identity.primaryPort + suffix, "", null); michael@0: var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: httpChan.requestMethod = "GET"; michael@0: httpChan.setRequestHeader("x-request", value, false); michael@0: if (cookie != null) michael@0: httpChan.setRequestHeader("Cookie", cookie, false); michael@0: return httpChan; michael@0: } michael@0: michael@0: function triggerNextTest() { michael@0: var channel = setupChannel(tests[index].url, tests[index].server, tests[index].cookie); michael@0: channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null), null); michael@0: } michael@0: michael@0: function checkValueAndTrigger(request, data, ctx) { michael@0: do_check_eq(tests[index].expected, data); michael@0: michael@0: if (index < tests.length - 1) { michael@0: index++; michael@0: // This call happens in onStopRequest from the channel. Opening a new michael@0: // channel to the same url here is no good idea! Post it instead... michael@0: do_timeout(1, triggerNextTest); michael@0: } else { michael@0: httpserver.stop(do_test_finished); michael@0: } michael@0: } michael@0: michael@0: function run_test() { michael@0: httpserver.registerPathHandler("/bug468426", handler); michael@0: httpserver.start(-1); michael@0: michael@0: // Clear cache and trigger the first test michael@0: evict_cache_entries(); michael@0: triggerNextTest(); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function handler(metadata, response) { michael@0: var body = "unset"; michael@0: try { michael@0: body = metadata.getHeader("x-request"); michael@0: } catch(e) { } michael@0: response.setStatusLine(metadata.httpVersion, 200, "Ok"); michael@0: response.setHeader("Content-Type", "text/plain", false); michael@0: response.setHeader("Last-Modified", getDateString(-1), false); michael@0: response.setHeader("Vary", "Cookie", false); michael@0: response.bodyOutputStream.write(body, body.length); michael@0: } michael@0: michael@0: function getDateString(yearDelta) { michael@0: var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', michael@0: 'Sep', 'Oct', 'Nov', 'Dec' ]; michael@0: var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; michael@0: michael@0: var d = new Date(); michael@0: return days[d.getUTCDay()] + ", " + d.getUTCDate() + " " michael@0: + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta) michael@0: + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" michael@0: + d.getUTCSeconds() + " UTC"; michael@0: }