netwerk/test/unit/test_bug468426.js

Thu, 15 Jan 2015 15:55:04 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:55:04 +0100
branch
TOR_BUG_9701
changeset 9
a63d609f5ebe
permissions
-rw-r--r--

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 = new HttpServer();
michael@0 4 var index = 0;
michael@0 5 var tests = [
michael@0 6 // Initial request. Cached variant will have no cookie
michael@0 7 { url : "/bug468426", server : "0", expected : "0", cookie: null},
michael@0 8
michael@0 9 // Cache now contains a variant with no value for cookie. If we don't
michael@0 10 // set cookie we expect to receive the cached variant
michael@0 11 { url : "/bug468426", server : "1", expected : "0", cookie: null},
michael@0 12
michael@0 13 // Cache still contains a variant with no value for cookie. If we
michael@0 14 // set a value for cookie we expect a fresh value
michael@0 15 { url : "/bug468426", server : "2", expected : "2", cookie: "c=2"},
michael@0 16
michael@0 17 // Cache now contains a variant with cookie "c=2". If the request
michael@0 18 // also set cookie "c=2", we expect to receive the cached variant.
michael@0 19 { url : "/bug468426", server : "3", expected : "2", cookie: "c=2"},
michael@0 20
michael@0 21 // Cache still contains a variant with cookie "c=2". When setting
michael@0 22 // cookie "c=4" in the request we expect a fresh value
michael@0 23 { url : "/bug468426", server : "4", expected : "4", cookie: "c=4"},
michael@0 24
michael@0 25 // Cache now contains a variant with cookie "c=4". When setting
michael@0 26 // cookie "c=4" in the request we expect the cached variant
michael@0 27 { url : "/bug468426", server : "5", expected : "4", cookie: "c=4"},
michael@0 28
michael@0 29 // Cache still contains a variant with cookie "c=4". When setting
michael@0 30 // no cookie in the request we expect a fresh value
michael@0 31 { url : "/bug468426", server : "6", expected : "6", cookie: null},
michael@0 32
michael@0 33 ];
michael@0 34
michael@0 35 function setupChannel(suffix, value, cookie) {
michael@0 36 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 37 .getService(Ci.nsIIOService);
michael@0 38 var chan = ios.newChannel("http://localhost:" +
michael@0 39 httpserver.identity.primaryPort + suffix, "", null);
michael@0 40 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 41 httpChan.requestMethod = "GET";
michael@0 42 httpChan.setRequestHeader("x-request", value, false);
michael@0 43 if (cookie != null)
michael@0 44 httpChan.setRequestHeader("Cookie", cookie, false);
michael@0 45 return httpChan;
michael@0 46 }
michael@0 47
michael@0 48 function triggerNextTest() {
michael@0 49 var channel = setupChannel(tests[index].url, tests[index].server, tests[index].cookie);
michael@0 50 channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null), null);
michael@0 51 }
michael@0 52
michael@0 53 function checkValueAndTrigger(request, data, ctx) {
michael@0 54 do_check_eq(tests[index].expected, data);
michael@0 55
michael@0 56 if (index < tests.length - 1) {
michael@0 57 index++;
michael@0 58 // This call happens in onStopRequest from the channel. Opening a new
michael@0 59 // channel to the same url here is no good idea! Post it instead...
michael@0 60 do_timeout(1, triggerNextTest);
michael@0 61 } else {
michael@0 62 httpserver.stop(do_test_finished);
michael@0 63 }
michael@0 64 }
michael@0 65
michael@0 66 function run_test() {
michael@0 67 httpserver.registerPathHandler("/bug468426", handler);
michael@0 68 httpserver.start(-1);
michael@0 69
michael@0 70 // Clear cache and trigger the first test
michael@0 71 evict_cache_entries();
michael@0 72 triggerNextTest();
michael@0 73
michael@0 74 do_test_pending();
michael@0 75 }
michael@0 76
michael@0 77 function handler(metadata, response) {
michael@0 78 var body = "unset";
michael@0 79 try {
michael@0 80 body = metadata.getHeader("x-request");
michael@0 81 } catch(e) { }
michael@0 82 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 83 response.setHeader("Content-Type", "text/plain", false);
michael@0 84 response.setHeader("Last-Modified", getDateString(-1), false);
michael@0 85 response.setHeader("Vary", "Cookie", false);
michael@0 86 response.bodyOutputStream.write(body, body.length);
michael@0 87 }
michael@0 88
michael@0 89 function getDateString(yearDelta) {
michael@0 90 var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
michael@0 91 'Sep', 'Oct', 'Nov', 'Dec' ];
michael@0 92 var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
michael@0 93
michael@0 94 var d = new Date();
michael@0 95 return days[d.getUTCDay()] + ", " + d.getUTCDate() + " "
michael@0 96 + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta)
michael@0 97 + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":"
michael@0 98 + d.getUTCSeconds() + " UTC";
michael@0 99 }

mercurial