Chromium Code Reviews| Index: remoting/webapp/crd/js/xhr_unittest.js |
| diff --git a/remoting/webapp/crd/js/xhr_unittest.js b/remoting/webapp/crd/js/xhr_unittest.js |
| index b02987e13aac18f83e0003a869906402856e1305..0e74d9bab0f93b90acd69ce79329a05e91f02d2f 100644 |
| --- a/remoting/webapp/crd/js/xhr_unittest.js |
| +++ b/remoting/webapp/crd/js/xhr_unittest.js |
| @@ -10,16 +10,23 @@ |
| 'use strict'; |
| +/** @type {sinon.FakeXhrCtrl} */ |
| +var fakeXhrCtrl; |
| + |
| /** @type {sinon.FakeXhr} */ |
| var fakeXhr; |
| QUnit.module('xhr', { |
| beforeEach: function() { |
| fakeXhr = null; |
| - sinon.useFakeXMLHttpRequest().onCreate = |
| + fakeXhrCtrl = sinon.useFakeXMLHttpRequest(); |
| + fakeXhrCtrl.onCreate = |
| function(/** sinon.FakeXhr */ xhr) { |
| fakeXhr = xhr; |
| }; |
| + }, |
| + afterEach: function() { |
| + remoting.identity = null; |
| } |
| }); |
| @@ -38,14 +45,140 @@ QUnit.test('urlencodeParamHash', function(assert) { |
| 'k1=v1&k2=v2'); |
| }); |
| -QUnit.test('basic GET', function(assert) { |
| +// |
| +// Test for what happens when the parameters are specified |
| +// incorrectly. |
| +// |
| + |
| +QUnit.test('invalid *content parameters', function(assert) { |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + jsonContent: {}, |
| + formContent: {} |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + textContent: '', |
| + formContent: {} |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + textContent: '', |
| + jsonContent: {} |
| + }); |
| + }); |
| +}); |
| + |
| + |
| +QUnit.test('invalid URL parameters', function(assert) { |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com?', |
| + urlParams: {} |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com#', |
| + urlParams: {} |
| + }); |
| + }); |
| +}); |
| + |
| +QUnit.test('invalid auth parameters', function(assert) { |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: false, |
| + oauthToken: '', |
| + headers: { |
| + 'Authorization': '' |
| + } |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: true, |
| + headers: { |
| + 'Authorization': '' |
| + } |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: true, |
| + oauthToken: '', |
| + headers: {} |
| + }); |
| + }); |
| +}); |
| + |
| +QUnit.test('invalid auth parameters', function(assert) { |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: false, |
| + oauthToken: '', |
| + headers: { |
| + 'Authorization': '' |
| + } |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: true, |
| + headers: { |
| + 'Authorization': '' |
| + } |
| + }); |
| + }); |
| + |
| + assert.throws(function() { |
| + new remoting.Xhr({ |
| + method: 'POST', |
| + url: 'http://foo.com', |
| + useIdentity: true, |
| + oauthToken: '', |
| + headers: {} |
| + }); |
| + }); |
| +}); |
| + |
| +// |
| +// The typical case. |
| +// |
| + |
| +QUnit.test('successful GET', function(assert) { |
| var promise = new remoting.Xhr({ |
| method: 'GET', |
| - url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT |
| + url: 'http://foo.com' |
| }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| - assert.equal(response.getText(), 'body'); |
| + assert.equal(response.status, 200); |
| + assert.equal(response.getText(), 'body'); |
| }); |
| assert.equal(fakeXhr.method, 'GET'); |
| assert.equal(fakeXhr.url, 'http://foo.com'); |
| @@ -56,96 +189,152 @@ QUnit.test('basic GET', function(assert) { |
| return promise; |
| }); |
| -QUnit.test('GET with param string', function(assert) { |
| +// |
| +// Handling of errors with different values of ignoreErrors. |
| +// |
| + |
| +QUnit.test('failed GET', function(assert) { |
| var promise = new remoting.Xhr({ |
| method: 'GET', |
| - url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| - urlParams: 'the_param_string' |
| + url: 'http://foo.com' |
| }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| + assert.equal(response.status, 500); |
| assert.equal(response.getText(), 'body'); |
| }); |
| - assert.equal(fakeXhr.method, 'GET'); |
| - assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); |
| - assert.equal(fakeXhr.withCredentials, false); |
| - assert.equal(fakeXhr.requestBody, null); |
| - assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| - fakeXhr.respond(200, {}, 'body'); |
| + fakeXhr.respond(500, {}, 'body'); |
| return promise; |
| }); |
| -QUnit.test('GET with param object', function(assert) { |
| +QUnit.test('failed GET with non-ignored error', function(assert) { |
| + var expectedError = remoting.Error.fromHttpStatus(500); |
| + assert.ok(!expectedError.isNone()); |
| + |
| var promise = new remoting.Xhr({ |
| method: 'GET', |
| url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| - urlParams: {'a': 'b', 'c': 'd'} |
| + ignoreErrors: [] |
| + }).start().then(function() { |
| + assert.ok(false); |
| + }, function(/** remoting.Error */ error) { |
| + assert.equal(error.getTag(), expectedError.getTag()); |
| + }); |
| + fakeXhr.respond(500, {}, 'body'); |
| + return promise; |
| +}); |
| + |
| + |
| +QUnit.test('failed GET with ignored error', function(assert) { |
| + var expectedError = remoting.Error.fromHttpStatus(500); |
| + assert.ok(!expectedError.isNone()); |
| + |
| + var promise = new remoting.Xhr({ |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + ignoreErrors: [expectedError.getTag()] |
| + }).start(); |
| + fakeXhr.respond(500, {}, 'body'); |
| + return promise; |
| +}); |
| + |
| +// |
| +// Tests for the effect of acceptJson. |
| +// |
| + |
| +QUnit.test('acceptJson required', function(assert) { |
| + var promise = new remoting.Xhr({ |
| + method: 'GET', |
| + url: 'http://foo.com' |
| }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| - assert.equal(response.getText(), 'body'); |
| + assert.throws(response.getJson); |
| }); |
| - assert.equal(fakeXhr.method, 'GET'); |
| - assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); |
| - assert.equal(fakeXhr.withCredentials, false); |
| - assert.equal(fakeXhr.requestBody, null); |
| - assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| - fakeXhr.respond(200, {}, 'body'); |
| + fakeXhr.respond(200, {}, '{}'); |
| return promise; |
| }); |
| -QUnit.test('GET with headers', function(assert) { |
| +QUnit.test('JSON response', function(assert) { |
| + var responseJson = { |
| + 'myJsonData': [true] |
| + }; |
| + var responseText = JSON.stringify(jsonData); |
| var promise = new remoting.Xhr({ |
| method: 'GET', |
| url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| - headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} |
| + acceptJson: true |
| }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| - assert.equal(response.getText(), 'body'); |
| + // Calling getText is still OK even when a JSON response is |
| + // requested. |
| + assert.equal( |
| + response.getText(), |
| + responseText); |
| + // Check that getJson works as advertized. |
| + assert.deepEqual( |
| + response.getJson(), |
| + responseJson); |
| + // Calling getJson multiple times doesn't re-parse the response. |
| + assert.strictEqual( |
| + response.getJson(), |
| + response.getJson()); |
| }); |
| - assert.equal(fakeXhr.method, 'GET'); |
| - assert.equal(fakeXhr.url, 'http://foo.com'); |
| - assert.equal(fakeXhr.withCredentials, false); |
| - assert.equal(fakeXhr.requestBody, null); |
| + fakeXhr.respond(200, {}, responseText); |
| + return promise; |
| +}); |
| + |
| +// |
| +// Tests for various parameters that modify the HTTP request. |
| +// |
| + |
| +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.
|
| + new remoting.Xhr({ |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + urlParams: 'the_param_string' |
| + }).start(); |
| + assert.equal(fakeXhr.url, 'http://foo.com?the_param_string'); |
| +}); |
| + |
| +QUnit.test('GET with param object', function(assert) { |
| + new remoting.Xhr({ |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + urlParams: {'a': 'b', 'c': 'd'} |
| + }).start(); |
| + assert.equal(fakeXhr.url, 'http://foo.com?a=b&c=d'); |
| +}); |
| + |
| +QUnit.test('GET with headers', function(assert) { |
| + new remoting.Xhr({ |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + headers: {'Header1': 'headerValue1', 'Header2': 'headerValue2'} |
| + }).start(); |
| assert.equal( |
| fakeXhr.requestHeaders['Header1'], |
| 'headerValue1'); |
| assert.equal( |
| fakeXhr.requestHeaders['Header2'], |
| 'headerValue2'); |
| - assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| - fakeXhr.respond(200, {}, 'body'); |
| - return promise; |
| }); |
| QUnit.test('GET with credentials', function(assert) { |
| - var promise = new remoting.Xhr({ |
| + new remoting.Xhr({ |
| method: 'GET', |
| url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| withCredentials: true |
| - }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| - assert.equal(response.getText(), 'body'); |
| - }); |
| - assert.equal(fakeXhr.method, 'GET'); |
| - assert.equal(fakeXhr.url, 'http://foo.com'); |
| + }).start(); |
| assert.equal(fakeXhr.withCredentials, true); |
| - assert.equal(fakeXhr.requestBody, null); |
| - assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| - fakeXhr.respond(200, {}, 'body'); |
| - return promise; |
| }); |
| +// |
| +// Checking that typical POST requests work. |
| +// |
| + |
| QUnit.test('POST with text content', function(assert) { |
| var done = assert.async(); |
| var promise = new remoting.Xhr({ |
| method: 'POST', |
| url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| textContent: 'the_content_string' |
| }).start().then(function(response) { |
| assert.equal(response.status, 200); |
| @@ -156,71 +345,55 @@ QUnit.test('POST with text content', function(assert) { |
| assert.equal(fakeXhr.url, 'http://foo.com'); |
| assert.equal(fakeXhr.withCredentials, false); |
| assert.equal(fakeXhr.requestBody, 'the_content_string'); |
| - assert.ok(!('Content-type' in fakeXhr.requestHeaders)); |
| + assert.equal(fakeXhr.requestHeaders['Content-type'], |
| + 'text/plain; charset=UTF-8'); |
| fakeXhr.respond(200, {}, 'body'); |
| return promise; |
| }); |
| QUnit.test('POST with form content', function(assert) { |
| - var promise = new remoting.Xhr({ |
| + new remoting.Xhr({ |
| method: 'POST', |
| url: 'http://foo.com', |
| - responseType: remoting.Xhr.ResponseType.TEXT, |
| formContent: {'a': 'b', 'c': 'd'} |
| - }).start().then(function(response) { |
| - assert.equal(response.status, 200); |
| - assert.equal(response.getText(), 'body'); |
| - }); |
| - assert.equal(fakeXhr.method, 'POST'); |
| - assert.equal(fakeXhr.url, 'http://foo.com'); |
| - assert.equal(fakeXhr.withCredentials, false); |
| + }).start(); |
| assert.equal(fakeXhr.requestBody, 'a=b&c=d'); |
| assert.equal( |
| fakeXhr.requestHeaders['Content-type'], |
| - 'application/x-www-form-urlencoded'); |
| - fakeXhr.respond(200, {}, 'body'); |
| - return promise; |
| + 'application/x-www-form-urlencoded; charset=UTF-8'); |
| }); |
| -QUnit.test('defaultResponse 200', function(assert) { |
| - var done = assert.async(); |
| - |
| - var onDone = function() { |
| - assert.ok(true); |
| - done(); |
| - }; |
| - |
| - var onError = function(error) { |
| - assert.ok(false); |
| - done(); |
| - }; |
| +// |
| +// Tests for authentication-related options. |
| +// |
| +QUnit.test('GET with auth token', function(assert) { |
| new remoting.Xhr({ |
| - method: 'POST', |
| - url: 'http://foo.com' |
| - }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); |
| - fakeXhr.respond(200, {}, ''); |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + oauthToken: 'my_token' |
| + }).start(); |
| + assert.equal(fakeXhr.requestHeaders['Authorization'], |
| + 'Bearer my_token'); |
| }); |
| +QUnit.test('GET with useIdentity', function(assert) { |
| + var tokenPromise = Promise.resolve('my_token'); |
| -QUnit.test('defaultResponse 404', function(assert) { |
| - var done = assert.async(); |
| - |
| - var onDone = function() { |
| - assert.ok(false); |
| - done(); |
| - }; |
| - |
| - var onError = function(error) { |
| - assert.ok(true); |
| - done(); |
| - }; |
| + remoting.identity = new remoting.Identity(); |
| + sinon.stub(remoting.identity, 'getToken').returns(tokenPromise); |
| new remoting.Xhr({ |
| - method: 'POST', |
| - url: 'http://foo.com' |
| - }).start().then(remoting.Xhr.defaultResponse(onDone, onError)); |
| - fakeXhr.respond(404, {}, ''); |
| + method: 'GET', |
| + url: 'http://foo.com', |
| + useIdentity: true |
| + }).start(); |
| + |
| + return tokenPromise.then(function() { |
| + assert.equal(fakeXhr.requestHeaders['Authorization'], |
| + 'Bearer my_token'); |
| + fakeXhr.respond(200, {}, ''); |
| + }); |
| }); |
| })(); |