3 // Response headers can be changed after they have been received, e.g. empty
4 // headers are deleted, some duplicate header are merged (if no error is
7 // The "original header" is introduced to hold the header array in the order
8 // and the form as they have been received from the network.
9 // Here, the "original headers" are tested.
11 // Original headers will be stored in the cache as well. This test checks
14 // Note: sets Cc and Ci variables
18 const { HttpServer
} = ChromeUtils
.importESModule(
19 "resource://testing-common/httpd.sys.mjs"
22 ChromeUtils
.defineLazyGetter(this, "URL", function () {
23 return "http://localhost:" + httpserver
.identity
.primaryPort
;
26 var httpserver
= new HttpServer();
27 var testpath
= "/simple";
28 var httpbody
= "0123456789";
34 print("============== START ==========");
37 httpserver
.registerPathHandler(testpath
, serverHandler
);
42 add_test(function test_headerChange() {
44 print("============== test_headerChange setup: in");
47 var channel1
= setupChannel(testpath
);
48 channel1
.loadFlags
= Ci
.nsIRequest
.LOAD_BYPASS_CACHE
;
50 // ChannelListener defined in head_channels.js
51 channel1
.asyncOpen(new ChannelListener(checkResponse
, null));
54 print("============== test_headerChange setup: out");
58 add_test(function test_fromCache() {
60 print("============== test_fromCache setup: in");
63 var channel2
= setupChannel(testpath
);
64 channel2
.loadFlags
= Ci
.nsIRequest
.LOAD_FROM_CACHE
;
66 // ChannelListener defined in head_channels.js
67 channel2
.asyncOpen(new ChannelListener(checkResponse
, null));
70 print("============== test_fromCache setup: out");
74 add_test(function finish() {
76 print("============== STOP ==========");
78 httpserver
.stop(do_test_finished
);
81 function setupChannel(path
) {
82 var chan
= NetUtil
.newChannel({
84 loadUsingSystemPrincipal
: true,
85 }).QueryInterface(Ci
.nsIHttpChannel
);
86 chan
.requestMethod
= "GET";
90 function serverHandler(metadata
, response
) {
92 print("============== serverHandler: in");
97 etag
= metadata
.getHeader("If-None-Match");
101 if (etag
== "testtag") {
103 print("============== 304 answerr: in");
105 response
.setStatusLine("1.1", 304, "Not Modified");
107 response
.setHeader("Content-Type", "text/plain", false);
108 response
.setStatusLine("1.1", 200, "OK");
110 // Set a empty header. A empty link header will not appear in header list,
111 // but in the "original headers", it will be still exactly as received.
112 response
.setHeaderNoCheck("Link", "", true);
113 response
.setHeaderNoCheck("Link", "value1");
114 response
.setHeaderNoCheck("Link", "value2");
115 response
.setHeaderNoCheck("Location", "loc");
116 response
.setHeader("Cache-Control", "max-age=10000", false);
117 response
.setHeader("ETag", "testtag", false);
118 response
.bodyOutputStream
.write(httpbody
, httpbody
.length
);
121 print("============== serverHandler: out");
125 function checkResponse(request
) {
127 print("============== checkResponse: in");
130 request
.QueryInterface(Ci
.nsIHttpChannel
);
131 Assert
.equal(request
.responseStatus
, 200);
132 Assert
.equal(request
.responseStatusText
, "OK");
133 Assert
.ok(request
.requestSucceeded
);
135 // Response header have only one link header.
136 var linkHeaderFound
= 0;
137 var locationHeaderFound
= 0;
138 request
.visitResponseHeaders({
139 visitHeader
: function visit(aName
, aValue
) {
140 if (aName
== "link") {
142 Assert
.equal(aValue
, "value1, value2");
144 if (aName
== "location") {
145 locationHeaderFound
++;
146 Assert
.equal(aValue
, "loc");
150 Assert
.equal(linkHeaderFound
, 1);
151 Assert
.equal(locationHeaderFound
, 1);
153 // The "original header" still contains 3 link headers.
154 var linkOrgHeaderFound
= 0;
155 var locationOrgHeaderFound
= 0;
156 request
.visitOriginalResponseHeaders({
157 visitHeader
: function visitOrg(aName
, aValue
) {
158 if (aName
== "link") {
159 if (linkOrgHeaderFound
== 0) {
160 Assert
.equal(aValue
, "");
161 } else if (linkOrgHeaderFound
== 1) {
162 Assert
.equal(aValue
, "value1");
164 Assert
.equal(aValue
, "value2");
166 linkOrgHeaderFound
++;
168 if (aName
== "location") {
169 locationOrgHeaderFound
++;
170 Assert
.equal(aValue
, "loc");
174 Assert
.equal(linkOrgHeaderFound
, 3);
175 Assert
.equal(locationOrgHeaderFound
, 1);
178 print("============== Remove headers");
181 request
.setResponseHeader("Link", "", false);
182 request
.setResponseHeader("Location", "", false);
184 var linkHeaderFound2
= false;
185 var locationHeaderFound2
= 0;
186 request
.visitResponseHeaders({
187 visitHeader
: function visit(aName
) {
188 if (aName
== "Link") {
189 linkHeaderFound2
= true;
191 if (aName
== "Location") {
192 locationHeaderFound2
= true;
196 Assert
.ok(!linkHeaderFound2
, "There should be no link header");
197 Assert
.ok(!locationHeaderFound2
, "There should be no location headers.");
199 // The "original header" still contains the empty header.
200 var linkOrgHeaderFound2
= 0;
201 var locationOrgHeaderFound2
= 0;
202 request
.visitOriginalResponseHeaders({
203 visitHeader
: function visitOrg(aName
, aValue
) {
204 if (aName
== "link") {
205 if (linkOrgHeaderFound2
== 0) {
206 Assert
.equal(aValue
, "");
207 } else if (linkOrgHeaderFound2
== 1) {
208 Assert
.equal(aValue
, "value1");
210 Assert
.equal(aValue
, "value2");
212 linkOrgHeaderFound2
++;
214 if (aName
== "location") {
215 locationOrgHeaderFound2
++;
216 Assert
.equal(aValue
, "loc");
220 Assert
.ok(linkOrgHeaderFound2
== 3, "Original link header still here.");
222 locationOrgHeaderFound2
== 1,
223 "Original location header still here."
227 print("============== Test GetResponseHeader");
229 var linkOrgHeaderFound3
= 0;
230 request
.getOriginalResponseHeader("link", {
231 visitHeader
: function visitOrg(aName
, aValue
) {
232 if (linkOrgHeaderFound3
== 0) {
233 Assert
.equal(aValue
, "");
234 } else if (linkOrgHeaderFound3
== 1) {
235 Assert
.equal(aValue
, "value1");
237 Assert
.equal(aValue
, "value2");
239 linkOrgHeaderFound3
++;
242 Assert
.ok(linkOrgHeaderFound2
== 3, "Original link header still here.");
245 print("============== checkResponse: out");