netwerk/test/unit/test_bug468594.js

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

michael@0 1 //
michael@0 2 // This script emulates the test called "Freshness"
michael@0 3 // by Mark Nottingham, located at
michael@0 4 //
michael@0 5 // http://mnot.net/javascript/xmlhttprequest/cache.html
michael@0 6 //
michael@0 7 // The issue with Mr. Nottinghams page is that the server
michael@0 8 // always seems to send an Expires-header in the response,
michael@0 9 // breaking the finer details of the test. This script has
michael@0 10 // full control of response-headers, however, and can perform
michael@0 11 // the intended testing plus some extra stuff.
michael@0 12 //
michael@0 13 // Please see RFC 2616 section 13.2.1 6th paragraph for the
michael@0 14 // definition of "explicit expiration time" being used here.
michael@0 15
michael@0 16 Cu.import("resource://testing-common/httpd.js");
michael@0 17
michael@0 18 var httpserver = new HttpServer();
michael@0 19 var index = 0;
michael@0 20 var tests = [
michael@0 21 {url: "/freshness", server: "0", expected: "0"},
michael@0 22 {url: "/freshness", server: "1", expected: "0"}, // cached
michael@0 23
michael@0 24 // RFC 2616 section 13.9 2nd paragraph
michael@0 25 {url: "/freshness?a", server: "2", expected: "2"},
michael@0 26 {url: "/freshness?a", server: "3", expected: "3"},
michael@0 27
michael@0 28 // explicit expiration dates in the future should be cached
michael@0 29 {url: "/freshness?b", server: "4", expected: "4",
michael@0 30 responseheader: "Expires: "+getDateString(1)},
michael@0 31 {url: "/freshness?b", server: "5", expected: "4"},// cached due to Expires
michael@0 32
michael@0 33 {url: "/freshness?c", server: "6", expected: "6",
michael@0 34 responseheader: "Cache-Control: max-age=3600"},
michael@0 35 {url: "/freshness?c", server: "7", expected: "6"}, // cached due to max-age
michael@0 36
michael@0 37 // explicit expiration dates in the past should NOT be cached
michael@0 38 {url: "/freshness?d", server: "8", expected: "8",
michael@0 39 responseheader: "Expires: "+getDateString(-1)},
michael@0 40 {url: "/freshness?d", server: "9", expected: "9"},
michael@0 41
michael@0 42 {url: "/freshness?e", server: "10", expected: "10",
michael@0 43 responseheader: "Cache-Control: max-age=0"},
michael@0 44 {url: "/freshness?e", server: "11", expected: "11"},
michael@0 45
michael@0 46 {url: "/freshness", server: "99", expected: "0"}, // cached
michael@0 47 ];
michael@0 48
michael@0 49 function logit(i, data) {
michael@0 50 dump(tests[i].url + "\t requested [" + tests[i].server + "]" +
michael@0 51 " got [" + data + "] expected [" + tests[i].expected + "]");
michael@0 52 if (tests[i].responseheader)
michael@0 53 dump("\t[" + tests[i].responseheader + "]");
michael@0 54 dump("\n");
michael@0 55 }
michael@0 56
michael@0 57 function setupChannel(suffix, value) {
michael@0 58 var ios = Components.classes["@mozilla.org/network/io-service;1"].
michael@0 59 getService(Ci.nsIIOService);
michael@0 60 var chan = ios.newChannel("http://localhost:" + httpserver.identity.primaryPort + suffix, "", null);
michael@0 61 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 62 httpChan.requestMethod = "GET";
michael@0 63 httpChan.setRequestHeader("x-request", value, false);
michael@0 64 return httpChan;
michael@0 65 }
michael@0 66
michael@0 67 function triggerNextTest() {
michael@0 68 var channel = setupChannel(tests[index].url, tests[index].server);
michael@0 69 channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null),null);
michael@0 70 }
michael@0 71
michael@0 72 function checkValueAndTrigger(request, data, ctx) {
michael@0 73 logit(index, data);
michael@0 74 do_check_eq(tests[index].expected, data);
michael@0 75
michael@0 76 if (index < tests.length-1) {
michael@0 77 index++;
michael@0 78 triggerNextTest();
michael@0 79 } else {
michael@0 80 httpserver.stop(do_test_finished);
michael@0 81 }
michael@0 82 }
michael@0 83
michael@0 84 function run_test() {
michael@0 85 httpserver.registerPathHandler("/freshness", handler);
michael@0 86 httpserver.start(-1);
michael@0 87
michael@0 88 // clear cache
michael@0 89 evict_cache_entries();
michael@0 90 triggerNextTest();
michael@0 91
michael@0 92 do_test_pending();
michael@0 93 }
michael@0 94
michael@0 95 function handler(metadata, response) {
michael@0 96 var body = metadata.getHeader("x-request");
michael@0 97 response.setHeader("Content-Type", "text/plain", false);
michael@0 98 response.setHeader("Date", getDateString(0), false);
michael@0 99
michael@0 100 var header = tests[index].responseheader;
michael@0 101 if (header == null) {
michael@0 102 response.setHeader("Last-Modified", getDateString(-1), false);
michael@0 103 } else {
michael@0 104 var splitHdr = header.split(": ");
michael@0 105 response.setHeader(splitHdr[0], splitHdr[1], false);
michael@0 106 }
michael@0 107
michael@0 108 response.setStatusLine(metadata.httpVersion, 200, "OK");
michael@0 109 response.bodyOutputStream.write(body, body.length);
michael@0 110 }
michael@0 111
michael@0 112 function getDateString(yearDelta) {
michael@0 113 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
michael@0 114 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
michael@0 115 var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
michael@0 116
michael@0 117 var d = new Date();
michael@0 118 return days[d.getUTCDay()] + ", " +
michael@0 119 d.getUTCDate() + " " +
michael@0 120 months[d.getUTCMonth()] + " " +
michael@0 121 (d.getUTCFullYear() + yearDelta) + " " +
michael@0 122 d.getUTCHours() + ":" + d.getUTCMinutes() +":" +
michael@0 123 d.getUTCSeconds() + " UTC";
michael@0 124 }

mercurial