Can you tell me how to test the HttpInterceptor provided by the Angular 4. I have created an interceptor as per the examples but not sure how to test it. Below is my interceptor and I want to test if the custom headers are added and when response status is 401 window.location.href
is done.
export class MyInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const headers = new HttpHeaders();
this.addHeader(headers); // This will add headers
const changedReq = req.clone({ headers: headers });
return next.handle(req)
.catch(err => {
if (err instanceof HttpErrorResponse) {
switch (err.status) {
case 302:
case 401:
window.location.href = "http//google.com";
break;
default:
throw new Error(this.getErrorMessage(err));
}
}
return Observable.throw(err);
});
}