summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjrw <jrw@chromium.org>2015-03-27 18:47:25 -0700
committerCommit bot <commit-bot@chromium.org>2015-03-28 01:47:50 +0000
commitf6af169dd63e9aa32ec961fe67d6c6c7d7295e38 (patch)
treed2a05fcf29eeba31adf293a9af4914fe1349ed2d
parente80156ffd5b377f9709b9664c39ac93e58ad5539 (diff)
downloadchromium_src-f6af169dd63e9aa32ec961fe67d6c6c7d7295e38.zip
chromium_src-f6af169dd63e9aa32ec961fe67d6c6c7d7295e38.tar.gz
chromium_src-f6af169dd63e9aa32ec961fe67d6c6c7d7295e38.tar.bz2
Added isError method to remoting.Xhr.Error to check for HTTP errors.
BUG= Review URL: https://codereview.chromium.org/1039853002 Cr-Commit-Position: refs/heads/master@{#322694}
-rw-r--r--remoting/webapp/crd/js/xhr.js8
-rw-r--r--remoting/webapp/crd/js/xhr_unittest.js42
2 files changed, 49 insertions, 1 deletions
diff --git a/remoting/webapp/crd/js/xhr.js b/remoting/webapp/crd/js/xhr.js
index cc50b27..4b3603e 100644
--- a/remoting/webapp/crd/js/xhr.js
+++ b/remoting/webapp/crd/js/xhr.js
@@ -294,6 +294,14 @@ remoting.Xhr.Response = function(xhr, allowJson) {
};
/**
+ * @return {boolean} True if the response code is outside the 200-299
+ * range (i.e. success as defined by the HTTP protocol).
+ */
+remoting.Xhr.Response.prototype.isError = function() {
+ return this.status < 200 || this.status >= 300;
+};
+
+/**
* @return {string} The text content of the response.
*/
remoting.Xhr.Response.prototype.getText = function() {
diff --git a/remoting/webapp/crd/js/xhr_unittest.js b/remoting/webapp/crd/js/xhr_unittest.js
index c8e96e0..f5cb772 100644
--- a/remoting/webapp/crd/js/xhr_unittest.js
+++ b/remoting/webapp/crd/js/xhr_unittest.js
@@ -181,6 +181,7 @@ QUnit.test('successful GET', function(assert) {
method: 'GET',
url: 'http://foo.com'
}).start().then(function(response) {
+ assert.ok(!response.isError());
assert.equal(response.status, 200);
assert.equal(response.getText(), 'body');
});
@@ -342,7 +343,6 @@ QUnit.test('GET with useIdentity', function(assert) {
});
xhr.start();
-
var done = assert.async();
fakeXhr.addEventListener('loadstart', function() {
assert.equal(fakeXhr.requestHeaders['Authorization'],
@@ -351,4 +351,44 @@ QUnit.test('GET with useIdentity', function(assert) {
});
});
+//
+// Error responses.
+//
+QUnit.test('GET with error response', function(assert) {
+ var promise = new remoting.Xhr({
+ method: 'GET',
+ url: 'http://foo.com'
+ }).start().then(function(response) {
+ assert.ok(response.isError());
+ assert.equal(response.status, 500);
+ assert.equal(response.getText(), 'body');
+ });
+ fakeXhr.respond(500, {}, 'body');
+ return promise;
+});
+
+QUnit.test('204 is not an error', function(assert) {
+ var promise = new remoting.Xhr({
+ method: 'GET',
+ url: 'http://foo.com'
+ }).start().then(function(response) {
+ assert.ok(!response.isError());
+ assert.equal(response.status, 204);
+ assert.equal(response.getText(), '');
+ });
+ fakeXhr.respond(204, {}, null);
+ return promise;
+});
+
+QUnit.test('GET with non-HTTP response', function(assert) {
+ var promise = new remoting.Xhr({
+ method: 'GET',
+ url: 'http://foo.com'
+ }).start().then(function(response) {
+ assert.ok(response.isError());
+ });
+ fakeXhr.respond(0, {}, null);
+ return promise;
+});
+
})();