test_resource_header.js (1777B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 const { Resource } = ChromeUtils.importESModule( 7 "resource://services-sync/resource.sys.mjs" 8 ); 9 10 var httpServer = new HttpServer(); 11 httpServer.registerPathHandler("/content", contentHandler); 12 httpServer.start(-1); 13 14 const HTTP_PORT = httpServer.identity.primaryPort; 15 const TEST_URL = "http://localhost:" + HTTP_PORT + "/content"; 16 const BODY = "response body"; 17 18 // Keep headers for later inspection. 19 var auth = null; 20 var foo = null; 21 function contentHandler(metadata, response) { 22 _("Handling request."); 23 auth = metadata.getHeader("Authorization"); 24 foo = metadata.getHeader("X-Foo"); 25 26 _("Extracted headers. " + auth + ", " + foo); 27 28 response.setHeader("Content-Type", "text/plain"); 29 response.bodyOutputStream.write(BODY, BODY.length); 30 } 31 32 // Set a proxy function to cause an internal redirect. 33 function triggerRedirect() { 34 const PROXY_FUNCTION = 35 "function FindProxyForURL(url, host) {" + 36 " return 'PROXY a_non_existent_domain_x7x6c572v:80; " + 37 "PROXY localhost:" + 38 HTTP_PORT + 39 "';" + 40 "}"; 41 42 let prefs = Services.prefs.getBranch("network.proxy."); 43 prefs.setIntPref("type", 2); 44 prefs.setStringPref("autoconfig_url", "data:text/plain," + PROXY_FUNCTION); 45 } 46 47 add_task(async function test_headers_copied() { 48 triggerRedirect(); 49 50 _("Issuing request."); 51 let resource = new Resource(TEST_URL); 52 resource.setHeader("Authorization", "Basic foobar"); 53 resource.setHeader("X-Foo", "foofoo"); 54 55 let result = await resource.get(TEST_URL); 56 _("Result: " + result.data); 57 58 Assert.equal(result.data, BODY); 59 Assert.equal(auth, "Basic foobar"); 60 Assert.equal(foo, "foofoo"); 61 62 await promiseStopServer(httpServer); 63 });