test_suspend_channel_on_examine.js (1952B)
1 // This file tests async handling of a channel suspended in http-on-modify-request. 2 "use strict"; 3 4 const { HttpServer } = ChromeUtils.importESModule( 5 "resource://testing-common/httpd.sys.mjs" 6 ); 7 8 var obs = Services.obs; 9 10 var baseUrl; 11 12 function responseHandler(metadata, response) { 13 var text = "testing"; 14 response.setHeader("Content-Type", "text/plain", false); 15 response.setHeader("Set-Cookie", "chewy", false); 16 response.bodyOutputStream.write(text, text.length); 17 } 18 19 function onExamineListener(callback) { 20 obs.addObserver( 21 { 22 observe(subject) { 23 obs.removeObserver(this, "http-on-examine-response"); 24 callback(subject.QueryInterface(Ci.nsIHttpChannel)); 25 }, 26 }, 27 "http-on-examine-response" 28 ); 29 } 30 31 function startChannelRequest(uri, flags, callback) { 32 var chan = NetUtil.newChannel({ 33 uri, 34 loadUsingSystemPrincipal: true, 35 }); 36 chan.asyncOpen(new ChannelListener(callback, null, flags)); 37 } 38 39 // We first make a request that we'll cancel asynchronously. The response will 40 // still contain the set-cookie header. Then verify the cookie was not actually 41 // retained. 42 add_test(function testAsyncCancel() { 43 onExamineListener(chan => { 44 // Suspend the channel then yield to make this async. 45 chan.suspend(); 46 Promise.resolve().then(() => { 47 chan.cancel(Cr.NS_BINDING_ABORTED); 48 chan.resume(); 49 }); 50 }); 51 startChannelRequest(baseUrl, CL_EXPECT_FAILURE, (request, data) => { 52 Assert.ok(!data, "no response"); 53 54 Assert.equal( 55 Services.cookies.countCookiesFromHost("localhost"), 56 0, 57 "no cookies set" 58 ); 59 60 executeSoon(run_next_test); 61 }); 62 }); 63 64 function run_test() { 65 var httpServer = new HttpServer(); 66 httpServer.registerPathHandler("/", responseHandler); 67 httpServer.start(-1); 68 69 baseUrl = `http://localhost:${httpServer.identity.primaryPort}`; 70 71 run_next_test(); 72 73 registerCleanupFunction(function () { 74 httpServer.stop(() => {}); 75 }); 76 }