summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_bug1064258.js
blob: da982c2d6f0704731946a13356235a268b267ab8 (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
/**
 * Check how nsICachingChannel.cacheOnlyMetadata works.
 * - all channels involved in this test are set cacheOnlyMetadata = true
 * - do a previously uncached request for a long living content
 * - check we have downloaded the content from the server (channel provides it)
 * - check the entry has metadata, but zero-length content
 * - load the same URL again, now cached
 * - check the channel is giving no content (no call to OnDataAvailable) but succeeds
 * - repeat again, but for a different URL that is not cached (immediately expires)
 * - only difference is that we get a newer version of the content from the server during the second request
 */

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

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

var httpServer = null;

function make_channel(url, callback, ctx) {
  return NetUtil.newChannel({uri: url, loadUsingSystemPrincipal: true});
}

const responseBody1 = "response body 1";
const responseBody2a = "response body 2a";
const responseBody2b = "response body 2b";

function contentHandler1(metadata, response)
{
  response.setHeader("Content-Type", "text/plain");
  response.setHeader("Cache-control", "max-age=999999");
  response.bodyOutputStream.write(responseBody1, responseBody1.length);
}

var content2passCount = 0;

function contentHandler2(metadata, response)
{
  response.setHeader("Content-Type", "text/plain");
  response.setHeader("Cache-control", "no-cache");
  switch (content2passCount++) {
    case 0:
      response.setHeader("ETag", "testetag");
      response.bodyOutputStream.write(responseBody2a, responseBody2a.length);
      break;
    case 1:
      do_check_true(metadata.hasHeader("If-None-Match"));
      do_check_eq(metadata.getHeader("If-None-Match"), "testetag");
      response.bodyOutputStream.write(responseBody2b, responseBody2b.length);
      break;
    default:
      throw "Unexpected request in the test";
  }
}


function run_test()
{
  httpServer = new HttpServer();
  httpServer.registerPathHandler("/content1", contentHandler1);
  httpServer.registerPathHandler("/content2", contentHandler2);
  httpServer.start(-1);

  run_test_content1a();
  do_test_pending();
}

function run_test_content1a()
{
  var chan = make_channel(URL + "/content1");
  caching = chan.QueryInterface(Ci.nsICachingChannel);
  caching.cacheOnlyMetadata = true;
  chan.asyncOpen2(new ChannelListener(contentListener1a, null));
}

function contentListener1a(request, buffer)
{
  do_check_eq(buffer, responseBody1);

  asyncOpenCacheEntry(URL + "/content1", "disk", 0, null, cacheCheck1)
}

function cacheCheck1(status, entry)
{
  do_check_eq(status, 0);
  do_check_eq(entry.dataSize, 0);
  try {
    do_check_neq(entry.getMetaDataElement("response-head"), null);
  }
  catch (ex) {
    do_throw("Missing response head");
  }

  var chan = make_channel(URL + "/content1");
  caching = chan.QueryInterface(Ci.nsICachingChannel);
  caching.cacheOnlyMetadata = true;
  chan.asyncOpen2(new ChannelListener(contentListener1b, null, CL_IGNORE_CL));
}

function contentListener1b(request, buffer)
{
  request.QueryInterface(Ci.nsIHttpChannel);
  do_check_eq(request.requestMethod, "GET");
  do_check_eq(request.responseStatus, 200);
  do_check_eq(request.getResponseHeader("Cache-control"), "max-age=999999");

  do_check_eq(buffer, "");
  run_test_content2a();
}

// Now same set of steps but this time for an immediately expiring content.

function run_test_content2a()
{
  var chan = make_channel(URL + "/content2");
  caching = chan.QueryInterface(Ci.nsICachingChannel);
  caching.cacheOnlyMetadata = true;
  chan.asyncOpen2(new ChannelListener(contentListener2a, null));
}

function contentListener2a(request, buffer)
{
  do_check_eq(buffer, responseBody2a);

  asyncOpenCacheEntry(URL + "/content2", "disk", 0, null, cacheCheck2)
}

function cacheCheck2(status, entry)
{
  do_check_eq(status, 0);
  do_check_eq(entry.dataSize, 0);
  try {
    do_check_neq(entry.getMetaDataElement("response-head"), null);
    do_check_true(entry.getMetaDataElement("response-head").match('etag: testetag'));
  }
  catch (ex) {
    do_throw("Missing response head");
  }

  var chan = make_channel(URL + "/content2");
  caching = chan.QueryInterface(Ci.nsICachingChannel);
  caching.cacheOnlyMetadata = true;
  chan.asyncOpen2(new ChannelListener(contentListener2b, null));
}

function contentListener2b(request, buffer)
{
  do_check_eq(buffer, responseBody2b);

  httpServer.stop(do_test_finished);
}