Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
latitude: 35,
longitude: -105
};
// Success Response
feltResponseSucces = {
ciim_mapLat: '35',
ciim_mapLon: '-105',
ciim_time: 'five minutes ago',
eventid: '1234abcd',
form_version: '1.2.3',
your_cdi: 'I'
};
// Error Response
feltResponseError = new HttpErrorResponse({
status: 500,
statusText: 'Server Error'
});
it('handles successes', inject(
[FormSubmitService],
(service: FormSubmitService) => {
service.onSubmit(feltReport);
const req = httpClient.expectOne(service.responseUrl);
req.flush(feltResponseSucces);
service.formResponse$.subscribe(response => {
expect(response).toEqual(feltResponseSucces);
});
}
));
() => {
const error: HttpErrorResponse = new HttpErrorResponse({ status: HttpStatusCode.UNAUTHORIZED, statusText: 'Server Error', url: url });
httpClient.get(url).subscribe(() => { }, () => {
// error
expect(loaderServiceSpy.start).toHaveBeenCalledTimes(1);
expect(loaderServiceSpy.stopAll).toHaveBeenCalledTimes(1);
});
httpTestingController.expectOne(url).flush({}, error);
});
});
status = 204;
statusText = statusText || 'No Content';
}
else {
statusText = statusText || 'OK';
}
}
if (statusText === undefined) {
throw new Error('statusText is required when setting a custom status.');
}
if (status >= 200 && status < 300) {
this.observer.next(new HttpResponse({ body, headers, status, statusText, url }));
this.observer.complete();
}
else {
this.observer.error(new HttpErrorResponse({ error: body, headers, status, statusText, url }));
}
}
/**
private handleError(httpError: HttpErrorResponse, req: HttpRequest) {
const msg = `${req.method} ${req.urlWithParams} ${httpError.status} (${
httpError.statusText
})`;
if (httpError instanceof HttpErrorResponse) {
const errorObj = httpError.error === 'object' ? httpError.error : {};
errorObj.message = httpError.error.message || httpError.statusText;
errorObj.caught = false;
console.error(msg, '\n', errorObj.message, '\n\n', httpError);
this.httpError = new HttpErrorResponse({
error: errorObj,
headers: httpError.headers,
status: httpError.status,
statusText: httpError.statusText,
url: httpError.url
});
}
return throwError(this.httpError);
}
it('should reflect the Error that occured', () => {
const error = new HttpErrorResponse({});
const action = actionStockMarketRetrieveError({ error: error });
const state = stockMarketReducer(originalState, action);
expect(state.symbol).toBe(state.symbol);
expect(state.loading).toBeFalsy();
expect(state.stock).toBeNull();
expect(state.error).toBe(error);
});
});
it('should not be able to get all the process definitions if error occurred', () => {
const errorResponse = new HttpErrorResponse({
error: 'Mock Error',
status: 404, statusText: 'Not Found'
});
spyOn(service, 'getProcessDefinitions').and.returnValue(throwError(errorResponse));
service.getProcessDefinitions('appName1')
.subscribe(
() => {
fail('expected an error, not applications');
},
(error) => {
expect(error.status).toEqual(404);
expect(error.statusText).toEqual('Not Found');
expect(error.error).toEqual('Mock Error');
}
);
});
export function responseBody(
body: T,
status = 200,
error?: { code: number; message: string }
): Observable {
if (status >= 200 && status < 300) {
return of(body).pipe(delay(environment.httpDelay));
} else {
return _throw(new HttpErrorResponse({ status, error })).pipe(
materialize(),
delay(environment.httpDelay),
dematerialize()
);
}
}
beforeEach(() => {
store.setState(
createState({
symbol: 'TDD',
loading: false,
error: new HttpErrorResponse({})
})
);
fixture.detectChanges();
});
intercept(req: HttpRequest, next: HttpHandler): Observable> {
const cachedHttpEvent = this.transferState.get | HttpErrorResponse | undefined>(this.createCacheKey(req), undefined)
if (cachedHttpEvent) {
if (cachedHttpEvent.status < 200 || cachedHttpEvent.status >= 300) {
const err = cachedHttpEvent as HttpErrorResponse
const cachedErrorResponse = new HttpErrorResponse({
status: err.status,
statusText: err.statusText,
error: err.error
})
return Observable.throw(cachedErrorResponse)
} else {
const response = cachedHttpEvent as HttpResponse
const cachedResponse = new HttpResponse({
body: response.body,
status: response.status,
statusText: response.statusText
})
return Observable.of(cachedResponse)
}
}