In the code snippet below, I am attempting to find a custom header on error:
login(credentials: Credentials): Observable<any> {
return this.http.post(loginUrl, credentials)
.pipe(
catchError((httpErrorResponse: HttpErrorResponse) => {
let error = new Error('', httpErrorResponse.status);
...
if (httpErrorResponse.headers.get('customHeaderName')) {
error.message = 'Appropriate Response';
}
...
return throwError(error);
})
);
}
While using HttpClientTestingModule
, I am trying to test the above code with the following test case:
it('should catch error with custom header', (done) => {
authService.login(credentials)
.subscribe({
next: null,
error: (error: Error) => {
expect(error.message).toEqual('Appropriate Response');
}
});
httpMock.expectOne({
url: apiUrl,
method: 'POST'
}).flush([], {status: 403, statusText: 'Forbidden', headers: {'customHeaderName': 'customHeaderValue'}});
done();
});
One issue I encountered is that while status
and statusText
are as expected, the headers are not included, so the if block is not activated.
Any thoughts on this?