netwerk/test/unit/test_bug203271.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 //
michael@0 2 // Tests if a response with an Expires-header in the past
michael@0 3 // and Cache-Control: max-age in the future works as
michael@0 4 // specified in RFC 2616 section 14.9.3 by letting max-age
michael@0 5 // take precedence
michael@0 6
michael@0 7 Cu.import("resource://testing-common/httpd.js");
michael@0 8 const BUGID = "203271";
michael@0 9
michael@0 10 var httpserver = new HttpServer();
michael@0 11 var index = 0;
michael@0 12 var tests = [
michael@0 13 // original problem described in bug#203271
michael@0 14 {url: "/precedence", server: "0", expected: "0",
michael@0 15 responseheader: [ "Expires: " + getDateString(-1),
michael@0 16 "Cache-Control: max-age=3600"]},
michael@0 17
michael@0 18 {url: "/precedence?0", server: "0", expected: "0",
michael@0 19 responseheader: [ "Cache-Control: max-age=3600",
michael@0 20 "Expires: " + getDateString(-1)]},
michael@0 21
michael@0 22 // max-age=1s, expires=1 year from now
michael@0 23 {url: "/precedence?1", server: "0", expected: "0",
michael@0 24 responseheader: [ "Expires: " + getDateString(1),
michael@0 25 "Cache-Control: max-age=1"]},
michael@0 26
michael@0 27 // expires=now
michael@0 28 {url: "/precedence?2", server: "0", expected: "0",
michael@0 29 responseheader: [ "Expires: " + getDateString(0)]},
michael@0 30
michael@0 31 // max-age=1s
michael@0 32 {url: "/precedence?3", server: "0", expected: "0",
michael@0 33 responseheader: ["Cache-Control: max-age=1"]},
michael@0 34
michael@0 35 // The test below is the example from
michael@0 36 //
michael@0 37 // https://bugzilla.mozilla.org/show_bug.cgi?id=203271#c27
michael@0 38 //
michael@0 39 // max-age=2592000s (1 month), expires=1 year from now, date=1 year ago
michael@0 40 {url: "/precedence?4", server: "0", expected: "0",
michael@0 41 responseheader: [ "Cache-Control: private, max-age=2592000",
michael@0 42 "Expires: " + getDateString(+1)],
michael@0 43 explicitDate: getDateString(-1)},
michael@0 44
michael@0 45 // The two tests below are also examples of clocks really out of synch
michael@0 46 // max-age=1s, date=1 year from now
michael@0 47 {url: "/precedence?5", server: "0", expected: "0",
michael@0 48 responseheader: [ "Cache-Control: max-age=1"],
michael@0 49 explicitDate: getDateString(1)},
michael@0 50
michael@0 51 // max-age=60s, date=1 year from now
michael@0 52 {url: "/precedence?6", server: "0", expected: "0",
michael@0 53 responseheader: [ "Cache-Control: max-age=60"],
michael@0 54 explicitDate: getDateString(1)},
michael@0 55
michael@0 56 // this is just to get a pause of 3s to allow cache-entries to expire
michael@0 57 {url: "/precedence?999", server: "0", expected: "0", delay: "3000"},
michael@0 58
michael@0 59 // Below are the cases which actually matters
michael@0 60 {url: "/precedence", server: "1", expected: "0"}, // should be cached
michael@0 61
michael@0 62 {url: "/precedence?0", server: "1", expected: "0"}, // should be cached
michael@0 63
michael@0 64 {url: "/precedence?1", server: "1", expected: "1"}, // should have expired
michael@0 65
michael@0 66 {url: "/precedence?2", server: "1", expected: "1"}, // should have expired
michael@0 67
michael@0 68 {url: "/precedence?3", server: "1", expected: "1"}, // should have expired
michael@0 69
michael@0 70 {url: "/precedence?4", server: "1", expected: "1"}, // should have expired
michael@0 71
michael@0 72 {url: "/precedence?5", server: "1", expected: "1"}, // should have expired
michael@0 73
michael@0 74 {url: "/precedence?6", server: "1", expected: "0"}, // should be cached
michael@0 75
michael@0 76 ];
michael@0 77
michael@0 78 function logit(i, data, ctx) {
michael@0 79 dump("requested [" + tests[i].server + "] " +
michael@0 80 "got [" + data + "] " +
michael@0 81 "expected [" + tests[i].expected + "]");
michael@0 82
michael@0 83 if (tests[i].responseheader)
michael@0 84 dump("\t[" + tests[i].responseheader + "]");
michael@0 85 dump("\n");
michael@0 86 // Dump all response-headers
michael@0 87 dump("\n===================================\n")
michael@0 88 ctx.visitResponseHeaders({
michael@0 89 visitHeader: function(key, val) {
michael@0 90 dump("\t" + key + ":"+val + "\n");
michael@0 91 }}
michael@0 92 );
michael@0 93 dump("===================================\n")
michael@0 94 }
michael@0 95
michael@0 96 function setupChannel(suffix, value) {
michael@0 97 var ios = Components.classes["@mozilla.org/network/io-service;1"].
michael@0 98 getService(Ci.nsIIOService);
michael@0 99 var chan = ios.newChannel("http://localhost:" +
michael@0 100 httpserver.identity.primaryPort + suffix,
michael@0 101 "", null);
michael@0 102 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 103 httpChan.requestMethod = "GET"; // default value, just being paranoid...
michael@0 104 httpChan.setRequestHeader("x-request", value, false);
michael@0 105 return httpChan;
michael@0 106 }
michael@0 107
michael@0 108 function triggerNextTest() {
michael@0 109 var channel = setupChannel(tests[index].url, tests[index].server);
michael@0 110 channel.asyncOpen(new ChannelListener(checkValueAndTrigger, channel), null);
michael@0 111 }
michael@0 112
michael@0 113 function checkValueAndTrigger(request, data, ctx) {
michael@0 114 logit(index, data, ctx);
michael@0 115 do_check_eq(tests[index].expected, data);
michael@0 116
michael@0 117 if (index < tests.length - 1) {
michael@0 118 var delay = tests[index++].delay;
michael@0 119 if (delay) {
michael@0 120 do_timeout(delay, triggerNextTest);
michael@0 121 } else {
michael@0 122 triggerNextTest();
michael@0 123 }
michael@0 124 } else {
michael@0 125 httpserver.stop(do_test_finished);
michael@0 126 }
michael@0 127 }
michael@0 128
michael@0 129 function run_test() {
michael@0 130 httpserver.registerPathHandler("/precedence", handler);
michael@0 131 httpserver.start(-1);
michael@0 132
michael@0 133 // clear cache
michael@0 134 evict_cache_entries();
michael@0 135
michael@0 136 triggerNextTest();
michael@0 137 do_test_pending();
michael@0 138 }
michael@0 139
michael@0 140 function handler(metadata, response) {
michael@0 141 var body = metadata.getHeader("x-request");
michael@0 142 response.setHeader("Content-Type", "text/plain", false);
michael@0 143
michael@0 144 var date = tests[index].explicitDate;
michael@0 145 if (date == undefined) {
michael@0 146 response.setHeader("Date", getDateString(0), false);
michael@0 147 } else {
michael@0 148 response.setHeader("Date", date, false);
michael@0 149 }
michael@0 150
michael@0 151 var header = tests[index].responseheader;
michael@0 152 if (header == undefined) {
michael@0 153 response.setHeader("Last-Modified", getDateString(-1), false);
michael@0 154 } else {
michael@0 155 for (var i = 0; i < header.length; i++) {
michael@0 156 var splitHdr = header[i].split(": ");
michael@0 157 response.setHeader(splitHdr[0], splitHdr[1], false);
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 162 response.bodyOutputStream.write(body, body.length);
michael@0 163 }
michael@0 164
michael@0 165 function getDateString(yearDelta) {
michael@0 166 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
michael@0 167 'Sep', 'Oct', 'Nov', 'Dec'];
michael@0 168 var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
michael@0 169
michael@0 170 var d = new Date();
michael@0 171 return days[d.getUTCDay()] + ", " +
michael@0 172 d.getUTCDate() + " " +
michael@0 173 months[d.getUTCMonth()] + " " +
michael@0 174 (d.getUTCFullYear() + yearDelta) + " " +
michael@0 175 d.getUTCHours() + ":" + d.getUTCMinutes() + ":" +
michael@0 176 d.getUTCSeconds() + " UTC";
michael@0 177 }

mercurial