Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(631)

Side by Side Diff: remoting/webapp/crd/js/xhr_unittest.js

Issue 1028683004: Added better error and OAuth support in xhr.js. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « remoting/webapp/crd/js/xhr.js ('k') | remoting/webapp/js_proto/sinon_proto.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 },
28 afterEach: function() {
29 remoting.identity = null;
23 } 30 }
24 }); 31 });
25 32
26 QUnit.test('urlencodeParamHash', function(assert) { 33 QUnit.test('urlencodeParamHash', function(assert) {
27 assert.equal( 34 assert.equal(
28 remoting.Xhr.urlencodeParamHash({}), 35 remoting.Xhr.urlencodeParamHash({}),
29 ''); 36 '');
30 assert.equal( 37 assert.equal(
31 remoting.Xhr.urlencodeParamHash({'key': 'value'}), 38 remoting.Xhr.urlencodeParamHash({'key': 'value'}),
32 'key=value'); 39 'key=value');
33 assert.equal( 40 assert.equal(
34 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}), 41 remoting.Xhr.urlencodeParamHash({'key /?=&': 'value /?=&'}),
35 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26'); 42 'key%20%2F%3F%3D%26=value%20%2F%3F%3D%26');
36 assert.equal( 43 assert.equal(
37 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}), 44 remoting.Xhr.urlencodeParamHash({'k1': 'v1', 'k2': 'v2'}),
38 'k1=v1&k2=v2'); 45 'k1=v1&k2=v2');
39 }); 46 });
40 47
41 QUnit.test('basic GET', function(assert) { 48 //
49 // Test for what happens when the parameters are specified
50 // incorrectly.
51 //
52
53 QUnit.test('invalid *content parameters', function(assert) {
54 assert.throws(function() {
55 new remoting.Xhr({
56 method: 'POST',
57 url: 'http://foo.com',
58 jsonContent: {},
59 formContent: {}
60 });
61 });
62
63 assert.throws(function() {
64 new remoting.Xhr({
65 method: 'POST',
66 url: 'http://foo.com',
67 textContent: '',
68 formContent: {}
69 });
70 });
71
72 assert.throws(function() {
73 new remoting.Xhr({
74 method: 'POST',
75 url: 'http://foo.com',
76 textContent: '',
77 jsonContent: {}
78 });
79 });
80 });
81
82
83 QUnit.test('invalid URL parameters', function(assert) {
84 assert.throws(function() {
85 new remoting.Xhr({
86 method: 'POST',
87 url: 'http://foo.com?',
88 urlParams: {}
89 });
90 });
91
92 assert.throws(function() {
93 new remoting.Xhr({
94 method: 'POST',
95 url: 'http://foo.com#',
96 urlParams: {}
97 });
98 });
99 });
100
101 QUnit.test('invalid auth parameters', function(assert) {
102 assert.throws(function() {
103 new remoting.Xhr({
104 method: 'POST',
105 url: 'http://foo.com',
106 useIdentity: false,
107 oauthToken: '',
108 headers: {
109 'Authorization': ''
110 }
111 });
112 });
113
114 assert.throws(function() {
115 new remoting.Xhr({
116 method: 'POST',
117 url: 'http://foo.com',
118 useIdentity: true,
119 headers: {
120 'Authorization': ''
121 }
122 });
123 });
124
125 assert.throws(function() {
126 new remoting.Xhr({
127 method: 'POST',
128 url: 'http://foo.com',
129 useIdentity: true,
130 oauthToken: '',
131 headers: {}
132 });
133 });
134 });
135
136 QUnit.test('invalid auth parameters', function(assert) {
137 assert.throws(function() {
138 new remoting.Xhr({
139 method: 'POST',
140 url: 'http://foo.com',
141 useIdentity: false,
142 oauthToken: '',
143 headers: {
144 'Authorization': ''
145 }
146 });
147 });
148
149 assert.throws(function() {
150 new remoting.Xhr({
151 method: 'POST',
152 url: 'http://foo.com',
153 useIdentity: true,
154 headers: {
155 'Authorization': ''
156 }
157 });
158 });
159
160 assert.throws(function() {
161 new remoting.Xhr({
162 method: 'POST',
163 url: 'http://foo.com',
164 useIdentity: true,
165 oauthToken: '',
166 headers: {}
167 });
168 });
169 });
170
171 //
172 // The typical case.
173 //
174
175 QUnit.test('successful GET', function(assert) {
42 var promise = new remoting.Xhr({ 176 var promise = new remoting.Xhr({
43 method: 'GET', 177 method: 'GET',
44 url: 'http://foo.com', 178 url: 'http://foo.com'
45 responseType: remoting.Xhr.ResponseType.TEXT
46 }).start().then(function(response) { 179 }).start().then(function(response) {
47 assert.equal(response.status, 200); 180 assert.equal(response.status, 200);
48 assert.equal(response.getText(), 'body'); 181 assert.equal(response.getText(), 'body');
49 }); 182 });
50 assert.equal(fakeXhr.method, 'GET'); 183 assert.equal(fakeXhr.method, 'GET');
51 assert.equal(fakeXhr.url, 'http://foo.com'); 184 assert.equal(fakeXhr.url, 'http://foo.com');
52 assert.equal(fakeXhr.withCredentials, false); 185 assert.equal(fakeXhr.withCredentials, false);
53 assert.equal(fakeXhr.requestBody, null); 186 assert.equal(fakeXhr.requestBody, null);
54 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); 187 assert.ok(!('Content-type' in fakeXhr.requestHeaders));
55 fakeXhr.respond(200, {}, 'body'); 188 fakeXhr.respond(200, {}, 'body');
56 return promise; 189 return promise;
57 }); 190 });
58 191
59 QUnit.test('GET with param string', function(assert) { 192 //
193 // Handling of errors with different values of ignoreErrors.
194 //
195
196 QUnit.test('failed GET', function(assert) {
197 var promise = new remoting.Xhr({
198 method: 'GET',
199 url: 'http://foo.com'
200 }).start().then(function(response) {
201 assert.equal(response.status, 500);
202 assert.equal(response.getText(), 'body');
203 });
204 fakeXhr.respond(500, {}, 'body');
205 return promise;
206 });
207
208 QUnit.test('failed GET with non-ignored error', function(assert) {
209 var expectedError = remoting.Error.fromHttpStatus(500);
210 assert.ok(!expectedError.isNone());
211
60 var promise = new remoting.Xhr({ 212 var promise = new remoting.Xhr({
61 method: 'GET', 213 method: 'GET',
62 url: 'http://foo.com', 214 url: 'http://foo.com',
63 responseType: remoting.Xhr.ResponseType.TEXT, 215 ignoreErrors: []
64 urlParams: 'the_param_string' 216 }).start().then(function() {
65 }).start().then(function(response) { 217 assert.ok(false);
66 assert.equal(response.status, 200); 218 }, function(/** remoting.Error */ error) {
67 assert.equal(response.getText(), 'body'); 219 assert.equal(error.getTag(), expectedError.getTag());
68 }); 220 });
69 assert.equal(fakeXhr.method, 'GET'); 221 fakeXhr.respond(500, {}, 'body');
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; 222 return promise;
76 }); 223 });
77 224
78 QUnit.test('GET with param object', function(assert) { 225
226 QUnit.test('failed GET with ignored error', function(assert) {
227 var expectedError = remoting.Error.fromHttpStatus(500);
228 assert.ok(!expectedError.isNone());
229
79 var promise = new remoting.Xhr({ 230 var promise = new remoting.Xhr({
80 method: 'GET', 231 method: 'GET',
81 url: 'http://foo.com', 232 url: 'http://foo.com',
82 responseType: remoting.Xhr.ResponseType.TEXT, 233 ignoreErrors: [expectedError.getTag()]
83 urlParams: {'a': 'b', 'c': 'd'} 234 }).start();
84 }).start().then(function(response) { 235 fakeXhr.respond(500, {}, 'body');
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');
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; 236 return promise;
95 }); 237 });
96 238
97 QUnit.test('GET with headers', function(assert) { 239 //
240 // Tests for the effect of acceptJson.
241 //
242
243 QUnit.test('acceptJson required', function(assert) {
244 var promise = new remoting.Xhr({
245 method: 'GET',
246 url: 'http://foo.com'
247 }).start().then(function(response) {
248 assert.throws(response.getJson);
249 });
250 fakeXhr.respond(200, {}, '{}');
251 return promise;
252 });
253
254 QUnit.test('JSON response', function(assert) {
255 var responseJson = {
256 'myJsonData': [true]
257 };
258 var responseText = JSON.stringify(jsonData);
98 var promise = new remoting.Xhr({ 259 var promise = new remoting.Xhr({
99 method: 'GET', 260 method: 'GET',
100 url: 'http://foo.com', 261 url: 'http://foo.com',
101 responseType: remoting.Xhr.ResponseType.TEXT, 262 acceptJson: true
263 }).start().then(function(response) {
264 // Calling getText is still OK even when a JSON response is
265 // requested.
266 assert.equal(
267 response.getText(),
268 responseText);
269 // Check that getJson works as advertized.
270 assert.deepEqual(
271 response.getJson(),
272 responseJson);
273 // Calling getJson multiple times doesn't re-parse the response.
274 assert.strictEqual(
275 response.getJson(),
276 response.getJson());
277 });
278 fakeXhr.respond(200, {}, responseText);
279 return promise;
280 });
281
282 //
283 // Tests for various parameters that modify the HTTP request.
284 //
285
286 QUnit.test('GET with param string', function(assert) {
rmsousa 2015/03/21 01:49:56 "Since you're here": could you add a unit test for
John Williams 2015/03/23 19:55:10 Done.
287 new remoting.Xhr({
288 method: 'GET',
289 url: 'http://foo.com',
290 urlParams: 'the_param_string'
291 }).start();
292 assert.equal(fakeXhr.url, 'http://foo.com?the_param_string');
293 });
294
295 QUnit.test('GET with param object', function(assert) {
296 new remoting.Xhr({
297 method: 'GET',
298 url: 'http://foo.com',
299 urlParams: {'a': 'b', 'c': 'd'}
300 }).start();
301 assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d');
302 });
303
304 QUnit.test('GET with headers', function(assert) {
305 new remoting.Xhr({
306 method: 'GET',
307 url: 'http://foo.com',
102 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} 308 headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'}
103 }).start().then(function(response) { 309 }).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( 310 assert.equal(
112 fakeXhr.requestHeaders['Header1'], 311 fakeXhr.requestHeaders['Header1'],
113 'headerValue1'); 312 'headerValue1');
114 assert.equal( 313 assert.equal(
115 fakeXhr.requestHeaders['Header2'], 314 fakeXhr.requestHeaders['Header2'],
116 'headerValue2'); 315 'headerValue2');
117 assert.ok(!('Content-type' in fakeXhr.requestHeaders));
118 fakeXhr.respond(200, {}, 'body');
119 return promise;
120 }); 316 });
121 317
122 318
123 QUnit.test('GET with credentials', function(assert) { 319 QUnit.test('GET with credentials', function(assert) {
124 var promise = new remoting.Xhr({ 320 new remoting.Xhr({
125 method: 'GET', 321 method: 'GET',
126 url: 'http://foo.com', 322 url: 'http://foo.com',
127 responseType: remoting.Xhr.ResponseType.TEXT,
128 withCredentials: true 323 withCredentials: true
129 }).start().then(function(response) { 324 }).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); 325 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 }); 326 });
141 327
328 //
329 // Checking that typical POST requests work.
330 //
331
142 QUnit.test('POST with text content', function(assert) { 332 QUnit.test('POST with text content', function(assert) {
143 var done = assert.async(); 333 var done = assert.async();
144 334
145 var promise = new remoting.Xhr({ 335 var promise = new remoting.Xhr({
146 method: 'POST', 336 method: 'POST',
147 url: 'http://foo.com', 337 url: 'http://foo.com',
148 responseType: remoting.Xhr.ResponseType.TEXT,
149 textContent: 'the_content_string' 338 textContent: 'the_content_string'
150 }).start().then(function(response) { 339 }).start().then(function(response) {
151 assert.equal(response.status, 200); 340 assert.equal(response.status, 200);
152 assert.equal(response.getText(), 'body'); 341 assert.equal(response.getText(), 'body');
153 done(); 342 done();
154 }); 343 });
155 assert.equal(fakeXhr.method, 'POST'); 344 assert.equal(fakeXhr.method, 'POST');
156 assert.equal(fakeXhr.url, 'http://foo.com'); 345 assert.equal(fakeXhr.url, 'http://foo.com');
157 assert.equal(fakeXhr.withCredentials, false); 346 assert.equal(fakeXhr.withCredentials, false);
158 assert.equal(fakeXhr.requestBody, 'the_content_string'); 347 assert.equal(fakeXhr.requestBody, 'the_content_string');
159 assert.ok(!('Content-type' in fakeXhr.requestHeaders)); 348 assert.equal(fakeXhr.requestHeaders['Content-type'],
349 'text/plain; charset=UTF-8');
160 fakeXhr.respond(200, {}, 'body'); 350 fakeXhr.respond(200, {}, 'body');
161 return promise; 351 return promise;
162 }); 352 });
163 353
164 QUnit.test('POST with form content', function(assert) { 354 QUnit.test('POST with form content', function(assert) {
165 var promise = new remoting.Xhr({ 355 new remoting.Xhr({
166 method: 'POST', 356 method: 'POST',
167 url: 'http://foo.com', 357 url: 'http://foo.com',
168 responseType: remoting.Xhr.ResponseType.TEXT,
169 formContent: {'a': 'b', 'c': 'd'} 358 formContent: {'a': 'b', 'c': 'd'}
170 }).start().then(function(response) { 359 }).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'); 360 assert.equal(fakeXhr.requestBody, 'a=b&c=d');
178 assert.equal( 361 assert.equal(
179 fakeXhr.requestHeaders['Content-type'], 362 fakeXhr.requestHeaders['Content-type'],
180 'application/x-www-form-urlencoded'); 363 'application/x-www-form-urlencoded; charset=UTF-8');
181 fakeXhr.respond(200, {}, 'body');
182 return promise;
183 }); 364 });
184 365
185 QUnit.test('defaultResponse 200', function(assert) { 366 //
186 var done = assert.async(); 367 // Tests for authentication-related options.
368 //
187 369
188 var onDone = function() { 370 QUnit.test('GET with auth token', function(assert) {
189 assert.ok(true); 371 new remoting.Xhr({
190 done(); 372 method: 'GET',
191 }; 373 url: 'http://foo.com',
374 oauthToken: 'my_token'
375 }).start();
376 assert.equal(fakeXhr.requestHeaders['Authorization'],
377 'Bearer my_token');
378 });
192 379
193 var onError = function(error) { 380 QUnit.test('GET with useIdentity', function(assert) {
194 assert.ok(false); 381 var tokenPromise = Promise.resolve('my_token');
195 done(); 382
196 }; 383 remoting.identity = new remoting.Identity();
384 sinon.stub(remoting.identity, 'getToken').returns(tokenPromise);
197 385
198 new remoting.Xhr({ 386 new remoting.Xhr({
199 method: 'POST', 387 method: 'GET',
200 url: 'http://foo.com' 388 url: 'http://foo.com',
201 }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); 389 useIdentity: true
202 fakeXhr.respond(200, {}, ''); 390 }).start();
203 });
204 391
205 392 return tokenPromise.then(function() {
206 QUnit.test('defaultResponse 404', function(assert) { 393 assert.equal(fakeXhr.requestHeaders['Authorization'],
207 var done = assert.async(); 394 'Bearer my_token');
208 395 fakeXhr.respond(200, {}, '');
209 var onDone = function() { 396 });
210 assert.ok(false);
211 done();
212 };
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 }); 397 });
225 398
226 })(); 399 })();
OLDNEW
« no previous file with comments | « remoting/webapp/crd/js/xhr.js ('k') | remoting/webapp/js_proto/sinon_proto.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698