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