fix(common): send flushed body as error instead of null

fix #18181
This commit is contained in:
jnizet 2017-07-17 21:16:27 +02:00 committed by Alex Rickabaugh
parent 256bc8acdd
commit 5c62e300e1
2 changed files with 14 additions and 5 deletions

View File

@ -12,7 +12,7 @@ import 'rxjs/add/operator/toPromise';
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal'; import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';
import {HttpClient} from '../src/client'; import {HttpClient} from '../src/client';
import {HttpEventType, HttpResponse} from '../src/response'; import {HttpErrorResponse, HttpEventType, HttpResponse} from '../src/response';
import {HttpClientTestingBackend} from '../testing/src/backend'; import {HttpClientTestingBackend} from '../testing/src/backend';
export function main() { export function main() {
@ -123,5 +123,15 @@ export function main() {
.flush('hello world'); .flush('hello world');
}); });
}); });
describe('makes a request for an error response', () => {
it('with a JSON body', (done: DoneFn) => {
client.get('/test').subscribe(() => {}, (res: HttpErrorResponse) => {
expect(res.error.data).toEqual('hello world');
done();
});
backend.expectOne('/test').flush(
{'data': 'hello world'}, {status: 500, statusText: 'Server error'});
});
});
}); });
} }

View File

@ -61,12 +61,11 @@ export class TestRequest {
if (statusText === undefined) { if (statusText === undefined) {
throw new Error('statusText is required when setting a custom status.'); throw new Error('statusText is required when setting a custom status.');
} }
const res = {body, headers, status, statusText, url};
if (status >= 200 && status < 300) { if (status >= 200 && status < 300) {
this.observer.next(new HttpResponse<any>(res)); this.observer.next(new HttpResponse<any>({body, headers, status, statusText, url}));
this.observer.complete(); this.observer.complete();
} else { } else {
this.observer.error(new HttpErrorResponse(res)); this.observer.error(new HttpErrorResponse({error: body, headers, status, statusText, url}));
} }
} }