Thu, 15 Jan 2015 15:55:04 +0100
Back out 97036ab72558 which inappropriately compared turds to third parties.
michael@0 | 1 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 2 | |
michael@0 | 3 | var httpServer = null; |
michael@0 | 4 | var path = "/bug699001"; |
michael@0 | 5 | |
michael@0 | 6 | XPCOMUtils.defineLazyGetter(this, "URI", function() { |
michael@0 | 7 | return "http://localhost:" + httpServer.identity.primaryPort + path; |
michael@0 | 8 | }); |
michael@0 | 9 | |
michael@0 | 10 | function make_channel(url) { |
michael@0 | 11 | var ios = Cc["@mozilla.org/network/io-service;1"]. |
michael@0 | 12 | getService(Ci.nsIIOService); |
michael@0 | 13 | return ios.newChannel(url, "", null); |
michael@0 | 14 | } |
michael@0 | 15 | |
michael@0 | 16 | var fetched; |
michael@0 | 17 | |
michael@0 | 18 | // The test loads a resource that expires in one year, has an etag and varies only by User-Agent |
michael@0 | 19 | // First we load it, then check we load it only from the cache w/o even checking with the server |
michael@0 | 20 | // Then we modify our User-Agent and try it again |
michael@0 | 21 | // We have to get a new content (even though with the same etag) and again on next load only from |
michael@0 | 22 | // cache w/o accessing the server |
michael@0 | 23 | // Goal is to check we've updated User-Agent request header in cache after we've got 304 response |
michael@0 | 24 | // from the server |
michael@0 | 25 | |
michael@0 | 26 | var tests = [ |
michael@0 | 27 | { |
michael@0 | 28 | prepare: function() { }, |
michael@0 | 29 | test: function(response) { |
michael@0 | 30 | do_check_true(fetched); |
michael@0 | 31 | } |
michael@0 | 32 | }, |
michael@0 | 33 | { |
michael@0 | 34 | prepare: function() { }, |
michael@0 | 35 | test: function(response) { |
michael@0 | 36 | do_check_false(fetched); |
michael@0 | 37 | } |
michael@0 | 38 | }, |
michael@0 | 39 | { |
michael@0 | 40 | prepare: function() { |
michael@0 | 41 | setUA("A different User Agent"); |
michael@0 | 42 | }, |
michael@0 | 43 | test: function(response) { |
michael@0 | 44 | do_check_true(fetched); |
michael@0 | 45 | } |
michael@0 | 46 | }, |
michael@0 | 47 | { |
michael@0 | 48 | prepare: function() { }, |
michael@0 | 49 | test: function(response) { |
michael@0 | 50 | do_check_false(fetched); |
michael@0 | 51 | } |
michael@0 | 52 | }, |
michael@0 | 53 | { |
michael@0 | 54 | prepare: function() { |
michael@0 | 55 | setUA("And another User Agent"); |
michael@0 | 56 | }, |
michael@0 | 57 | test: function(response) { |
michael@0 | 58 | do_check_true(fetched); |
michael@0 | 59 | } |
michael@0 | 60 | }, |
michael@0 | 61 | { |
michael@0 | 62 | prepare: function() { }, |
michael@0 | 63 | test: function(response) { |
michael@0 | 64 | do_check_false(fetched); |
michael@0 | 65 | } |
michael@0 | 66 | } |
michael@0 | 67 | ]; |
michael@0 | 68 | |
michael@0 | 69 | function handler(metadata, response) |
michael@0 | 70 | { |
michael@0 | 71 | if (metadata.hasHeader("If-None-Match")) { |
michael@0 | 72 | response.setStatusLine(metadata.httpVersion, 304, "Not modified"); |
michael@0 | 73 | } |
michael@0 | 74 | else { |
michael@0 | 75 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 76 | response.setHeader("Content-Type", "text/plain"); |
michael@0 | 77 | |
michael@0 | 78 | var body = "body"; |
michael@0 | 79 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | fetched = true; |
michael@0 | 83 | |
michael@0 | 84 | response.setHeader("Expires", getDateString(+1)); |
michael@0 | 85 | response.setHeader("Cache-Control", "private"); |
michael@0 | 86 | response.setHeader("Vary", "User-Agent"); |
michael@0 | 87 | response.setHeader("ETag", "1234"); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | function run_test() |
michael@0 | 91 | { |
michael@0 | 92 | httpServer = new HttpServer(); |
michael@0 | 93 | httpServer.registerPathHandler(path, handler); |
michael@0 | 94 | httpServer.start(-1); |
michael@0 | 95 | |
michael@0 | 96 | do_test_pending(); |
michael@0 | 97 | |
michael@0 | 98 | nextTest(); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | function nextTest() |
michael@0 | 102 | { |
michael@0 | 103 | fetched = false; |
michael@0 | 104 | tests[0].prepare(); |
michael@0 | 105 | |
michael@0 | 106 | dump("Testing with User-Agent: " + getUA() + "\n"); |
michael@0 | 107 | var chan = make_channel(URI); |
michael@0 | 108 | |
michael@0 | 109 | // Give the old channel a chance to close the cache entry first. |
michael@0 | 110 | // XXX This is actually a race condition that might be considered a bug... |
michael@0 | 111 | do_execute_soon(function() { |
michael@0 | 112 | chan.asyncOpen(new ChannelListener(checkAndShiftTest, null), null); |
michael@0 | 113 | }); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | function checkAndShiftTest(request, response) |
michael@0 | 117 | { |
michael@0 | 118 | tests[0].test(response); |
michael@0 | 119 | |
michael@0 | 120 | tests.shift(); |
michael@0 | 121 | if (tests.length == 0) { |
michael@0 | 122 | httpServer.stop(tearDown); |
michael@0 | 123 | return; |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | nextTest(); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | function tearDown() |
michael@0 | 130 | { |
michael@0 | 131 | setUA(""); |
michael@0 | 132 | do_test_finished(); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | // Helpers |
michael@0 | 136 | |
michael@0 | 137 | function getUA() |
michael@0 | 138 | { |
michael@0 | 139 | var httphandler = Cc["@mozilla.org/network/protocol;1?name=http"]. |
michael@0 | 140 | getService(Ci.nsIHttpProtocolHandler); |
michael@0 | 141 | return httphandler.userAgent; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | function setUA(value) |
michael@0 | 145 | { |
michael@0 | 146 | var prefs = Cc["@mozilla.org/preferences-service;1"]. |
michael@0 | 147 | getService(Ci.nsIPrefBranch); |
michael@0 | 148 | prefs.setCharPref("general.useragent.override", value); |
michael@0 | 149 | } |
michael@0 | 150 | |
michael@0 | 151 | function getDateString(yearDelta) { |
michael@0 | 152 | var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', |
michael@0 | 153 | 'Sep', 'Oct', 'Nov', 'Dec' ]; |
michael@0 | 154 | var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ]; |
michael@0 | 155 | |
michael@0 | 156 | var d = new Date(); |
michael@0 | 157 | return days[d.getUTCDay()] + ", " + d.getUTCDate() + " " |
michael@0 | 158 | + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta) |
michael@0 | 159 | + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":" |
michael@0 | 160 | + d.getUTCSeconds() + " UTC"; |
michael@0 | 161 | } |