2012-08-14 07:06:04 -07:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
const Cu = Components.utils;
|
|
|
|
const Cr = Components.results;
|
|
|
|
|
|
|
|
Cu.import("resource://testing-common/httpd.js");
|
|
|
|
var httpserver = new HttpServer();
|
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
var expectedOnStopRequests = 3;
|
2010-11-09 01:24:42 -08:00
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
function setupChannel(suffix, xRequest, flags) {
|
2010-11-09 01:24:42 -08:00
|
|
|
var ios = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Ci.nsIIOService);
|
|
|
|
var chan = ios.newChannel("http://localhost:4444" + suffix, "", null);
|
|
|
|
if (flags)
|
|
|
|
chan.loadFlags |= flags;
|
2010-12-08 15:38:42 -08:00
|
|
|
|
|
|
|
var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
|
|
|
|
httpChan.setRequestHeader("x-request", xRequest, false);
|
|
|
|
|
|
|
|
return httpChan;
|
2010-11-09 01:24:42 -08:00
|
|
|
}
|
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
function Listener(response) {
|
2010-11-09 01:24:42 -08:00
|
|
|
this._response = response;
|
|
|
|
}
|
|
|
|
Listener.prototype = {
|
|
|
|
_response: null,
|
|
|
|
_buffer: null,
|
|
|
|
|
|
|
|
QueryInterface: function(iid) {
|
|
|
|
if (iid.equals(Components.interfaces.nsIStreamListener) ||
|
|
|
|
iid.equals(Components.interfaces.nsIRequestObserver) ||
|
|
|
|
iid.equals(Components.interfaces.nsISupports))
|
|
|
|
return this;
|
|
|
|
throw Components.results.NS_ERROR_NO_INTERFACE;
|
|
|
|
},
|
|
|
|
|
|
|
|
onStartRequest: function (request, ctx) {
|
|
|
|
this._buffer = "";
|
|
|
|
},
|
|
|
|
onDataAvailable: function (request, ctx, stream, offset, count) {
|
|
|
|
this._buffer = this._buffer.concat(read_stream(stream, count));
|
|
|
|
},
|
|
|
|
onStopRequest: function (request, ctx, status) {
|
2010-12-08 15:38:42 -08:00
|
|
|
do_check_eq(this._buffer, this._response);
|
|
|
|
if (--expectedOnStopRequests == 0)
|
2010-11-09 01:24:42 -08:00
|
|
|
do_timeout(10, function() {
|
|
|
|
httpserver.stop(do_test_finished);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function run_test() {
|
|
|
|
httpserver.registerPathHandler("/bug596443", handler);
|
|
|
|
httpserver.start(4444);
|
|
|
|
|
|
|
|
// make sure we have a profile so we can use the disk-cache
|
|
|
|
do_get_profile();
|
|
|
|
|
|
|
|
// clear cache
|
2012-06-12 14:23:32 -07:00
|
|
|
evict_cache_entries();
|
2010-11-09 01:24:42 -08:00
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
var ch0 = setupChannel("/bug596443", "Response0", Ci.nsIRequest.LOAD_BYPASS_CACHE);
|
|
|
|
ch0.asyncOpen(new Listener("Response0"), null);
|
2010-11-09 01:24:42 -08:00
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
var ch1 = setupChannel("/bug596443", "Response1", Ci.nsIRequest.LOAD_BYPASS_CACHE);
|
|
|
|
ch1.asyncOpen(new Listener("Response1"), null);
|
2010-11-09 01:24:42 -08:00
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
var ch2 = setupChannel("/bug596443", "Should not be used");
|
|
|
|
ch2.asyncOpen(new Listener("Response1"), null); // Note param: we expect this to come from cache
|
2010-11-09 01:24:42 -08:00
|
|
|
|
|
|
|
do_test_pending();
|
|
|
|
}
|
|
|
|
|
|
|
|
function triggerHandlers() {
|
2010-12-08 15:38:42 -08:00
|
|
|
do_timeout(100, handlers[1]);
|
|
|
|
do_timeout(100, handlers[0]);
|
2010-11-09 01:24:42 -08:00
|
|
|
}
|
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
var handlers = [];
|
2010-11-09 01:24:42 -08:00
|
|
|
function handler(metadata, response) {
|
|
|
|
var func = function(body) {
|
|
|
|
return function() {
|
|
|
|
response.setStatusLine(metadata.httpVersion, 200, "Ok");
|
|
|
|
response.setHeader("Content-Type", "text/plain", false);
|
|
|
|
response.setHeader("Content-Length", "" + body.length, false);
|
|
|
|
response.setHeader("Cache-Control", "max-age=600", false);
|
|
|
|
response.bodyOutputStream.write(body, body.length);
|
|
|
|
response.finish();
|
|
|
|
}};
|
|
|
|
|
|
|
|
response.processAsync();
|
2010-12-08 15:38:42 -08:00
|
|
|
var request = metadata.getHeader("x-request");
|
|
|
|
handlers.push(func(request));
|
2010-11-09 01:24:42 -08:00
|
|
|
|
2010-12-08 15:38:42 -08:00
|
|
|
if (handlers.length > 1)
|
2010-11-09 01:24:42 -08:00
|
|
|
triggerHandlers();
|
|
|
|
}
|