2 // cleaner HTTP header test infrastructure
4 // tests bugs: 589292, [add more here: see hg log for definitive list]
7 // 1) Increment up 'lastTest' to new number (say, "99")
8 // 2) Add new test 'handler99' and 'completeTest99' functions.
9 // 3) If your test should fail the necko channel, set
10 // test_flags[99] = CL_EXPECT_FAILURE.
12 // TO DEBUG JUST ONE TEST: temporarily change firstTest and lastTest to equal
13 // the test # you're interested in.
15 // For tests that need duplicate copies of headers to be sent, see
16 // test_duplicate_headers.js
20 var firstTest = 1; // set to test of interest when debugging
21 var lastTest = 4; // set to test of interest when debugging
22 ////////////////////////////////////////////////////////////////////////////////
24 // Note: sets Cc and Ci variables
26 const { HttpServer } = ChromeUtils.importESModule(
27 "resource://testing-common/httpd.sys.mjs"
30 ChromeUtils.defineLazyGetter(this, "URL", function () {
31 return "http://localhost:" + httpserver.identity.primaryPort;
34 var httpserver = new HttpServer();
35 var nextTest = firstTest;
37 var testPathBase = "/test_headers";
43 run_test_number(nextTest);
46 function runNextTest() {
47 if (nextTest == lastTest) {
52 // Make sure test functions exist
53 if (globalThis["handler" + nextTest] == undefined) {
54 do_throw("handler" + nextTest + " undefined!");
56 if (globalThis["completeTest" + nextTest] == undefined) {
57 do_throw("completeTest" + nextTest + " undefined!");
60 run_test_number(nextTest);
63 function run_test_number(num) {
64 let testPath = testPathBase + num;
65 httpserver.registerPathHandler(testPath, globalThis["handler" + num]);
67 var channel = setupChannel(testPath);
68 let flags = test_flags[num]; // OK if flags undefined for test
70 new ChannelListener(globalThis["completeTest" + num], channel, flags)
74 function setupChannel(url) {
75 var chan = NetUtil.newChannel({
77 loadUsingSystemPrincipal: true,
79 var httpChan = chan.QueryInterface(Ci.nsIHttpChannel);
84 httpserver.stop(do_test_finished);
87 ////////////////////////////////////////////////////////////////////////////////
88 // Test 1: test Content-Disposition channel attributes
89 // eslint-disable-next-line no-unused-vars
90 function handler1(metadata, response) {
91 response.setStatusLine(metadata.httpVersion, 200, "OK");
92 response.setHeader("Content-Disposition", "attachment; filename=foo");
93 response.setHeader("Content-Type", "text/plain", false);
96 // eslint-disable-next-line no-unused-vars
97 function completeTest1(request, data, ctx) {
99 var chan = request.QueryInterface(Ci.nsIChannel);
100 Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
101 Assert.equal(chan.contentDispositionFilename, "foo");
102 Assert.equal(chan.contentDispositionHeader, "attachment; filename=foo");
104 do_throw("error parsing Content-Disposition: " + ex);
109 ////////////////////////////////////////////////////////////////////////////////
110 // Test 2: no filename
111 // eslint-disable-next-line no-unused-vars
112 function handler2(metadata, response) {
113 response.setStatusLine(metadata.httpVersion, 200, "OK");
114 response.setHeader("Content-Type", "text/plain", false);
115 response.setHeader("Content-Disposition", "attachment");
117 response.bodyOutputStream.write(body, body.length);
120 // eslint-disable-next-line no-unused-vars
121 function completeTest2(request, data, ctx) {
123 var chan = request.QueryInterface(Ci.nsIChannel);
124 Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
125 Assert.equal(chan.contentDispositionHeader, "attachment");
126 chan.contentDispositionFilename; // should barf
127 do_throw("Should have failed getting Content-Disposition filename");
129 info("correctly ate exception");
134 ////////////////////////////////////////////////////////////////////////////////
135 // Test 3: filename missing
136 // eslint-disable-next-line no-unused-vars
137 function handler3(metadata, response) {
138 response.setStatusLine(metadata.httpVersion, 200, "OK");
139 response.setHeader("Content-Type", "text/plain", false);
140 response.setHeader("Content-Disposition", "attachment; filename=");
142 response.bodyOutputStream.write(body, body.length);
145 // eslint-disable-next-line no-unused-vars
146 function completeTest3(request, data, ctx) {
148 var chan = request.QueryInterface(Ci.nsIChannel);
149 Assert.equal(chan.contentDisposition, chan.DISPOSITION_ATTACHMENT);
150 Assert.equal(chan.contentDispositionHeader, "attachment; filename=");
151 chan.contentDispositionFilename; // should barf
153 do_throw("Should have failed getting Content-Disposition filename");
155 info("correctly ate exception");
160 ////////////////////////////////////////////////////////////////////////////////
162 // eslint-disable-next-line no-unused-vars
163 function handler4(metadata, response) {
164 response.setStatusLine(metadata.httpVersion, 200, "OK");
165 response.setHeader("Content-Type", "text/plain", false);
166 response.setHeader("Content-Disposition", "inline");
168 response.bodyOutputStream.write(body, body.length);
171 // eslint-disable-next-line no-unused-vars
172 function completeTest4(request, data, ctx) {
174 var chan = request.QueryInterface(Ci.nsIChannel);
175 Assert.equal(chan.contentDisposition, chan.DISPOSITION_INLINE);
176 Assert.equal(chan.contentDispositionHeader, "inline");
178 chan.contentDispositionFilename; // should barf
179 do_throw("Should have failed getting Content-Disposition filename");
181 info("correctly ate exception");