Skip to content

Commit

Permalink
fix(ajax): should no longer succeed on 300 status
Browse files Browse the repository at this point in the history
  • Loading branch information
benlesh committed Jan 13, 2016
1 parent 30a11ee commit 4d4fa32
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
51 changes: 39 additions & 12 deletions spec/observables/dom/ajax-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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';
});

Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/observable/dom/ajax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ export class AjaxObservable<T> extends Observable<T> {
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 {
Expand Down

0 comments on commit 4d4fa32

Please sign in to comment.