Skip to content

Commit 2128932

Browse files
claudiordgzbenlesh
authored andcommitted
fix(AjaxObservable): 1xx,2xx,3xx requests shouldn't error, only 4xx,5xx (#3438)
w3 rfc2616 defines 4xx,5xx status codes as errors, but anything below is a valid response. AjaxObservable errors when a request status returns 1xx and 3xx, passing only 2xx requests. #3308
1 parent 4287424 commit 2128932

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed

spec/observables/dom/ajax-spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,9 @@ describe('Observable.ajax', () => {
262262
expect(error.status).to.equal(404);
263263
});
264264

265-
it('should fail on 404', () => {
266-
let error;
265+
it('should succeed on 300', () => {
266+
let result;
267+
let complete = false;
267268
const obj = {
268269
url: '/flibbertyJibbet',
269270
normalizeError: (e: any, xhr: any, type: any) => {
@@ -273,13 +274,12 @@ describe('Observable.ajax', () => {
273274
method: ''
274275
};
275276

276-
Rx.Observable.ajax(obj).subscribe(x => {
277-
throw 'should not next';
278-
}, (err: any) => {
279-
error = err;
280-
}, () => {
281-
throw 'should not complete';
282-
});
277+
Rx.Observable.ajax(obj)
278+
.subscribe((x: any) => {
279+
result = x;
280+
}, null, () => {
281+
complete = true;
282+
});
283283

284284
expect(MockXMLHttpRequest.mostRecent.url).to.equal('/flibbertyJibbet');
285285

@@ -289,9 +289,9 @@ describe('Observable.ajax', () => {
289289
'responseText': 'Wee! I am text!'
290290
});
291291

292-
expect(error instanceof Rx.AjaxError).to.be.true;
293-
expect(error.message).to.equal('ajax error 300');
294-
expect(error.status).to.equal(300);
292+
expect(result.xhr).exist;
293+
expect(result.response).to.deep.equal('Wee! I am text!');
294+
expect(complete).to.be.true;
295295
});
296296

297297
it('should succeed no settings', () => {

src/internal/observable/dom/AjaxObservable.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,8 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
365365
status = response ? 200 : 0;
366366
}
367367

368-
if (200 <= status && status < 300) {
368+
// 4xx and 5xx should error (https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html)
369+
if (status < 400) {
369370
if (progressSubscriber) {
370371
progressSubscriber.complete();
371372
}

0 commit comments

Comments
 (0)