From 4d4fa32384911daab9f4018858809736a867bb8a Mon Sep 17 00:00:00 2001 From: Ben Lesh Date: Tue, 5 Jan 2016 13:15:14 -0800 Subject: [PATCH] fix(ajax): should no longer succeed on 300 status --- spec/observables/dom/ajax-spec.js | 51 +++++++++++++++++++++++-------- src/observable/dom/ajax.ts | 2 +- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/spec/observables/dom/ajax-spec.js b/spec/observables/dom/ajax-spec.js index 72c07b98f2..c9d5989291 100644 --- a/spec/observables/dom/ajax-spec.js +++ b/spec/observables/dom/ajax-spec.js @@ -5,15 +5,15 @@ var Observable = Rx.Observable; function noop() { } describe('Observable.ajax', function () { - beforeEach(function() { + beforeEach(function () { jasmine.Ajax.install(); }); - afterEach(function() { + afterEach(function () { jasmine.Ajax.uninstall(); }); - it('should succeed', function() { + it('should succeed on 200', function () { var expected = { foo: 'bar' }; var doneFn = jasmine.createSpy("success"); @@ -38,18 +38,18 @@ describe('Observable.ajax', function () { expect(doneFn).toHaveBeenCalledWith(expected); }); - it('should fail', function() { + it('should fail on 404', function () { var expected = JSON.stringify({ foo: 'bar' }); var errorFn = jasmine.createSpy("success"); Rx.Observable .ajax({ url: '/flibbertyJibbet', - normalizeError: function(e, xhr, type) { + normalizeError: function (e, xhr, type) { return xhr.response || xhr.responseText; } }) - .subscribe(function() {}, errorFn, function () { + .subscribe(function () {}, errorFn, function () { throw 'should not have been called'; }); @@ -63,14 +63,41 @@ describe('Observable.ajax', function () { }); expect(errorFn).toHaveBeenCalledWith(expected); - }) + }); + + it('should fail on 300', function () { + var expected = JSON.stringify({ foo: 'bar' }); + var errorFn = jasmine.createSpy("success"); + + Rx.Observable + .ajax({ + url: '/flibbertyJibbet', + normalizeError: function (e, xhr, type) { + return xhr.response || xhr.responseText; + } + }) + .subscribe(function () {}, errorFn, function () { + throw 'should not have been called'; + }); + + expect(jasmine.Ajax.requests.mostRecent().url).toBe('/flibbertyJibbet'); + expect(errorFn).not.toHaveBeenCalled(); + + jasmine.Ajax.requests.mostRecent().respondWith({ + 'status': 300, + 'contentType': 'text/plain', + 'responseText': expected + }); + + expect(errorFn).toHaveBeenCalledWith(expected); + }); - it('should succeed no settings', function() { + it('should succeed no settings', function () { var expected = JSON.stringify({ foo: 'bar' }); Rx.Observable .ajax('/flibbertyJibbet') - .subscribe(function(x) { + .subscribe(function (x) { expect(x.status).toBe(200); expect(x.xhr.method).toBe('GET'); expect(x.xhr.responseText).toBe(expected); @@ -86,14 +113,14 @@ describe('Observable.ajax', function () { }); }); - it('should fail no settings', function() { + it('should fail no settings', function () { var expected = JSON.stringify({ foo: 'bar' }); Rx.Observable .ajax('/flibbertyJibbet') - .subscribe(function() { + .subscribe(function () { throw 'should not have been called'; - }, function(x) { + }, function (x) { expect(x.status).toBe(500); expect(x.xhr.method).toBe('GET'); expect(x.xhr.responseText).toBe(expected); diff --git a/src/observable/dom/ajax.ts b/src/observable/dom/ajax.ts index c680dd1da4..2edbb71727 100644 --- a/src/observable/dom/ajax.ts +++ b/src/observable/dom/ajax.ts @@ -209,7 +209,7 @@ export class AjaxObservable extends Observable { function processResponse(xhr, e) { done = true; const status: any = xhr.status === 1223 ? 204 : xhr.status; - if ((status >= 200 && status <= 300) || (status === 0) || (status === '')) { + if ((status >= 200 && status < 300) || (status === 0) || (status === '')) { if (emitType === 'json') { subscriber.next(normalizeSuccess(e, xhr, settings).response); } else {