summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_suspend_channel_on_examine_merged_response.js
blob: bd35d5cc9de23eccff5b8c13ac70cfab3f9b1516 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// This file tests async handling of a channel suspended in
// notifying http-on-examine-merged-response observers.
// Note that this test is developed based on test_bug482601.js.
"use strict";

const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");

var httpserv = null;
var test_nr = 0;
var buffer = "";
var observerCalled = false;
var channelResumed = false;

var observer = {
  QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),

  observe(subject, topic, data) {
    if (
      topic === "http-on-examine-merged-response" &&
      subject instanceof Ci.nsIHttpChannel
    ) {
      var chan = subject.QueryInterface(Ci.nsIHttpChannel);
      executeSoon(() => {
        Assert.equal(channelResumed, false);
        channelResumed = true;
        chan.resume();
      });
      Assert.equal(observerCalled, false);
      observerCalled = true;
      chan.suspend();
    }
  },
};

var listener = {
  onStartRequest(request) {
    buffer = "";
  },

  onDataAvailable(request, stream, offset, count) {
    buffer = buffer.concat(read_stream(stream, count));
  },

  onStopRequest(request, status) {
    Assert.equal(status, Cr.NS_OK);
    Assert.equal(buffer, "0123456789");
    Assert.equal(channelResumed, true);
    Assert.equal(observerCalled, true);
    test_nr++;
    do_timeout(0, do_test);
  },
};

function run_test() {
  httpserv = new HttpServer();
  httpserv.registerPathHandler("/path/partial", path_partial);
  httpserv.registerPathHandler("/path/cached", path_cached);
  httpserv.start(-1);

  Services.obs.addObserver(observer, "http-on-examine-merged-response");

  do_timeout(0, do_test);
  do_test_pending();
}

function do_test() {
  if (test_nr < tests.length) {
    tests[test_nr]();
  } else {
    Services.obs.removeObserver(observer, "http-on-examine-merged-response");
    httpserv.stop(do_test_finished);
  }
}

var tests = [test_partial, test_cached];

function makeChan(url) {
  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

function storeCache(aCacheEntry, aResponseHeads, aContent) {
  aCacheEntry.setMetaDataElement("request-method", "GET");
  aCacheEntry.setMetaDataElement("response-head", aResponseHeads);
  aCacheEntry.setMetaDataElement("charset", "ISO-8859-1");

  var oStream = aCacheEntry.openOutputStream(0, aContent.length);
  var written = oStream.write(aContent, aContent.length);
  if (written != aContent.length) {
    do_throw(
      "oStream.write has not written all data!\n" +
        "  Expected: " +
        written +
        "\n" +
        "  Actual: " +
        aContent.length +
        "\n"
    );
  }
  oStream.close();
  aCacheEntry.close();
}

function test_partial() {
  observerCalled = false;
  channelResumed = false;
  asyncOpenCacheEntry(
    "http://localhost:" + httpserv.identity.primaryPort + "/path/partial",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    test_partial2
  );
}

function test_partial2(status, entry) {
  Assert.equal(status, Cr.NS_OK);
  storeCache(
    entry,
    "HTTP/1.1 200 OK\r\n" +
      "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
      "Server: httpd.js\r\n" +
      "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
      "Accept-Ranges: bytes\r\n" +
      "Content-Length: 10\r\n" +
      "Content-Type: text/plain\r\n",
    "0123"
  );

  observerCalled = false;

  var chan = makeChan(
    "http://localhost:" + httpserv.identity.primaryPort + "/path/partial"
  );
  chan.asyncOpen(listener);
}

function test_cached() {
  observerCalled = false;
  channelResumed = false;
  asyncOpenCacheEntry(
    "http://localhost:" + httpserv.identity.primaryPort + "/path/cached",
    "disk",
    Ci.nsICacheStorage.OPEN_NORMALLY,
    null,
    test_cached2
  );
}

function test_cached2(status, entry) {
  Assert.equal(status, Cr.NS_OK);
  storeCache(
    entry,
    "HTTP/1.1 200 OK\r\n" +
      "Date: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
      "Server: httpd.js\r\n" +
      "Last-Modified: Thu, 1 Jan 2009 00:00:00 GMT\r\n" +
      "Accept-Ranges: bytes\r\n" +
      "Content-Length: 10\r\n" +
      "Content-Type: text/plain\r\n",
    "0123456789"
  );

  observerCalled = false;

  var chan = makeChan(
    "http://localhost:" + httpserv.identity.primaryPort + "/path/cached"
  );
  chan.loadFlags = Ci.nsIRequest.VALIDATE_ALWAYS;
  chan.asyncOpen(listener);
}

// PATHS

// /path/partial
function path_partial(metadata, response) {
  Assert.ok(metadata.hasHeader("If-Range"));
  Assert.equal(metadata.getHeader("If-Range"), "Thu, 1 Jan 2009 00:00:00 GMT");
  Assert.ok(metadata.hasHeader("Range"));
  Assert.equal(metadata.getHeader("Range"), "bytes=4-");

  response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
  response.setHeader("Content-Range", "bytes 4-9/10", false);
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("Last-Modified", "Thu, 1 Jan 2009 00:00:00 GMT");

  var body = "456789";
  response.bodyOutputStream.write(body, body.length);
}

// /path/cached
function path_cached(metadata, response) {
  Assert.ok(metadata.hasHeader("If-Modified-Since"));
  Assert.equal(
    metadata.getHeader("If-Modified-Since"),
    "Thu, 1 Jan 2009 00:00:00 GMT"
  );

  response.setStatusLine(metadata.httpVersion, 304, "Not Modified");
}