1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 const { HttpServer } = ChromeUtils.importESModule(
8 "resource://testing-common/httpd.sys.mjs"
11 function makeChan(url) {
12 return NetUtil.newChannel({
14 loadUsingSystemPrincipal: true,
15 }).QueryInterface(Ci.nsIHttpChannel);
19 function request_handler1(metadata, response) {
20 response.seizePower();
21 response.write("HTTP/1.1 200 OK\r\n");
22 response.write("Content-Type: text/plain\r\n");
23 response.write("X-header-first: FIRSTVALUE\r\n");
24 response.write("X-header-second: 1; second\r\n");
25 response.write(`Content-Length: ${body.length}\r\n`);
26 response.write("\r\n");
31 // This handler is for obs-fold
32 // The line that contains X-header-second starts with a space. As a consequence
33 // it gets folded into the previous line.
34 function request_handler2(metadata, response) {
35 response.seizePower();
36 response.write("HTTP/1.1 200 OK\r\n");
37 response.write("Content-Type: text/plain\r\n");
38 response.write("X-header-first: FIRSTVALUE\r\n");
39 // Note the space at the begining of the line
40 response.write(" X-header-second: 1; second\r\n");
41 response.write(`Content-Length: ${body.length}\r\n`);
42 response.write("\r\n");
47 add_task(async function test() {
48 let http_server = new HttpServer();
49 http_server.registerPathHandler("/test1", request_handler1);
50 http_server.registerPathHandler("/test2", request_handler2);
51 http_server.start(-1);
52 const port = http_server.identity.primaryPort;
54 let chan1 = makeChan(`http://localhost:${port}/test1`);
55 await new Promise(resolve => {
56 chan1.asyncOpen(new ChannelListener(resolve));
58 equal(chan1.getResponseHeader("X-header-first"), "FIRSTVALUE");
59 equal(chan1.getResponseHeader("X-header-second"), "1; second");
61 let chan2 = makeChan(`http://localhost:${port}/test2`);
62 await new Promise(resolve => {
63 chan2.asyncOpen(new ChannelListener(resolve));
66 chan2.getResponseHeader("X-header-first"),
67 "FIRSTVALUE X-header-second: 1; second"
70 () => chan2.getResponseHeader("X-header-second"),
71 /NS_ERROR_NOT_AVAILABLE/
74 await new Promise(resolve => http_server.stop(resolve));