Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_original_sent_received_head.js
blobfa06234386f3908329bc0eff66d87cd5b303b2f4
1 //
2 // HTTP headers test
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
5 // thrown), etc.
6 //
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
12 // that too.
14 // Note: sets Cc and Ci variables
16 "use strict";
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;
24 });
26 var httpserver = new HttpServer();
27 var testpath = "/simple";
28 var httpbody = "0123456789";
30 var dbg = 1;
32 function run_test() {
33 if (dbg) {
34 print("============== START ==========");
37 httpserver.registerPathHandler(testpath, serverHandler);
38 httpserver.start(-1);
39 run_next_test();
42 add_test(function test_headerChange() {
43 if (dbg) {
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));
53 if (dbg) {
54 print("============== test_headerChange setup: out");
56 });
58 add_test(function test_fromCache() {
59 if (dbg) {
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));
69 if (dbg) {
70 print("============== test_fromCache setup: out");
72 });
74 add_test(function finish() {
75 if (dbg) {
76 print("============== STOP ==========");
78 httpserver.stop(do_test_finished);
79 });
81 function setupChannel(path) {
82 var chan = NetUtil.newChannel({
83 uri: URL + path,
84 loadUsingSystemPrincipal: true,
85 }).QueryInterface(Ci.nsIHttpChannel);
86 chan.requestMethod = "GET";
87 return chan;
90 function serverHandler(metadata, response) {
91 if (dbg) {
92 print("============== serverHandler: in");
95 let etag;
96 try {
97 etag = metadata.getHeader("If-None-Match");
98 } catch (ex) {
99 etag = "";
101 if (etag == "testtag") {
102 if (dbg) {
103 print("============== 304 answerr: in");
105 response.setStatusLine("1.1", 304, "Not Modified");
106 } else {
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);
120 if (dbg) {
121 print("============== serverHandler: out");
125 function checkResponse(request) {
126 if (dbg) {
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") {
141 linkHeaderFound++;
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");
163 } else {
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);
177 if (dbg) {
178 print("============== Remove headers");
180 // Remove header.
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");
209 } else {
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.");
221 Assert.ok(
222 locationOrgHeaderFound2 == 1,
223 "Original location header still here."
226 if (dbg) {
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");
236 } else {
237 Assert.equal(aValue, "value2");
239 linkOrgHeaderFound3++;
242 Assert.ok(linkOrgHeaderFound2 == 3, "Original link header still here.");
244 if (dbg) {
245 print("============== checkResponse: out");
248 run_next_test();