|
1 |
|
2 var cu = Components.utils; |
|
3 cu.import("resource://testing-common/httpd.js"); |
|
4 |
|
5 var httpserver = new HttpServer(); |
|
6 var httpserver2 = new HttpServer(); |
|
7 var testpath = "/simple"; |
|
8 var negativetestpath = "/negative"; |
|
9 var httpbody = "<?xml version='1.0' ?><root>0123456789</root>"; |
|
10 |
|
11 var sb = cu.Sandbox(["http://www.example.com", |
|
12 "http://localhost:4444/simple"], |
|
13 { wantGlobalProperties: ["XMLHttpRequest"] }); |
|
14 |
|
15 function createXHR(loc, async) |
|
16 { |
|
17 var xhr = new XMLHttpRequest(); |
|
18 xhr.open("GET", "http://localhost:" + loc, async); |
|
19 return xhr; |
|
20 } |
|
21 |
|
22 function checkResults(xhr) |
|
23 { |
|
24 if (xhr.readyState != 4) |
|
25 return false; |
|
26 |
|
27 do_check_eq(xhr.status, 200); |
|
28 do_check_eq(xhr.responseText, httpbody); |
|
29 |
|
30 var root_node = xhr.responseXML.getElementsByTagName('root').item(0); |
|
31 do_check_eq(root_node.firstChild.data, "0123456789"); |
|
32 return true; |
|
33 } |
|
34 |
|
35 var httpServersClosed = 0; |
|
36 function finishIfDone() |
|
37 { |
|
38 if (++httpServersClosed == 2) |
|
39 do_test_finished(); |
|
40 } |
|
41 |
|
42 function run_test() |
|
43 { |
|
44 do_test_pending(); |
|
45 |
|
46 httpserver.registerPathHandler(testpath, serverHandler); |
|
47 httpserver.start(4444); |
|
48 |
|
49 httpserver2.registerPathHandler(negativetestpath, serverHandler); |
|
50 httpserver2.start(4445); |
|
51 |
|
52 // Test sync XHR sending |
|
53 cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); |
|
54 var res = cu.evalInSandbox('var sync = createXHR("4444/simple"); sync.send(null); sync', sb); |
|
55 do_check_true(checkResults(res)); |
|
56 |
|
57 // negative test sync XHR sending (to ensure that the xhr do not have chrome caps, see bug 779821) |
|
58 try { |
|
59 cu.evalInSandbox('var createXHR = ' + createXHR.toString(), sb); |
|
60 var res = cu.evalInSandbox('var sync = createXHR("4445/negative"); sync.send(null); sync', sb); |
|
61 do_check_false(true, "XHR created from sandbox should not have chrome caps"); |
|
62 } catch (e) { |
|
63 do_check_true(true); |
|
64 } |
|
65 |
|
66 httpserver2.stop(finishIfDone); |
|
67 |
|
68 // Test async XHR sending |
|
69 sb.finish = function(){ |
|
70 httpserver.stop(finishIfDone); |
|
71 } |
|
72 |
|
73 // We want to execute checkResults from the scope of the sandbox as well to |
|
74 // make sure that there are no permission errors related to nsEP. For that |
|
75 // we need to clone the function into the sandbox and make a few things |
|
76 // available for it. |
|
77 cu.evalInSandbox('var checkResults = ' + checkResults.toSource(), sb); |
|
78 sb.do_check_eq = do_check_eq; |
|
79 sb.httpbody = httpbody; |
|
80 |
|
81 function changeListener(event) { |
|
82 if (checkResults(async)) |
|
83 finish(); |
|
84 } |
|
85 |
|
86 var async = cu.evalInSandbox('var async = createXHR("4444/simple", true);' + |
|
87 'async.addEventListener("readystatechange", ' + |
|
88 changeListener.toString() + ', false);' + |
|
89 'async', sb); |
|
90 async.send(null); |
|
91 } |
|
92 |
|
93 function serverHandler(metadata, response) |
|
94 { |
|
95 response.setHeader("Content-Type", "text/xml", false); |
|
96 response.bodyOutputStream.write(httpbody, httpbody.length); |
|
97 } |