netwerk/test/unit/test_bug468426.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_bug468426.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +Cu.import("resource://testing-common/httpd.js");
     1.5 +
     1.6 +var httpserver = new HttpServer();
     1.7 +var index = 0;
     1.8 +var tests = [
     1.9 +    // Initial request. Cached variant will have no cookie
    1.10 +    { url : "/bug468426", server : "0", expected : "0", cookie: null},
    1.11 +
    1.12 +    // Cache now contains a variant with no value for cookie. If we don't
    1.13 +    // set cookie we expect to receive the cached variant
    1.14 +    { url : "/bug468426", server : "1", expected : "0", cookie: null},
    1.15 +
    1.16 +    // Cache still contains a variant with no value for cookie. If we
    1.17 +    // set a value for cookie we expect a fresh value
    1.18 +    { url : "/bug468426", server : "2", expected : "2", cookie: "c=2"},
    1.19 +
    1.20 +    // Cache now contains a variant with cookie "c=2". If the request
    1.21 +    // also set cookie "c=2", we expect to receive the cached variant.
    1.22 +    { url : "/bug468426", server : "3", expected : "2", cookie: "c=2"},
    1.23 +
    1.24 +    // Cache still contains a variant with cookie "c=2". When setting
    1.25 +    // cookie "c=4" in the request we expect a fresh value
    1.26 +    { url : "/bug468426", server : "4", expected : "4", cookie: "c=4"},
    1.27 +
    1.28 +    // Cache now contains a variant with cookie "c=4". When setting
    1.29 +    // cookie "c=4" in the request we expect the cached variant
    1.30 +    { url : "/bug468426", server : "5", expected : "4", cookie: "c=4"},
    1.31 +    
    1.32 +    // Cache still contains a variant with cookie "c=4". When setting
    1.33 +    // no cookie in the request we expect a fresh value
    1.34 +    { url : "/bug468426", server : "6", expected : "6", cookie: null},
    1.35 +
    1.36 +];
    1.37 +
    1.38 +function setupChannel(suffix, value, cookie) {
    1.39 +    var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.40 +            .getService(Ci.nsIIOService);
    1.41 +    var chan = ios.newChannel("http://localhost:" +
    1.42 +                             httpserver.identity.primaryPort + suffix, "", null);
    1.43 +    var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.44 +    httpChan.requestMethod = "GET";
    1.45 +    httpChan.setRequestHeader("x-request", value, false);
    1.46 +    if (cookie != null)
    1.47 +        httpChan.setRequestHeader("Cookie", cookie, false);
    1.48 +    return httpChan;
    1.49 +}
    1.50 +
    1.51 +function triggerNextTest() {
    1.52 +    var channel = setupChannel(tests[index].url, tests[index].server, tests[index].cookie);
    1.53 +    channel.asyncOpen(new ChannelListener(checkValueAndTrigger, null), null);
    1.54 +}
    1.55 +
    1.56 +function checkValueAndTrigger(request, data, ctx) {
    1.57 +    do_check_eq(tests[index].expected, data);
    1.58 +
    1.59 +    if (index < tests.length - 1) {
    1.60 +        index++;
    1.61 +        // This call happens in onStopRequest from the channel. Opening a new
    1.62 +        // channel to the same url here is no good idea!  Post it instead...
    1.63 +        do_timeout(1, triggerNextTest);
    1.64 +    } else {
    1.65 +        httpserver.stop(do_test_finished);
    1.66 +    }
    1.67 +}
    1.68 +
    1.69 +function run_test() {
    1.70 +    httpserver.registerPathHandler("/bug468426", handler);
    1.71 +    httpserver.start(-1);
    1.72 +
    1.73 +    // Clear cache and trigger the first test
    1.74 +    evict_cache_entries();
    1.75 +    triggerNextTest();
    1.76 +
    1.77 +    do_test_pending();
    1.78 +}
    1.79 +
    1.80 +function handler(metadata, response) {
    1.81 +    var body = "unset";
    1.82 +    try {
    1.83 +        body = metadata.getHeader("x-request");
    1.84 +    } catch(e) { }
    1.85 +    response.setStatusLine(metadata.httpVersion, 200, "Ok");
    1.86 +    response.setHeader("Content-Type", "text/plain", false);
    1.87 +    response.setHeader("Last-Modified", getDateString(-1), false);
    1.88 +    response.setHeader("Vary", "Cookie", false);
    1.89 +    response.bodyOutputStream.write(body, body.length);
    1.90 +}
    1.91 +
    1.92 +function getDateString(yearDelta) {
    1.93 +    var months = [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
    1.94 +            'Sep', 'Oct', 'Nov', 'Dec' ];
    1.95 +    var days = [ 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat' ];
    1.96 +
    1.97 +    var d = new Date();
    1.98 +    return days[d.getUTCDay()] + ", " + d.getUTCDate() + " "
    1.99 +            + months[d.getUTCMonth()] + " " + (d.getUTCFullYear() + yearDelta)
   1.100 +            + " " + d.getUTCHours() + ":" + d.getUTCMinutes() + ":"
   1.101 +            + d.getUTCSeconds() + " UTC";
   1.102 +}

mercurial