summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_inhibit_caching.js
blob: 7e81eb69684cba1a38cf33e8a9de0068f5d25f83 (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
Cu.import('resource://gre/modules/LoadContextInfo.jsm');
Cu.import("resource://testing-common/httpd.js");
Cu.import("resource://gre/modules/NetUtil.jsm");

var first = true;
function contentHandler(metadata, response)
{
  response.setHeader("Content-Type", 'text/plain');
  var body = "first";
  if (!first) {
    body = "second";
  }
  first = false;
  response.bodyOutputStream.write(body, body.length);
}

XPCOMUtils.defineLazyGetter(this, "uri", function() {
  return "http://localhost:" + httpserver.identity.primaryPort;
});

var httpserver = null;

function run_test()
{
  // setup test
  httpserver = new HttpServer();
  httpserver.registerPathHandler("/test", contentHandler);
  httpserver.start(-1);

  add_test(test_first_response);
  add_test(test_inhibit_caching);

  run_next_test();
}

// Makes a regular request
function test_first_response() {
  var chan = NetUtil.newChannel({uri: uri+"/test", loadUsingSystemPrincipal: true});
  chan.asyncOpen2(new ChannelListener(check_first_response, null));
}

// Checks that we got the appropriate response
function check_first_response(request, buffer) {
  request.QueryInterface(Ci.nsIHttpChannel);
  do_check_eq(request.responseStatus, 200);
  do_check_eq(buffer, "first");
  // Open the cache entry to check its contents
  asyncOpenCacheEntry(uri+"/test","disk", Ci.nsICacheStorage.OPEN_READONLY, null, cache_entry_callback);
}

// Checks that the cache entry has the correct contents
function cache_entry_callback(status, entry) {
  equal(status, Cr.NS_OK);
  var inputStream = entry.openInputStream(0);
  pumpReadStream(inputStream, function(read) {
      inputStream.close();
      equal(read,"first");
      run_next_test();
  });
}

// Makes a request with the INHIBIT_CACHING load flag
function test_inhibit_caching() {
  var chan = NetUtil.newChannel({uri: uri+"/test", loadUsingSystemPrincipal: true});
  chan.QueryInterface(Ci.nsIRequest).loadFlags |= Ci.nsIRequest.INHIBIT_CACHING;
  chan.asyncOpen2(new ChannelListener(check_second_response, null));
}

// Checks that we got a different response from the first request
function check_second_response(request, buffer) {
  request.QueryInterface(Ci.nsIHttpChannel);
  do_check_eq(request.responseStatus, 200);
  do_check_eq(buffer, "second");
  // Checks that the cache entry still contains the content from the first request
  asyncOpenCacheEntry(uri+"/test","disk", Ci.nsICacheStorage.OPEN_READONLY, null, cache_entry_callback);
}