1 // This file tests async handling of a channel suspended in http-on-modify-request.
4 const { HttpServer } = ChromeUtils.importESModule(
5 "resource://testing-common/httpd.sys.mjs"
8 var obs = Services.obs;
10 var ios = Services.io;
12 // baseUrl is always the initial connection attempt and is handled by
13 // failResponseHandler since every test expects that request will either be
14 // redirected or cancelled.
17 function failResponseHandler(metadata, response) {
18 var text = "failure response";
19 response.setHeader("Content-Type", "text/plain", false);
20 response.bodyOutputStream.write(text, text.length);
21 Assert.ok(false, "Received request when we shouldn't.");
24 function successResponseHandler(metadata, response) {
25 var text = "success response";
26 response.setHeader("Content-Type", "text/plain", false);
27 response.bodyOutputStream.write(text, text.length);
28 Assert.ok(true, "Received expected request.");
31 function onModifyListener(callback) {
35 obs.removeObserver(this, "http-on-modify-request");
36 callback(subject.QueryInterface(Ci.nsIHttpChannel));
39 "http-on-modify-request"
43 function startChannelRequest(uri, flags, expectedResponse = null) {
44 var chan = NetUtil.newChannel({
46 loadUsingSystemPrincipal: true,
51 if (expectedResponse) {
52 Assert.equal(data, expectedResponse);
54 Assert.ok(!data, "no response");
56 executeSoon(run_next_test);
64 add_test(function testSimpleRedirect() {
65 onModifyListener(chan => {
66 chan.redirectTo(ios.newURI(`${baseUrl}/success`));
68 startChannelRequest(baseUrl, undefined, "success response");
71 add_test(function testSimpleCancel() {
72 onModifyListener(chan => {
73 chan.cancel(Cr.NS_BINDING_ABORTED);
75 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
78 add_test(function testSimpleCancelRedirect() {
79 onModifyListener(chan => {
80 chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
81 chan.cancel(Cr.NS_BINDING_ABORTED);
83 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
86 // Test a request that will get redirected asynchronously. baseUrl should
87 // not be requested, we should receive the request for the redirectedUrl.
88 add_test(function testAsyncRedirect() {
89 onModifyListener(chan => {
90 // Suspend the channel then yield to make this async.
92 Promise.resolve().then(() => {
93 chan.redirectTo(ios.newURI(`${baseUrl}/success`));
97 startChannelRequest(baseUrl, undefined, "success response");
100 add_test(function testSyncRedirect() {
101 onModifyListener(chan => {
103 chan.redirectTo(ios.newURI(`${baseUrl}/success`));
104 Promise.resolve().then(() => {
108 startChannelRequest(baseUrl, undefined, "success response");
111 add_test(function testAsyncCancel() {
112 onModifyListener(chan => {
113 // Suspend the channel then yield to make this async.
115 Promise.resolve().then(() => {
116 chan.cancel(Cr.NS_BINDING_ABORTED);
120 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
123 add_test(function testSyncCancel() {
124 onModifyListener(chan => {
126 chan.cancel(Cr.NS_BINDING_ABORTED);
127 Promise.resolve().then(() => {
131 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
134 // Test request that will get redirected and cancelled asynchronously,
135 // ensure no connection is made.
136 add_test(function testAsyncCancelRedirect() {
137 onModifyListener(chan => {
138 // Suspend the channel then yield to make this async.
140 Promise.resolve().then(() => {
141 chan.cancel(Cr.NS_BINDING_ABORTED);
142 chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
146 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
149 // Test a request that will get cancelled synchronously, ensure async redirect
151 add_test(function testSyncCancelRedirect() {
152 onModifyListener(chan => {
154 chan.cancel(Cr.NS_BINDING_ABORTED);
155 Promise.resolve().then(() => {
156 chan.redirectTo(ios.newURI(`${baseUrl}/fail`));
160 startChannelRequest(baseUrl, CL_EXPECT_FAILURE);
163 function run_test() {
164 var httpServer = new HttpServer();
165 httpServer.registerPathHandler("/", failResponseHandler);
166 httpServer.registerPathHandler("/fail", failResponseHandler);
167 httpServer.registerPathHandler("/success", successResponseHandler);
168 httpServer.start(-1);
170 baseUrl = `http://localhost:${httpServer.identity.primaryPort}`;
174 registerCleanupFunction(function () {
175 httpServer.stop(() => {});