test_bug667907.js (2106B)
1 "use strict"; 2 3 const { HttpServer } = ChromeUtils.importESModule( 4 "resource://testing-common/httpd.sys.mjs" 5 ); 6 7 var httpserver = null; 8 var simplePath = "/simple"; 9 var normalPath = "/normal"; 10 var httpbody = "<html></html>"; 11 12 ChromeUtils.defineLazyGetter(this, "uri1", function () { 13 return "http://localhost:" + httpserver.identity.primaryPort + simplePath; 14 }); 15 16 ChromeUtils.defineLazyGetter(this, "uri2", function () { 17 return "http://localhost:" + httpserver.identity.primaryPort + normalPath; 18 }); 19 20 function make_channel(url) { 21 return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true }); 22 } 23 24 var listener_proto = { 25 QueryInterface: ChromeUtils.generateQI([ 26 "nsIStreamListener", 27 "nsIRequestObserver", 28 ]), 29 30 onStartRequest(request) { 31 Assert.equal( 32 request.QueryInterface(Ci.nsIChannel).contentType, 33 this.contentType 34 ); 35 request.cancel(Cr.NS_BINDING_ABORTED); 36 }, 37 38 onDataAvailable() { 39 do_throw("Unexpected onDataAvailable"); 40 }, 41 42 onStopRequest(request, status) { 43 Assert.equal(status, Cr.NS_BINDING_ABORTED); 44 this.termination_func(); 45 }, 46 }; 47 48 function listener(contentType, termination_func) { 49 this.contentType = contentType; 50 this.termination_func = termination_func; 51 } 52 listener.prototype = listener_proto; 53 54 function run_test() { 55 httpserver = new HttpServer(); 56 httpserver.registerPathHandler(simplePath, simpleHandler); 57 httpserver.registerPathHandler(normalPath, normalHandler); 58 httpserver.start(-1); 59 60 var channel = make_channel(uri1); 61 channel.asyncOpen( 62 new listener("text/plain", function () { 63 run_test2(); 64 }) 65 ); 66 67 do_test_pending(); 68 } 69 70 function run_test2() { 71 var channel = make_channel(uri2); 72 channel.asyncOpen( 73 new listener("text/html", function () { 74 httpserver.stop(do_test_finished); 75 }) 76 ); 77 } 78 79 function simpleHandler(metadata, response) { 80 response.seizePower(); 81 response.bodyOutputStream.write(httpbody, httpbody.length); 82 response.finish(); 83 } 84 85 function normalHandler(metadata, response) { 86 response.bodyOutputStream.write(httpbody, httpbody.length); 87 response.finish(); 88 }