Skip to content

Commit

Permalink
fix(common): send flushed body as error instead of null
Browse files Browse the repository at this point in the history
fix #18181
  • Loading branch information
jnizet authored and alxhub committed Jul 18, 2017
1 parent f19bd5f commit 17b7bc3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
14 changes: 12 additions & 2 deletions packages/common/http/test/client_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import 'rxjs/add/operator/toPromise';
import {ddescribe, describe, iit, it} from '@angular/core/testing/src/testing_internal';

import {HttpClient} from '../src/client';
import {HttpEventType, HttpResponse} from '../src/response';
import {HttpErrorResponse, HttpEventType, HttpResponse} from '../src/response';
import {HttpClientTestingBackend} from '../testing/src/backend';

export function main() {
Expand Down Expand Up @@ -123,5 +123,15 @@ export function main() {
.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'});
});
});
});
}
}
5 changes: 2 additions & 3 deletions packages/common/http/testing/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,11 @@ export class TestRequest {
if (statusText === undefined) {
throw new Error('statusText is required when setting a custom status.');
}
const res = {body, headers, status, statusText, url};
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();
} else {
this.observer.error(new HttpErrorResponse(res));
this.observer.error(new HttpErrorResponse({error: body, headers, status, statusText, url}));
}
}

Expand Down

0 comments on commit 17b7bc3

Please sign in to comment.