5 // Note: sets Cc and Ci variables
9 const { HttpServer } = ChromeUtils.importESModule(
10 "resource://testing-common/httpd.sys.mjs"
12 const ReferrerInfo = Components.Constructor(
13 "@mozilla.org/referrer-info;1",
18 ChromeUtils.defineLazyGetter(this, "URL", function () {
19 return "http://localhost:" + httpserver.identity.primaryPort;
22 var httpserver = new HttpServer();
23 var testpath = "/simple";
24 var httpbody = "0123456789";
29 print("============== START ==========");
37 function setup_test() {
39 print("============== setup_test: in");
42 httpserver.registerPathHandler(testpath, serverHandler);
45 channel = setupChannel(testpath);
47 channel.setRequestHeader("ReplaceMe", "initial value", true);
48 var setOK = channel.getRequestHeader("ReplaceMe");
49 Assert.equal(setOK, "initial value");
50 channel.setRequestHeader("ReplaceMe", "replaced", false);
51 setOK = channel.getRequestHeader("ReplaceMe");
52 Assert.equal(setOK, "replaced");
54 channel.setRequestHeader("MergeMe", "foo1", true);
55 channel.setRequestHeader("MergeMe", "foo2", true);
56 channel.setRequestHeader("MergeMe", "foo3", true);
57 setOK = channel.getRequestHeader("MergeMe");
58 Assert.equal(setOK, "foo1, foo2, foo3");
60 channel.setEmptyRequestHeader("Empty");
61 setOK = channel.getRequestHeader("Empty");
62 Assert.equal(setOK, "");
64 channel.setRequestHeader("ReplaceWithEmpty", "initial value", true);
65 setOK = channel.getRequestHeader("ReplaceWithEmpty");
66 Assert.equal(setOK, "initial value");
67 channel.setEmptyRequestHeader("ReplaceWithEmpty");
68 setOK = channel.getRequestHeader("ReplaceWithEmpty");
69 Assert.equal(setOK, "");
71 channel.setEmptyRequestHeader("MergeWithEmpty");
72 setOK = channel.getRequestHeader("MergeWithEmpty");
73 Assert.equal(setOK, "");
74 channel.setRequestHeader("MergeWithEmpty", "foo", true);
75 setOK = channel.getRequestHeader("MergeWithEmpty");
76 Assert.equal(setOK, "foo");
78 var uri = NetUtil.newURI("http://foo1.invalid:80");
79 channel.referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);
80 setOK = channel.getRequestHeader("Referer");
81 Assert.equal(setOK, "http://foo1.invalid/");
83 uri = NetUtil.newURI("http://foo2.invalid:90/bar");
84 channel.referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);
85 setOK = channel.getRequestHeader("Referer");
86 // No triggering URI inloadInfo, assume load is cross-origin.
87 Assert.equal(setOK, "http://foo2.invalid:90/");
89 // ChannelListener defined in head_channels.js
90 channel.asyncOpen(new ChannelListener(checkRequestResponse, channel));
93 print("============== setup_test: out");
97 function setupChannel(path) {
98 var chan = NetUtil.newChannel({
100 loadUsingSystemPrincipal: true,
102 chan.QueryInterface(Ci.nsIHttpChannel);
103 chan.requestMethod = "GET";
107 function serverHandler(metadata, response) {
109 print("============== serverHandler: in");
112 var setOK = metadata.getHeader("ReplaceMe");
113 Assert.equal(setOK, "replaced");
114 setOK = metadata.getHeader("MergeMe");
115 Assert.equal(setOK, "foo1, foo2, foo3");
116 setOK = metadata.getHeader("Empty");
117 Assert.equal(setOK, "");
118 setOK = metadata.getHeader("ReplaceWithEmpty");
119 Assert.equal(setOK, "");
120 setOK = metadata.getHeader("MergeWithEmpty");
121 Assert.equal(setOK, "foo");
122 setOK = metadata.getHeader("Referer");
123 Assert.equal(setOK, "http://foo2.invalid:90/");
125 response.setHeader("Content-Type", "text/plain", false);
126 response.setStatusLine("1.1", 200, "OK");
128 // note: httpd.js' "Response" class uses ',' (no space) for merge.
129 response.setHeader("httpdMerge", "bar1", false);
130 response.setHeader("httpdMerge", "bar2", true);
131 response.setHeader("httpdMerge", "bar3", true);
132 // Some special headers like Proxy-Authenticate merge with \n
133 response.setHeader("Proxy-Authenticate", "line 1", true);
134 response.setHeader("Proxy-Authenticate", "line 2", true);
135 response.setHeader("Proxy-Authenticate", "line 3", true);
137 response.bodyOutputStream.write(httpbody, httpbody.length);
140 print("============== serverHandler: out");
144 function checkRequestResponse(request, data) {
146 print("============== checkRequestResponse: in");
149 Assert.equal(channel.responseStatus, 200);
150 Assert.equal(channel.responseStatusText, "OK");
151 Assert.ok(channel.requestSucceeded);
153 var response = channel.getResponseHeader("httpdMerge");
154 Assert.equal(response, "bar1,bar2,bar3");
155 channel.setResponseHeader("httpdMerge", "bar", true);
156 Assert.equal(channel.getResponseHeader("httpdMerge"), "bar1,bar2,bar3, bar");
158 response = channel.getResponseHeader("Proxy-Authenticate");
159 Assert.equal(response, "line 1\nline 2\nline 3");
161 channel.contentCharset = "UTF-8";
162 Assert.equal(channel.contentCharset, "UTF-8");
163 Assert.equal(channel.contentType, "text/plain");
164 Assert.equal(channel.contentLength, httpbody.length);
165 Assert.equal(data, httpbody);
167 httpserver.stop(do_test_finished);
169 print("============== checkRequestResponse: out");