|
1 /* Any copyright is dedicated to the Public Domain. |
|
2 http://creativecommons.org/publicdomain/zero/1.0/ */ |
|
3 |
|
4 Components.utils.import("resource://gre/modules/Http.jsm"); |
|
5 Components.utils.import("resource://testing-common/httpd.js"); |
|
6 |
|
7 const BinaryInputStream = Components.Constructor("@mozilla.org/binaryinputstream;1", |
|
8 "nsIBinaryInputStream", "setInputStream"); |
|
9 |
|
10 var server; |
|
11 |
|
12 const kDefaultServerPort = 9000; |
|
13 const kSuccessPath = "/success"; |
|
14 const kBaseUrl = "http://localhost:" + kDefaultServerPort; |
|
15 const kSuccessUrl = kBaseUrl + kSuccessPath; |
|
16 |
|
17 const kPostPath = "/post"; |
|
18 const kPostUrl = kBaseUrl + kPostPath; |
|
19 const kPostDataSent = [["foo", "bar"], ["complex", "!*()@"]]; |
|
20 const kPostDataReceived = "foo=bar&complex=%21%2A%28%29%40"; |
|
21 |
|
22 const kPutPath = "/put"; |
|
23 const kPutUrl = kBaseUrl + kPutPath; |
|
24 const kPutDataSent = [["P", "NP"]]; |
|
25 const kPutDataReceived = "P=NP"; |
|
26 |
|
27 const kGetPath = "/get"; |
|
28 const kGetUrl = kBaseUrl + kGetPath; |
|
29 |
|
30 function successResult(aRequest, aResponse) { |
|
31 aResponse.setStatusLine(null, 200, "OK"); |
|
32 aResponse.setHeader("Content-Type", "application/json"); |
|
33 aResponse.write("Success!"); |
|
34 } |
|
35 |
|
36 function getDataChecker(aExpectedMethod, aExpectedData) { |
|
37 return function(aRequest, aResponse) { |
|
38 let body = new BinaryInputStream(aRequest.bodyInputStream); |
|
39 let bytes = []; |
|
40 let avail; |
|
41 while ((avail = body.available()) > 0) |
|
42 Array.prototype.push.apply(bytes, body.readByteArray(avail)); |
|
43 |
|
44 do_check_eq(aRequest.method, aExpectedMethod); |
|
45 |
|
46 var data = String.fromCharCode.apply(null, bytes); |
|
47 |
|
48 do_check_eq(data, aExpectedData); |
|
49 |
|
50 aResponse.setStatusLine(null, 200, "OK"); |
|
51 aResponse.setHeader("Content-Type", "application/json"); |
|
52 aResponse.write("Success!"); |
|
53 } |
|
54 } |
|
55 |
|
56 add_test(function test_successCallback() { |
|
57 do_test_pending(); |
|
58 let options = { |
|
59 onLoad: function(aResponse) { |
|
60 do_check_eq(aResponse, "Success!"); |
|
61 do_test_finished(); |
|
62 run_next_test(); |
|
63 }, |
|
64 onError: function(e) { |
|
65 do_check_true(false); |
|
66 do_test_finished(); |
|
67 run_next_test(); |
|
68 } |
|
69 } |
|
70 httpRequest(kSuccessUrl, options); |
|
71 }); |
|
72 |
|
73 add_test(function test_errorCallback() { |
|
74 do_test_pending(); |
|
75 let options = { |
|
76 onSuccess: function(aResponse) { |
|
77 do_check_true(false); |
|
78 do_test_finished(); |
|
79 run_next_test(); |
|
80 }, |
|
81 onError: function(e, aResponse) { |
|
82 do_check_eq(e, "404 - Not Found"); |
|
83 do_test_finished(); |
|
84 run_next_test(); |
|
85 } |
|
86 } |
|
87 httpRequest(kBaseUrl + "/failure", options); |
|
88 }); |
|
89 |
|
90 add_test(function test_PostData() { |
|
91 do_test_pending(); |
|
92 let options = { |
|
93 onLoad: function(aResponse) { |
|
94 do_check_eq(aResponse, "Success!"); |
|
95 do_test_finished(); |
|
96 run_next_test(); |
|
97 }, |
|
98 onError: function(e) { |
|
99 do_check_true(false); |
|
100 do_test_finished(); |
|
101 run_next_test(); |
|
102 }, |
|
103 postData: kPostDataSent |
|
104 } |
|
105 httpRequest(kPostUrl, options); |
|
106 }); |
|
107 |
|
108 add_test(function test_PutData() { |
|
109 do_test_pending(); |
|
110 let options = { |
|
111 method: "PUT", |
|
112 onLoad: function(aResponse) { |
|
113 do_check_eq(aResponse, "Success!"); |
|
114 do_test_finished(); |
|
115 run_next_test(); |
|
116 }, |
|
117 onError: function(e) { |
|
118 do_check_true(false); |
|
119 do_test_finished(); |
|
120 run_next_test(); |
|
121 }, |
|
122 postData: kPutDataSent |
|
123 } |
|
124 httpRequest(kPutUrl, options); |
|
125 }); |
|
126 |
|
127 add_test(function test_GetData() { |
|
128 do_test_pending(); |
|
129 let options = { |
|
130 onLoad: function(aResponse) { |
|
131 do_check_eq(aResponse, "Success!"); |
|
132 do_test_finished(); |
|
133 run_next_test(); |
|
134 }, |
|
135 onError: function(e) { |
|
136 do_check_true(false); |
|
137 do_test_finished(); |
|
138 run_next_test(); |
|
139 }, |
|
140 postData: null |
|
141 } |
|
142 httpRequest(kGetUrl, options); |
|
143 }); |
|
144 |
|
145 add_test(function test_OptionalParameters() { |
|
146 let options = { |
|
147 onLoad: null, |
|
148 onError: null, |
|
149 logger: null |
|
150 }; |
|
151 // Just make sure that nothing throws when doing this (i.e. httpRequest |
|
152 // doesn't try to access null options). |
|
153 httpRequest(kGetUrl, options); |
|
154 run_next_test(); |
|
155 }); |
|
156 |
|
157 function run_test() { |
|
158 // Set up a mock HTTP server to serve a success page. |
|
159 server = new HttpServer(); |
|
160 server.registerPathHandler(kSuccessPath, successResult); |
|
161 server.registerPathHandler(kPostPath, |
|
162 getDataChecker("POST", kPostDataReceived)); |
|
163 server.registerPathHandler(kPutPath, |
|
164 getDataChecker("PUT", kPutDataReceived)); |
|
165 server.registerPathHandler(kGetPath, getDataChecker("GET", "")); |
|
166 server.start(kDefaultServerPort); |
|
167 |
|
168 run_next_test(); |
|
169 |
|
170 // Teardown. |
|
171 do_register_cleanup(function() { |
|
172 server.stop(function() { }); |
|
173 }); |
|
174 } |
|
175 |