netwerk/test/unit/test_bug596443.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 Cu.import("resource://testing-common/httpd.js");
michael@0 2 var httpserver = new HttpServer();
michael@0 3
michael@0 4 var expectedOnStopRequests = 3;
michael@0 5
michael@0 6 function setupChannel(suffix, xRequest, flags) {
michael@0 7 var ios = Components.classes["@mozilla.org/network/io-service;1"]
michael@0 8 .getService(Ci.nsIIOService);
michael@0 9 var chan = ios.newChannel("http://localhost:" +
michael@0 10 httpserver.identity.primaryPort +
michael@0 11 suffix, "", null);
michael@0 12 if (flags)
michael@0 13 chan.loadFlags |= flags;
michael@0 14
michael@0 15 var httpChan = chan.QueryInterface(Components.interfaces.nsIHttpChannel);
michael@0 16 httpChan.setRequestHeader("x-request", xRequest, false);
michael@0 17
michael@0 18 return httpChan;
michael@0 19 }
michael@0 20
michael@0 21 function Listener(response) {
michael@0 22 this._response = response;
michael@0 23 }
michael@0 24 Listener.prototype = {
michael@0 25 _response: null,
michael@0 26 _buffer: null,
michael@0 27
michael@0 28 QueryInterface: function(iid) {
michael@0 29 if (iid.equals(Components.interfaces.nsIStreamListener) ||
michael@0 30 iid.equals(Components.interfaces.nsIRequestObserver) ||
michael@0 31 iid.equals(Components.interfaces.nsISupports))
michael@0 32 return this;
michael@0 33 throw Components.results.NS_ERROR_NO_INTERFACE;
michael@0 34 },
michael@0 35
michael@0 36 onStartRequest: function (request, ctx) {
michael@0 37 this._buffer = "";
michael@0 38 },
michael@0 39 onDataAvailable: function (request, ctx, stream, offset, count) {
michael@0 40 this._buffer = this._buffer.concat(read_stream(stream, count));
michael@0 41 },
michael@0 42 onStopRequest: function (request, ctx, status) {
michael@0 43 do_check_eq(this._buffer, this._response);
michael@0 44 if (--expectedOnStopRequests == 0)
michael@0 45 do_timeout(10, function() {
michael@0 46 httpserver.stop(do_test_finished);
michael@0 47 });
michael@0 48 }
michael@0 49 };
michael@0 50
michael@0 51 function run_test() {
michael@0 52 httpserver.registerPathHandler("/bug596443", handler);
michael@0 53 httpserver.start(-1);
michael@0 54
michael@0 55 // make sure we have a profile so we can use the disk-cache
michael@0 56 do_get_profile();
michael@0 57
michael@0 58 // clear cache
michael@0 59 evict_cache_entries();
michael@0 60
michael@0 61 var ch0 = setupChannel("/bug596443", "Response0", Ci.nsIRequest.LOAD_BYPASS_CACHE);
michael@0 62 ch0.asyncOpen(new Listener("Response0"), null);
michael@0 63
michael@0 64 var ch1 = setupChannel("/bug596443", "Response1", Ci.nsIRequest.LOAD_BYPASS_CACHE);
michael@0 65 ch1.asyncOpen(new Listener("Response1"), null);
michael@0 66
michael@0 67 var ch2 = setupChannel("/bug596443", "Should not be used");
michael@0 68 ch2.asyncOpen(new Listener("Response1"), null); // Note param: we expect this to come from cache
michael@0 69
michael@0 70 do_test_pending();
michael@0 71 }
michael@0 72
michael@0 73 function triggerHandlers() {
michael@0 74 do_timeout(100, handlers[1]);
michael@0 75 do_timeout(100, handlers[0]);
michael@0 76 }
michael@0 77
michael@0 78 var handlers = [];
michael@0 79 function handler(metadata, response) {
michael@0 80 var func = function(body) {
michael@0 81 return function() {
michael@0 82 response.setStatusLine(metadata.httpVersion, 200, "Ok");
michael@0 83 response.setHeader("Content-Type", "text/plain", false);
michael@0 84 response.setHeader("Content-Length", "" + body.length, false);
michael@0 85 response.setHeader("Cache-Control", "max-age=600", false);
michael@0 86 response.bodyOutputStream.write(body, body.length);
michael@0 87 response.finish();
michael@0 88 }};
michael@0 89
michael@0 90 response.processAsync();
michael@0 91 var request = metadata.getHeader("x-request");
michael@0 92 handlers.push(func(request));
michael@0 93
michael@0 94 if (handlers.length > 1)
michael@0 95 triggerHandlers();
michael@0 96 }

mercurial