5 // Note: sets Cc and Ci variables
9 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
10 const ReferrerInfo = Components.Constructor(
11 "@mozilla.org/referrer-info;1",
16 XPCOMUtils.defineLazyGetter(this, "URL", function() {
17 return "http://localhost:" + httpserver.identity.primaryPort;
20 var httpserver = new HttpServer();
21 var testpath = "/simple";
22 var httpbody = "0123456789";
27 print("============== START ==========");
35 function setup_test() {
37 print("============== setup_test: in");
40 httpserver.registerPathHandler(testpath, serverHandler);
43 channel = setupChannel(testpath);
45 channel.setRequestHeader("ReplaceMe", "initial value", true);
46 var setOK = channel.getRequestHeader("ReplaceMe");
47 Assert.equal(setOK, "initial value");
48 channel.setRequestHeader("ReplaceMe", "replaced", false);
49 setOK = channel.getRequestHeader("ReplaceMe");
50 Assert.equal(setOK, "replaced");
52 channel.setRequestHeader("MergeMe", "foo1", true);
53 channel.setRequestHeader("MergeMe", "foo2", true);
54 channel.setRequestHeader("MergeMe", "foo3", true);
55 setOK = channel.getRequestHeader("MergeMe");
56 Assert.equal(setOK, "foo1, foo2, foo3");
58 channel.setEmptyRequestHeader("Empty");
59 setOK = channel.getRequestHeader("Empty");
60 Assert.equal(setOK, "");
62 channel.setRequestHeader("ReplaceWithEmpty", "initial value", true);
63 setOK = channel.getRequestHeader("ReplaceWithEmpty");
64 Assert.equal(setOK, "initial value");
65 channel.setEmptyRequestHeader("ReplaceWithEmpty");
66 setOK = channel.getRequestHeader("ReplaceWithEmpty");
67 Assert.equal(setOK, "");
69 channel.setEmptyRequestHeader("MergeWithEmpty");
70 setOK = channel.getRequestHeader("MergeWithEmpty");
71 Assert.equal(setOK, "");
72 channel.setRequestHeader("MergeWithEmpty", "foo", true);
73 setOK = channel.getRequestHeader("MergeWithEmpty");
74 Assert.equal(setOK, "foo");
76 var uri = NetUtil.newURI("http://foo1.invalid:80");
77 channel.referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);
78 setOK = channel.getRequestHeader("Referer");
79 Assert.equal(setOK, "http://foo1.invalid/");
81 uri = NetUtil.newURI("http://foo2.invalid:90/bar");
82 channel.referrerInfo = new ReferrerInfo(Ci.nsIReferrerInfo.EMPTY, true, uri);
83 setOK = channel.getRequestHeader("Referer");
84 // No triggering URI inloadInfo, assume load is cross-origin.
85 Assert.equal(setOK, "http://foo2.invalid:90/");
87 // ChannelListener defined in head_channels.js
88 channel.asyncOpen(new ChannelListener(checkRequestResponse, channel));
91 print("============== setup_test: out");
95 function setupChannel(path) {
96 var chan = NetUtil.newChannel({
98 loadUsingSystemPrincipal: true,
100 chan.QueryInterface(Ci.nsIHttpChannel);
101 chan.requestMethod = "GET";
105 function serverHandler(metadata, response) {
107 print("============== serverHandler: in");
110 var setOK = metadata.getHeader("ReplaceMe");
111 Assert.equal(setOK, "replaced");
112 setOK = metadata.getHeader("MergeMe");
113 Assert.equal(setOK, "foo1, foo2, foo3");
114 setOK = metadata.getHeader("Empty");
115 Assert.equal(setOK, "");
116 setOK = metadata.getHeader("ReplaceWithEmpty");
117 Assert.equal(setOK, "");
118 setOK = metadata.getHeader("MergeWithEmpty");
119 Assert.equal(setOK, "foo");
120 setOK = metadata.getHeader("Referer");
121 Assert.equal(setOK, "http://foo2.invalid:90/");
123 response.setHeader("Content-Type", "text/plain", false);
124 response.setStatusLine("1.1", 200, "OK");
126 // note: httpd.js' "Response" class uses ',' (no space) for merge.
127 response.setHeader("httpdMerge", "bar1", false);
128 response.setHeader("httpdMerge", "bar2", true);
129 response.setHeader("httpdMerge", "bar3", true);
130 // Some special headers like Proxy-Authenticate merge with \n
131 response.setHeader("Proxy-Authenticate", "line 1", true);
132 response.setHeader("Proxy-Authenticate", "line 2", true);
133 response.setHeader("Proxy-Authenticate", "line 3", true);
135 response.bodyOutputStream.write(httpbody, httpbody.length);
138 print("============== serverHandler: out");
142 function checkRequestResponse(request, data, context) {
144 print("============== checkRequestResponse: in");
147 Assert.equal(channel.responseStatus, 200);
148 Assert.equal(channel.responseStatusText, "OK");
149 Assert.ok(channel.requestSucceeded);
151 var response = channel.getResponseHeader("httpdMerge");
152 Assert.equal(response, "bar1,bar2,bar3");
153 channel.setResponseHeader("httpdMerge", "bar", true);
154 Assert.equal(channel.getResponseHeader("httpdMerge"), "bar1,bar2,bar3, bar");
156 response = channel.getResponseHeader("Proxy-Authenticate");
157 Assert.equal(response, "line 1\nline 2\nline 3");
159 channel.contentCharset = "UTF-8";
160 Assert.equal(channel.contentCharset, "UTF-8");
161 Assert.equal(channel.contentType, "text/plain");
162 Assert.equal(channel.contentLength, httpbody.length);
163 Assert.equal(data, httpbody);
165 httpserver.stop(do_test_finished);
167 print("============== checkRequestResponse: out");