summaryrefslogtreecommitdiff
path: root/netwerk/test/unit/test_bug468594.js
blob: 66d463190e09ee704967661ee7b50c248762d9b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//
// This script emulates the test called "Freshness"
// by Mark Nottingham, located at
//
// http://mnot.net/javascript/xmlhttprequest/cache.html
//
// The issue with Mr. Nottinghams page is that the server
// always seems to send an Expires-header in the response,
// breaking the finer details of the test. This script has
// full control of response-headers, however, and can perform
// the intended testing plus some extra stuff.
//
// Please see RFC 2616 section 13.2.1 6th paragraph for the
// definition of "explicit expiration time" being used here.

Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");

var httpserver = new HttpServer();
var index = 0;
var tests = [
    {url: "/freshness",   server: "0", expected: "0"},
    {url: "/freshness",   server: "1", expected: "0"}, // cached

    // RFC 2616 section 13.9 2nd paragraph says not to heuristically cache
    // querystring, but we allow it to maintain web compat
    {url: "/freshness?a", server: "2", expected: "2"},
    {url: "/freshness?a", server: "3", expected: "2"},

    // explicit expiration dates in the future should be cached
    {url: "/freshness?b", server: "4", expected: "4",
     responseheader: "Expires: "+getDateString(1)},
    {url: "/freshness?b", server: "5", expected: "4"},// cached due to Expires

    {url: "/freshness?c", server: "6", expected: "6",
     responseheader: "Cache-Control: max-age=3600"},
    {url: "/freshness?c", server: "7", expected: "6"}, // cached due to max-age

    // explicit expiration dates in the past should NOT be cached
    {url: "/freshness?d", server: "8", expected: "8",
     responseheader: "Expires: "+getDateString(-1)},
    {url: "/freshness?d", server: "9", expected: "9"},

    {url: "/freshness?e", server: "10", expected: "10",
     responseheader: "Cache-Control: max-age=0"},
    {url: "/freshness?e", server: "11", expected: "11"},

    {url: "/freshness",   server: "99", expected: "0"}, // cached
];

function logit(i, data) {
    dump(tests[i].url + "\t requested [" + tests[i].server + "]" +
         " got [" + data + "] expected [" + tests[i].expected + "]");
    if (tests[i].responseheader)
        dump("\t[" + tests[i].responseheader + "]");
    dump("\n");
}

function setupChannel(suffix, value) {
    var chan = NetUtil.newChannel({
        uri: "http://localhost:" + httpserver.identity.primaryPort + suffix,
        loadUsingSystemPrincipal: true
    });
    var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
    httpChan.requestMethod = "GET";
    httpChan.setRequestHeader("x-request", value, false);
    return httpChan;
}

function triggerNextTest() {
    var channel = setupChannel(tests[index].url, tests[index].server);
    channel.asyncOpen2(new ChannelListener(checkValueAndTrigger, null));
}

function checkValueAndTrigger(request, data, ctx) {
    logit(index, data);
    do_check_eq(tests[index].expected, data);

    if (index < tests.length-1) {
        index++;
        triggerNextTest();
    } else {
        httpserver.stop(do_test_finished);
    }
}

function run_test() {
    httpserver.registerPathHandler("/freshness", handler);
    httpserver.start(-1);

    // clear cache
    evict_cache_entries();
    triggerNextTest();

    do_test_pending();
}

function handler(metadata, response) {
    var body = metadata.getHeader("x-request");
    response.setHeader("Content-Type", "text/plain", false);
    response.setHeader("Date", getDateString(0), false);

    var header = tests[index].responseheader;
    if (header == null) {
        response.setHeader("Last-Modified", getDateString(-1), false);
    } else {
        var splitHdr = header.split(": ");
        response.setHeader(splitHdr[0], splitHdr[1], false);
    }

    response.setStatusLine(metadata.httpVersion, 200, "OK");
    response.bodyOutputStream.write(body, body.length);
}

function getDateString(yearDelta) {
    var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
                  'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
    var days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

    var d = new Date();
    return days[d.getUTCDay()] + ", " +
            d.getUTCDate() + " " +
            months[d.getUTCMonth()] + " " +
            (d.getUTCFullYear() + yearDelta) + " " +
            d.getUTCHours() + ":" + d.getUTCMinutes() +":" +
            d.getUTCSeconds() + " UTC";
}