OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 */ | 7 */ |
8 | 8 |
9 (function() { | 9 (function() { |
10 | 10 |
11 'use strict'; | 11 'use strict'; |
12 | 12 |
| 13 /** @type {sinon.FakeXhrCtrl} */ |
| 14 var fakeXhrCtrl; |
| 15 |
13 /** @type {sinon.FakeXhr} */ | 16 /** @type {sinon.FakeXhr} */ |
14 var fakeXhr; | 17 var fakeXhr; |
15 | 18 |
16 QUnit.module('xhr', { | 19 QUnit.module('xhr', { |
17 beforeEach: function() { | 20 beforeEach: function() { |
18 fakeXhr = null; | 21 fakeXhr = null; |
19 sinon.useFakeXMLHttpRequest().onCreate = | 22 fakeXhrCtrl = sinon.useFakeXMLHttpRequest(); |
| 23 fakeXhrCtrl.onCreate = |
20 function(/** sinon.FakeXhr */ xhr) { | 24 function(/** sinon.FakeXhr */ xhr) { |
21 fakeXhr = xhr; | 25 fakeXhr = xhr; |
22 }; | 26 }; |
| 27 remoting.identity = new remoting.Identity(); |
| 28 chromeMocks.activate(['identity']); |
| 29 chromeMocks.identity.mock$setToken('my_token'); |
| 30 }, |
| 31 afterEach: function() { |
| 32 chromeMocks.restore(); |
| 33 remoting.identity = null; |
23 } | 34 } |
24 }); | 35 }); |
25 | 36 |
26 QUnit.test('urlencodeParamHash', function(assert) { | 37 QUnit.test('urlencodeParamHash', function(assert) { |
27 assert.equal( | 38 assert.equal( |
28 remoting.Xhr.urlencodeParamHash({}), | 39 remoting.Xhr.urlencodeParamHash({}), |
29 ''); | 40 ''); |
30 assert.equal( | 41 assert.equal( |
31 remoting.Xhr.urlencodeParamHash({'key': 'value'}), | 42 remoting.Xhr.urlencodeParamHash({'key': 'value'}), |
32 'key=value'); | 43 'key=value'); |
33 assert.equal( | 44 assert.equal( |
34 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), | 45 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), |
35 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); | 46 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); |
36 assert.equal( | 47 assert.equal( |
37 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), | 48 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), |
38 'k1=v1&k2=v2'); | 49 'k1=v1&k2=v2'); |
39 }); | 50 }); |
40 | 51 |
41 QUnit.test('basic GET', function(assert) { | 52 // |
| 53 // Test for what happens when the parameters are specified |
| 54 // incorrectly. |
| 55 // |
| 56 |
| 57 QUnit.test('invalid *content parameters', function(assert) { |
| 58 assert.throws(function() { |
| 59 new remoting.Xhr({ |
| 60 method: 'POST', |
| 61 url: 'http://foo.com', |
| 62 jsonContent: {}, |
| 63 formContent: {} |
| 64 }); |
| 65 }); |
| 66 |
| 67 assert.throws(function() { |
| 68 new remoting.Xhr({ |
| 69 method: 'POST', |
| 70 url: 'http://foo.com', |
| 71 textContent: '', |
| 72 formContent: {} |
| 73 }); |
| 74 }); |
| 75 |
| 76 assert.throws(function() { |
| 77 new remoting.Xhr({ |
| 78 method: 'POST', |
| 79 url: 'http://foo.com', |
| 80 textContent: '', |
| 81 jsonContent: {} |
| 82 }); |
| 83 }); |
| 84 }); |
| 85 |
| 86 |
| 87 QUnit.test('invalid URL parameters', function(assert) { |
| 88 assert.throws(function() { |
| 89 new remoting.Xhr({ |
| 90 method: 'POST', |
| 91 url: 'http://foo.com?', |
| 92 urlParams: {} |
| 93 }); |
| 94 }); |
| 95 |
| 96 assert.throws(function() { |
| 97 new remoting.Xhr({ |
| 98 method: 'POST', |
| 99 url: 'http://foo.com#', |
| 100 urlParams: {} |
| 101 }); |
| 102 }); |
| 103 }); |
| 104 |
| 105 QUnit.test('invalid auth parameters', function(assert) { |
| 106 assert.throws(function() { |
| 107 new remoting.Xhr({ |
| 108 method: 'POST', |
| 109 url: 'http://foo.com', |
| 110 useIdentity: false, |
| 111 oauthToken: '', |
| 112 headers: { |
| 113 'Authorization': '' |
| 114 } |
| 115 }); |
| 116 }); |
| 117 |
| 118 assert.throws(function() { |
| 119 new remoting.Xhr({ |
| 120 method: 'POST', |
| 121 url: 'http://foo.com', |
| 122 useIdentity: true, |
| 123 headers: { |
| 124 'Authorization': '' |
| 125 } |
| 126 }); |
| 127 }); |
| 128 |
| 129 assert.throws(function() { |
| 130 new remoting.Xhr({ |
| 131 method: 'POST', |
| 132 url: 'http://foo.com', |
| 133 useIdentity: true, |
| 134 oauthToken: '', |
| 135 headers: {} |
| 136 }); |
| 137 }); |
| 138 }); |
| 139 |
| 140 QUnit.test('invalid auth parameters', function(assert) { |
| 141 assert.throws(function() { |
| 142 new remoting.Xhr({ |
| 143 method: 'POST', |
| 144 url: 'http://foo.com', |
| 145 useIdentity: false, |
| 146 oauthToken: '', |
| 147 headers: { |
| 148 'Authorization': '' |
| 149 } |
| 150 }); |
| 151 }); |
| 152 |
| 153 assert.throws(function() { |
| 154 new remoting.Xhr({ |
| 155 method: 'POST', |
| 156 url: 'http://foo.com', |
| 157 useIdentity: true, |
| 158 headers: { |
| 159 'Authorization': '' |
| 160 } |
| 161 }); |
| 162 }); |
| 163 |
| 164 assert.throws(function() { |
| 165 new remoting.Xhr({ |
| 166 method: 'POST', |
| 167 url: 'http://foo.com', |
| 168 useIdentity: true, |
| 169 oauthToken: '', |
| 170 headers: {} |
| 171 }); |
| 172 }); |
| 173 }); |
| 174 |
| 175 // |
| 176 // The typical case. |
| 177 // |
| 178 |
| 179 QUnit.test('successful GET', function(assert) { |
42 var promise = new remoting.Xhr({ | 180 var promise = new remoting.Xhr({ |
43 method: 'GET', | 181 method: 'GET', |
44 url: 'http://foo.com', | 182 url: 'http://foo.com' |
45 responseType: remoting.Xhr.ResponseType.TEXT | |
46 }).start().then(function(response) { | 183 }).start().then(function(response) { |
47 assert.equal(response.status, 200); | 184 assert.equal(response.status, 200); |
48 assert.equal(response.getText(), 'body'); | 185 assert.equal(response.getText(), 'body'); |
49 }); | 186 }); |
50 assert.equal(fakeXhr.method, 'GET'); | 187 assert.equal(fakeXhr.method, 'GET'); |
51 assert.equal(fakeXhr.url, 'http://foo.com'); | 188 assert.equal(fakeXhr.url, 'http://foo.com'); |
52 assert.equal(fakeXhr.withCredentials, false); | 189 assert.equal(fakeXhr.withCredentials, false); |
53 assert.equal(fakeXhr.requestBody, null); | 190 assert.equal(fakeXhr.requestBody, null); |
54 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | 191 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
55 fakeXhr.respond(200, {}, 'body'); | 192 fakeXhr.respond(200, {}, 'body'); |
56 return promise; | 193 return promise; |
57 }); | 194 }); |
58 | 195 |
59 QUnit.test('GET with param string', function(assert) { | 196 // |
| 197 // Tests for the effect of acceptJson. |
| 198 // |
| 199 |
| 200 QUnit.test('acceptJson required', function(assert) { |
| 201 var promise = new remoting.Xhr({ |
| 202 method: 'GET', |
| 203 url: 'http://foo.com' |
| 204 }).start().then(function(response) { |
| 205 assert.throws(response.getJson); |
| 206 }); |
| 207 fakeXhr.respond(200, {}, '{}'); |
| 208 return promise; |
| 209 }); |
| 210 |
| 211 QUnit.test('JSON response', function(assert) { |
| 212 var responseJson = { |
| 213 'myJsonData': [true] |
| 214 }; |
| 215 var responseText = JSON.stringify(responseJson); |
60 var promise = new remoting.Xhr({ | 216 var promise = new remoting.Xhr({ |
61 method: 'GET', | 217 method: 'GET', |
62 url: 'http://foo.com', | 218 url: 'http://foo.com', |
63 responseType: remoting.Xhr.ResponseType.TEXT, | 219 acceptJson: true |
64 urlParams: 'the_param_string' | |
65 }).start().then(function(response) { | 220 }).start().then(function(response) { |
66 assert.equal(response.status, 200); | 221 // Calling getText is still OK even when a JSON response is |
67 assert.equal(response.getText(), 'body'); | 222 // requested. |
| 223 assert.equal( |
| 224 response.getText(), |
| 225 responseText); |
| 226 // Check that getJson works as advertised. |
| 227 assert.deepEqual( |
| 228 response.getJson(), |
| 229 responseJson); |
| 230 // Calling getJson multiple times doesn't re-parse the response. |
| 231 assert.strictEqual( |
| 232 response.getJson(), |
| 233 response.getJson()); |
68 }); | 234 }); |
69 assert.equal(fakeXhr.method, 'GET'); | 235 fakeXhr.respond(200, {}, responseText); |
70 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); | |
71 assert.equal(fakeXhr.withCredentials, false); | |
72 assert.equal(fakeXhr.requestBody, null); | |
73 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
74 fakeXhr.respond(200, {}, 'body'); | |
75 return promise; | 236 return promise; |
76 }); | 237 }); |
77 | 238 |
78 QUnit.test('GET with param object', function(assert) { | 239 // |
79 var promise = new remoting.Xhr({ | 240 // Tests for various parameters that modify the HTTP request. |
| 241 // |
| 242 |
| 243 QUnit.test('GET with param string', function(assert) { |
| 244 new remoting.Xhr({ |
80 method: 'GET', | 245 method: 'GET', |
81 url: 'http://foo.com', | 246 url: 'http://foo.com', |
82 responseType: remoting.Xhr.ResponseType.TEXT, | 247 urlParams: 'the_param_string' |
| 248 }).start(); |
| 249 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); |
| 250 }); |
| 251 |
| 252 QUnit.test('GET with param object', function(assert) { |
| 253 new remoting.Xhr({ |
| 254 method: 'GET', |
| 255 url: 'http://foo.com', |
83 urlParams: {'a': 'b', 'c': 'd'} | 256 urlParams: {'a': 'b', 'c': 'd'} |
84 }).start().then(function(response) { | 257 }).start(); |
85 assert.equal(response.status, 200); | |
86 assert.equal(response.getText(), 'body'); | |
87 }); | |
88 assert.equal(fakeXhr.method, 'GET'); | |
89 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); | 258 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); |
90 assert.equal(fakeXhr.withCredentials, false); | |
91 assert.equal(fakeXhr.requestBody, null); | |
92 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
93 fakeXhr.respond(200, {}, 'body'); | |
94 return promise; | |
95 }); | 259 }); |
96 | 260 |
97 QUnit.test('GET with headers', function(assert) { | 261 QUnit.test('GET with headers', function(assert) { |
98 var promise = new remoting.Xhr({ | 262 new remoting.Xhr({ |
99 method: 'GET', | 263 method: 'GET', |
100 url: 'http://foo.com', | 264 url: 'http://foo.com', |
101 responseType: remoting.Xhr.ResponseType.TEXT, | |
102 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} | 265 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} |
103 }).start().then(function(response) { | 266 }).start(); |
104 assert.equal(response.status, 200); | |
105 assert.equal(response.getText(), 'body'); | |
106 }); | |
107 assert.equal(fakeXhr.method, 'GET'); | |
108 assert.equal(fakeXhr.url, 'http://foo.com'); | |
109 assert.equal(fakeXhr.withCredentials, false); | |
110 assert.equal(fakeXhr.requestBody, null); | |
111 assert.equal( | 267 assert.equal( |
112 fakeXhr.requestHeaders['Header1'], | 268 fakeXhr.requestHeaders['Header1'], |
113 'headerValue1'); | 269 'headerValue1'); |
114 assert.equal( | 270 assert.equal( |
115 fakeXhr.requestHeaders['Header2'], | 271 fakeXhr.requestHeaders['Header2'], |
116 'headerValue2'); | 272 'headerValue2'); |
117 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
118 fakeXhr.respond(200, {}, 'body'); | |
119 return promise; | |
120 }); | 273 }); |
121 | 274 |
122 | 275 |
123 QUnit.test('GET with credentials', function(assert) { | 276 QUnit.test('GET with credentials', function(assert) { |
124 var promise = new remoting.Xhr({ | 277 new remoting.Xhr({ |
125 method: 'GET', | 278 method: 'GET', |
126 url: 'http://foo.com', | 279 url: 'http://foo.com', |
127 responseType: remoting.Xhr.ResponseType.TEXT, | |
128 withCredentials: true | 280 withCredentials: true |
129 }).start().then(function(response) { | 281 }).start(); |
130 assert.equal(response.status, 200); | |
131 assert.equal(response.getText(), 'body'); | |
132 }); | |
133 assert.equal(fakeXhr.method, 'GET'); | |
134 assert.equal(fakeXhr.url, 'http://foo.com'); | |
135 assert.equal(fakeXhr.withCredentials, true); | 282 assert.equal(fakeXhr.withCredentials, true); |
136 assert.equal(fakeXhr.requestBody, null); | |
137 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | |
138 fakeXhr.respond(200, {}, 'body'); | |
139 return promise; | |
140 }); | 283 }); |
141 | 284 |
| 285 // |
| 286 // Checking that typical POST requests work. |
| 287 // |
| 288 |
142 QUnit.test('POST with text content', function(assert) { | 289 QUnit.test('POST with text content', function(assert) { |
143 var done = assert.async(); | 290 var done = assert.async(); |
144 | 291 |
145 var promise = new remoting.Xhr({ | 292 var promise = new remoting.Xhr({ |
146 method: 'POST', | 293 method: 'POST', |
147 url: 'http://foo.com', | 294 url: 'http://foo.com', |
148 responseType: remoting.Xhr.ResponseType.TEXT, | |
149 textContent: 'the_content_string' | 295 textContent: 'the_content_string' |
150 }).start().then(function(response) { | 296 }).start().then(function(response) { |
151 assert.equal(response.status, 200); | 297 assert.equal(response.status, 200); |
152 assert.equal(response.getText(), 'body'); | 298 assert.equal(response.getText(), 'body'); |
153 done(); | 299 done(); |
154 }); | 300 }); |
155 assert.equal(fakeXhr.method, 'POST'); | 301 assert.equal(fakeXhr.method, 'POST'); |
156 assert.equal(fakeXhr.url, 'http://foo.com'); | 302 assert.equal(fakeXhr.url, 'http://foo.com'); |
157 assert.equal(fakeXhr.withCredentials, false); | 303 assert.equal(fakeXhr.withCredentials, false); |
158 assert.equal(fakeXhr.requestBody, 'the_content_string'); | 304 assert.equal(fakeXhr.requestBody, 'the_content_string'); |
159 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); | 305 assert.equal(fakeXhr.requestHeaders['Content-type'], |
| 306 'text/plain; charset=UTF-8'); |
160 fakeXhr.respond(200, {}, 'body'); | 307 fakeXhr.respond(200, {}, 'body'); |
161 return promise; | 308 return promise; |
162 }); | 309 }); |
163 | 310 |
164 QUnit.test('POST with form content', function(assert) { | 311 QUnit.test('POST with form content', function(assert) { |
165 var promise = new remoting.Xhr({ | 312 new remoting.Xhr({ |
166 method: 'POST', | 313 method: 'POST', |
167 url: 'http://foo.com', | 314 url: 'http://foo.com', |
168 responseType: remoting.Xhr.ResponseType.TEXT, | |
169 formContent: {'a': 'b', 'c': 'd'} | 315 formContent: {'a': 'b', 'c': 'd'} |
170 }).start().then(function(response) { | 316 }).start(); |
171 assert.equal(response.status, 200); | |
172 assert.equal(response.getText(), 'body'); | |
173 }); | |
174 assert.equal(fakeXhr.method, 'POST'); | |
175 assert.equal(fakeXhr.url, 'http://foo.com'); | |
176 assert.equal(fakeXhr.withCredentials, false); | |
177 assert.equal(fakeXhr.requestBody, 'a=b&c=d'); | 317 assert.equal(fakeXhr.requestBody, 'a=b&c=d'); |
178 assert.equal( | 318 assert.equal( |
179 fakeXhr.requestHeaders['Content-type'], | 319 fakeXhr.requestHeaders['Content-type'], |
180 'application/x-www-form-urlencoded'); | 320 'application/x-www-form-urlencoded; charset=UTF-8'); |
181 fakeXhr.respond(200, {}, 'body'); | |
182 return promise; | |
183 }); | 321 }); |
184 | 322 |
185 QUnit.test('defaultResponse 200', function(assert) { | 323 // |
186 var done = assert.async(); | 324 // Tests for authentication-related options. |
| 325 // |
187 | 326 |
188 var onDone = function() { | 327 QUnit.test('GET with auth token', function(assert) { |
189 assert.ok(true); | |
190 done(); | |
191 }; | |
192 | |
193 var onError = function(error) { | |
194 assert.ok(false); | |
195 done(); | |
196 }; | |
197 | |
198 new remoting.Xhr({ | 328 new remoting.Xhr({ |
199 method: 'POST', | 329 method: 'GET', |
200 url: 'http://foo.com' | 330 url: 'http://foo.com', |
201 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); | 331 oauthToken: 'my_token' |
202 fakeXhr.respond(200, {}, ''); | 332 }).start(); |
| 333 assert.equal(fakeXhr.requestHeaders['Authorization'], |
| 334 'Bearer my_token'); |
203 }); | 335 }); |
204 | 336 |
| 337 QUnit.test('GET with useIdentity', function(assert) { |
| 338 var xhr = new remoting.Xhr({ |
| 339 method: 'GET', |
| 340 url: 'http://foo.com', |
| 341 useIdentity: true |
| 342 }); |
205 | 343 |
206 QUnit.test('defaultResponse 404', function(assert) { | 344 xhr.start(); |
| 345 |
207 var done = assert.async(); | 346 var done = assert.async(); |
208 | 347 fakeXhr.addEventListener('loadstart', function() { |
209 var onDone = function() { | 348 assert.equal(fakeXhr.requestHeaders['Authorization'], |
210 assert.ok(false); | 349 'Bearer my_token'); |
211 done(); | 350 done(); |
212 }; | 351 }); |
213 | |
214 var onError = function(error) { | |
215 assert.ok(true); | |
216 done(); | |
217 }; | |
218 | |
219 new remoting.Xhr({ | |
220 method: 'POST', | |
221 url: 'http://foo.com' | |
222 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); | |
223 fakeXhr.respond(404, {}, ''); | |
224 }); | 352 }); |
225 | 353 |
226 })(); | 354 })(); |
OLD | NEW |