Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // |
michael@0 | 2 | // Verify that the VALIDATE_NEVER and LOAD_FROM_CACHE flags override |
michael@0 | 3 | // heuristic query freshness as defined in RFC 2616 section 13.9 |
michael@0 | 4 | // |
michael@0 | 5 | |
michael@0 | 6 | Cu.import("resource://testing-common/httpd.js"); |
michael@0 | 7 | |
michael@0 | 8 | var httpserver = new HttpServer(); |
michael@0 | 9 | var index = 0; |
michael@0 | 10 | var tests = [ |
michael@0 | 11 | // RFC 2616 section 13.9 2nd paragraph - query-url should be validated |
michael@0 | 12 | {url: "/freshness?a", server: "0", expected: "0"}, |
michael@0 | 13 | {url: "/freshness?a", server: "1", expected: "1"}, |
michael@0 | 14 | |
michael@0 | 15 | // Setting the VALIDATE_NEVER flag should grab entry from cache |
michael@0 | 16 | {url: "/freshness?a", server: "2", expected: "1", |
michael@0 | 17 | flags: Components.interfaces.nsIRequest.VALIDATE_NEVER }, |
michael@0 | 18 | |
michael@0 | 19 | // Finally, check that request is validated with no flags set |
michael@0 | 20 | {url: "/freshness?a", server: "99", expected: "99"}, |
michael@0 | 21 | |
michael@0 | 22 | |
michael@0 | 23 | // RFC 2616 section 13.9 2nd paragraph - query-url should be validated |
michael@0 | 24 | {url: "/freshness?b", server: "0", expected: "0"}, |
michael@0 | 25 | {url: "/freshness?b", server: "1", expected: "1"}, |
michael@0 | 26 | |
michael@0 | 27 | // Setting the LOAD_FROM_CACHE flag also grab the entry from cache |
michael@0 | 28 | {url: "/freshness?b", server: "2", expected: "1", |
michael@0 | 29 | flags: Components.interfaces.nsIRequest.LOAD_FROM_CACHE }, |
michael@0 | 30 | |
michael@0 | 31 | // Finally, check that request is validated with no flags set |
michael@0 | 32 | {url: "/freshness?b", server: "99", expected: "99"}, |
michael@0 | 33 | |
michael@0 | 34 | ]; |
michael@0 | 35 | |
michael@0 | 36 | function logit(i, data) { |
michael@0 | 37 | dump(tests[i].url + "\t requested [" + tests[i].server + "]" + |
michael@0 | 38 | " got [" + data + "] expected [" + tests[i].expected + "]"); |
michael@0 | 39 | if (tests[i].responseheader) |
michael@0 | 40 | dump("\t[" + tests[i].responseheader + "]"); |
michael@0 | 41 | dump("\n"); |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | function setupChannel(suffix, value) { |
michael@0 | 45 | var ios = Components.classes["@mozilla.org/network/io-service;1"]. |
michael@0 | 46 | getService(Ci.nsIIOService); |
michael@0 | 47 | var chan = ios.newChannel("http://localhost:" + |
michael@0 | 48 | httpserver.identity.primaryPort + |
michael@0 | 49 | suffix, "", null); |
michael@0 | 50 | var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel); |
michael@0 | 51 | httpChan.requestMethod = "GET"; |
michael@0 | 52 | httpChan.setRequestHeader("x-request", value, false); |
michael@0 | 53 | return httpChan; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | function triggerNextTest() { |
michael@0 | 57 | var test = tests[index]; |
michael@0 | 58 | var channel = setupChannel(test.url, test.server); |
michael@0 | 59 | if (test.flags) channel.loadFlags = test.flags; |
michael@0 | 60 | channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null), null); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | function checkValueAndTrigger(request, data, ctx) { |
michael@0 | 64 | logit(index, data); |
michael@0 | 65 | do_check_eq(tests[index].expected, data); |
michael@0 | 66 | |
michael@0 | 67 | if (index < tests.length-1) { |
michael@0 | 68 | index++; |
michael@0 | 69 | // this call happens in onStopRequest from the channel, and opening a |
michael@0 | 70 | // new channel to the same url here is no good idea... post it instead |
michael@0 | 71 | do_timeout(1, triggerNextTest); |
michael@0 | 72 | } else { |
michael@0 | 73 | httpserver.stop(do_test_finished); |
michael@0 | 74 | } |
michael@0 | 75 | } |
michael@0 | 76 | |
michael@0 | 77 | function run_test() { |
michael@0 | 78 | httpserver.registerPathHandler("/freshness", handler); |
michael@0 | 79 | httpserver.start(-1); |
michael@0 | 80 | |
michael@0 | 81 | // clear cache |
michael@0 | 82 | evict_cache_entries(); |
michael@0 | 83 | |
michael@0 | 84 | triggerNextTest(); |
michael@0 | 85 | |
michael@0 | 86 | do_test_pending(); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | function handler(metadata, response) { |
michael@0 | 90 | var body = metadata.getHeader("x-request"); |
michael@0 | 91 | response.setHeader("Content-Type", "text/plain", false); |
michael@0 | 92 | response.setHeader("Date", getDateString(0), false); |
michael@0 | 93 | |
michael@0 | 94 | var header = tests[index].responseheader; |
michael@0 | 95 | if (header == null) { |
michael@0 | 96 | response.setHeader("Last-Modified", getDateString(-1), false); |
michael@0 | 97 | } else { |
michael@0 | 98 | var splitHdr = header.split(": "); |
michael@0 | 99 | response.setHeader(splitHdr[0], splitHdr[1], false); |
michael@0 | 100 | } |
michael@0 | 101 | |
michael@0 | 102 | response.setStatusLine(metadata.httpVersion, 200, "OK"); |
michael@0 | 103 | response.bodyOutputStream.write(body, body.length); |
michael@0 | 104 | } |
michael@0 | 105 | |
michael@0 | 106 | function getDateString(yearDelta) { |
michael@0 | 107 | var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
michael@0 | 108 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; |
michael@0 | 109 | var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']; |
michael@0 | 110 | |
michael@0 | 111 | var d = new Date(); |
michael@0 | 112 | return days[d.getUTCDay()] + ", " + |
michael@0 | 113 | d.getUTCDate() + " " + |
michael@0 | 114 | months[d.getUTCMonth()] + " " + |
michael@0 | 115 | (d.getUTCFullYear() + yearDelta) + " " + |
michael@0 | 116 | d.getUTCHours() + ":" + d.getUTCMinutes() +":" + |
michael@0 | 117 | d.getUTCSeconds() + " UTC"; |
michael@0 | 118 | } |