test_original_sent_received_head.js (6850B)
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. 10 // 11 // Original headers will be stored in the cache as well. This test checks 12 // that too. 13 14 // Note: sets Cc and Ci variables 15 16 "use strict"; 17 18 const { HttpServer } = ChromeUtils.importESModule( 19 "resource://testing-common/httpd.sys.mjs" 20 ); 21 22 ChromeUtils.defineLazyGetter(this, "URL", function () { 23 return "http://localhost:" + httpserver.identity.primaryPort; 24 }); 25 26 var httpserver = new HttpServer(); 27 var testpath = "/simple"; 28 var httpbody = "0123456789"; 29 30 var dbg = 1; 31 32 function run_test() { 33 if (dbg) { 34 print("============== START =========="); 35 } 36 37 httpserver.registerPathHandler(testpath, serverHandler); 38 httpserver.start(-1); 39 run_next_test(); 40 } 41 42 add_test(function test_headerChange() { 43 if (dbg) { 44 print("============== test_headerChange setup: in"); 45 } 46 47 var channel1 = setupChannel(testpath); 48 channel1.loadFlags = Ci.nsIRequest.LOAD_BYPASS_CACHE; 49 50 // ChannelListener defined in head_channels.js 51 channel1.asyncOpen(new ChannelListener(checkResponse, null)); 52 53 if (dbg) { 54 print("============== test_headerChange setup: out"); 55 } 56 }); 57 58 add_test(function test_fromCache() { 59 if (dbg) { 60 print("============== test_fromCache setup: in"); 61 } 62 63 var channel2 = setupChannel(testpath); 64 channel2.loadFlags = Ci.nsIRequest.LOAD_FROM_CACHE; 65 66 // ChannelListener defined in head_channels.js 67 channel2.asyncOpen(new ChannelListener(checkResponse, null)); 68 69 if (dbg) { 70 print("============== test_fromCache setup: out"); 71 } 72 }); 73 74 add_test(function finish() { 75 if (dbg) { 76 print("============== STOP =========="); 77 } 78 httpserver.stop(do_test_finished); 79 }); 80 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; 88 } 89 90 function serverHandler(metadata, response) { 91 if (dbg) { 92 print("============== serverHandler: in"); 93 } 94 95 let etag; 96 try { 97 etag = metadata.getHeader("If-None-Match"); 98 } catch (ex) { 99 etag = ""; 100 } 101 if (etag == "testtag") { 102 if (dbg) { 103 print("============== 304 answerr: in"); 104 } 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"); 109 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); 119 } 120 if (dbg) { 121 print("============== serverHandler: out"); 122 } 123 } 124 125 function checkResponse(request) { 126 if (dbg) { 127 print("============== checkResponse: in"); 128 } 129 130 request.QueryInterface(Ci.nsIHttpChannel); 131 Assert.equal(request.responseStatus, 200); 132 Assert.equal(request.responseStatusText, "OK"); 133 Assert.ok(request.requestSucceeded); 134 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"); 143 } 144 if (aName == "location") { 145 locationHeaderFound++; 146 Assert.equal(aValue, "loc"); 147 } 148 }, 149 }); 150 Assert.equal(linkHeaderFound, 1); 151 Assert.equal(locationHeaderFound, 1); 152 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"); 165 } 166 linkOrgHeaderFound++; 167 } 168 if (aName == "location") { 169 locationOrgHeaderFound++; 170 Assert.equal(aValue, "loc"); 171 } 172 }, 173 }); 174 Assert.equal(linkOrgHeaderFound, 3); 175 Assert.equal(locationOrgHeaderFound, 1); 176 177 if (dbg) { 178 print("============== Remove headers"); 179 } 180 // Remove header. 181 request.setResponseHeader("Link", "", false); 182 request.setResponseHeader("Location", "", false); 183 184 var linkHeaderFound2 = false; 185 var locationHeaderFound2 = 0; 186 request.visitResponseHeaders({ 187 visitHeader: function visit(aName) { 188 if (aName == "Link") { 189 linkHeaderFound2 = true; 190 } 191 if (aName == "Location") { 192 locationHeaderFound2 = true; 193 } 194 }, 195 }); 196 Assert.ok(!linkHeaderFound2, "There should be no link header"); 197 Assert.ok(!locationHeaderFound2, "There should be no location headers."); 198 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"); 211 } 212 linkOrgHeaderFound2++; 213 } 214 if (aName == "location") { 215 locationOrgHeaderFound2++; 216 Assert.equal(aValue, "loc"); 217 } 218 }, 219 }); 220 Assert.equal(linkOrgHeaderFound2, 3, "Original link header still here."); 221 Assert.equal( 222 locationOrgHeaderFound2, 223 1, 224 "Original location header still here." 225 ); 226 227 if (dbg) { 228 print("============== Test GetResponseHeader"); 229 } 230 var linkOrgHeaderFound3 = 0; 231 request.getOriginalResponseHeader("link", { 232 visitHeader: function visitOrg(aName, aValue) { 233 if (linkOrgHeaderFound3 == 0) { 234 Assert.equal(aValue, ""); 235 } else if (linkOrgHeaderFound3 == 1) { 236 Assert.equal(aValue, "value1"); 237 } else { 238 Assert.equal(aValue, "value2"); 239 } 240 linkOrgHeaderFound3++; 241 }, 242 }); 243 Assert.equal(linkOrgHeaderFound2, 3, "Original link header still here."); 244 245 if (dbg) { 246 print("============== checkResponse: out"); 247 } 248 249 run_next_test(); 250 }