summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug482934.js
blob: ee2579b1bb87c1df1fc00ddc8ae2562773d262f5 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

var response_code;
var response_body;

var request_time;
var response_time;

var cache_storage;

var httpserver = new HttpServer();
httpserver.start(-1);

var base_url = "http://localhost:" + httpserver.identity.primaryPort;
var resource = "/resource";
var resource_url = base_url + resource;

// Test flags
var hit_server = false;

function make_channel(aUrl) {
  // Reset test global status
  hit_server = false;

  var req = NetUtil.newChannel({ uri: aUrl, loadUsingSystemPrincipal: true });
  req.QueryInterface(Ci.nsIHttpChannel);
  req.setRequestHeader("If-Modified-Since", request_time, false);
  return req;
}

function make_uri(aUrl) {
  return Services.io.newURI(aUrl);
}

function resource_handler(aMetadata, aResponse) {
  hit_server = true;
  Assert.ok(aMetadata.hasHeader("If-Modified-Since"));
  Assert.equal(aMetadata.getHeader("If-Modified-Since"), request_time);

  if (response_code == "200") {
    aResponse.setStatusLine(aMetadata.httpVersion, 200, "OK");
    aResponse.setHeader("Content-Type", "text/plain", false);
    aResponse.setHeader("Last-Modified", response_time, false);

    aResponse.bodyOutputStream.write(response_body, response_body.length);
  } else if (response_code == "304") {
    aResponse.setStatusLine(aMetadata.httpVersion, 304, "Not Modified");
    aResponse.setHeader("Returned-From-Handler", "1");
  }
}

function check_cached_data(aCachedData, aCallback) {
  asyncOpenCacheEntry(
    resource_url,
    "disk",
    Ci.nsICacheStorage.OPEN_READONLY,
    null,
    function (aStatus, aEntry) {
      Assert.equal(aStatus, Cr.NS_OK);
      pumpReadStream(aEntry.openInputStream(0), function (aData) {
        Assert.equal(aData, aCachedData);
        aCallback();
      });
    }
  );
}

function run_test() {
  do_get_profile();
  evict_cache_entries();

  do_test_pending();

  cache_storage = getCacheStorage("disk");
  httpserver.registerPathHandler(resource, resource_handler);

  wait_for_cache_index(run_next_test);
}

// 1. send custom conditional request when we don't have an entry
//    server returns 304 -> client receives 304
add_test(() => {
  response_code = "304";
  response_body = "";
  request_time = "Thu, 1 Jan 2009 00:00:00 GMT";
  response_time = "Thu, 1 Jan 2009 00:00:00 GMT";

  var ch = make_channel(resource_url);
  ch.asyncOpen(
    new ChannelListener(function (aRequest, aData) {
      syncWithCacheIOThread(() => {
        Assert.ok(hit_server);
        Assert.equal(
          aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
          304
        );
        Assert.ok(!cache_storage.exists(make_uri(resource_url), ""));
        Assert.equal(aRequest.getResponseHeader("Returned-From-Handler"), "1");

        run_next_test();
      }, true);
    }, null)
  );
});

// 2. send custom conditional request when we don't have an entry
//    server returns 200 -> result is cached
add_test(() => {
  response_code = "200";
  response_body = "content_body";
  request_time = "Thu, 1 Jan 2009 00:00:00 GMT";
  response_time = "Fri, 2 Jan 2009 00:00:00 GMT";

  var ch = make_channel(resource_url);
  ch.asyncOpen(
    new ChannelListener(function (aRequest, aData) {
      syncWithCacheIOThread(() => {
        Assert.ok(hit_server);
        Assert.equal(
          aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
          200
        );
        Assert.ok(cache_storage.exists(make_uri(resource_url), ""));

        check_cached_data(response_body, run_next_test);
      }, true);
    }, null)
  );
});

// 3. send custom conditional request when we have an entry
//    server returns 304 -> client receives 304 and cached entry is unchanged
add_test(() => {
  response_code = "304";
  var cached_body = response_body;
  response_body = "";
  request_time = "Fri, 2 Jan 2009 00:00:00 GMT";
  response_time = "Fri, 2 Jan 2009 00:00:00 GMT";

  var ch = make_channel(resource_url);
  ch.asyncOpen(
    new ChannelListener(function (aRequest, aData) {
      syncWithCacheIOThread(() => {
        Assert.ok(hit_server);
        Assert.equal(
          aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
          304
        );
        Assert.ok(cache_storage.exists(make_uri(resource_url), ""));
        Assert.equal(aRequest.getResponseHeader("Returned-From-Handler"), "1");
        Assert.equal(aData, "");

        // Check the cache data is not changed
        check_cached_data(cached_body, run_next_test);
      }, true);
    }, null)
  );
});

// 4. send custom conditional request when we have an entry
//    server returns 200 -> result is cached
add_test(() => {
  response_code = "200";
  response_body = "updated_content_body";
  request_time = "Fri, 2 Jan 2009 00:00:00 GMT";
  response_time = "Sat, 3 Jan 2009 00:00:00 GMT";
  var ch = make_channel(resource_url);
  ch.asyncOpen(
    new ChannelListener(function (aRequest, aData) {
      syncWithCacheIOThread(() => {
        Assert.ok(hit_server);
        Assert.equal(
          aRequest.QueryInterface(Ci.nsIHttpChannel).responseStatus,
          200
        );
        Assert.ok(cache_storage.exists(make_uri(resource_url), ""));

        // Check the cache data is updated
        check_cached_data(response_body, () => {
          run_next_test();
          httpserver.stop(do_test_finished);
        });
      }, true);
    }, null)
  );
});