Newer
Older
import { HttpStatusCode } from '@angular/common/http';

OZGCloud
committed
import { HttpMethod, isConnectionTimeout } from '..';
import { isChangingDataRequest, isForbidden, isServerError, isServiceUnavailable, isUnauthorized, isUnprocessableEntity } from './http.util';
describe('http util', () => {
const SERVER_ERROR_CODES: HttpStatusCode[] = [500, 501, 505, 506, 507, 508, 510, 511];
const CONNECTION_TIMEOUT_CODES: HttpStatusCode[] = [502, 503, 504];

OZGCloud
committed
const CLIENT_ERROR: HttpStatusCode[] = [400, 401, 402, 403, 404, 422];
describe('isForbidden', () => {

OZGCloud
committed
it.each(CLIENT_ERROR.slice(HttpStatusCode.Forbidden).concat(SERVER_ERROR_CODES))

OZGCloud
committed
('should be false on %i', (httpStatusCode: HttpStatusCode) => {
var result: boolean = isForbidden(httpStatusCode);
expect(result).toBeFalsy();
});
it('should be true on 403', () => {
var result: boolean = isForbidden(HttpStatusCode.Forbidden);
expect(result).toBeTruthy();
})
})
describe('isUnauthorized', () => {

OZGCloud
committed
it.each(CLIENT_ERROR.slice(HttpStatusCode.Unauthorized).concat(SERVER_ERROR_CODES))
('should be false on %i', (httpStatusCode: HttpStatusCode) => {
var result: boolean = isUnauthorized(httpStatusCode);
expect(result).toBeFalsy();
});
it('should be true on 401', () => {
var result: boolean = isUnauthorized(HttpStatusCode.Unauthorized);
expect(result).toBeTruthy();
});
})
describe('isServerError', () => {
it.each(SERVER_ERROR_CODES)
('should be true on %i', (httpStatusCode: HttpStatusCode) => {
var result: boolean = isServerError(httpStatusCode);
expect(result).toBeTruthy();
});

OZGCloud
committed
it.each(CLIENT_ERROR.concat(CONNECTION_TIMEOUT_CODES))
('should be false on %i', () => {

OZGCloud
committed
var result: boolean = isServerError(HttpStatusCode.Forbidden);
expect(result).toBeFalsy();
})
})
describe('isUnprocessableEntity', () => {

OZGCloud
committed
it.each(CLIENT_ERROR.slice(HttpStatusCode.UnprocessableEntity).concat(SERVER_ERROR_CODES).concat(SERVER_ERROR_CODES))

OZGCloud
committed
('should be false on %i', (httpStatusCode: HttpStatusCode) => {
var result: boolean = isUnprocessableEntity(httpStatusCode);
expect(result).toBeFalsy();
});

OZGCloud
committed
it('should be true on %i', () => {
var result: boolean = isUnprocessableEntity(HttpStatusCode.UnprocessableEntity);

OZGCloud
committed
expect(result).toBeTruthy();
})
describe('isChangingDataRequest', () => {

OZGCloud
committed
const CHANGING_DATA_REQUEST_METHOD: string[] = [HttpMethod.POST, HttpMethod.PUT, HttpMethod.PATCH, HttpMethod.DELETE];
it.each(CHANGING_DATA_REQUEST_METHOD)
('should return true on %i', (method: string) => {
var result: boolean = isChangingDataRequest(<any>{ method });
expect(result).toBeTruthy();
})

OZGCloud
committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
it('should return false on %i', () => {
var result: boolean = isChangingDataRequest(<any>{ method: HttpMethod.GET });
expect(result).toBeFalsy();
})
})
describe('isConnectionTimeout', () => {
it.each(CONNECTION_TIMEOUT_CODES)
('should return true on %i', (statusCode: number) => {
var result: boolean = isConnectionTimeout(statusCode);
expect(result).toBeTruthy();
})
it.each(SERVER_ERROR_CODES)
('should return false on %i', (statusCode: number) => {
var result: boolean = isConnectionTimeout(statusCode);
expect(result).toBeFalsy();
})
})
describe('isServiceUnavailable', () => {
it('should return true on %i', () => {
var result: boolean = isServiceUnavailable(HttpStatusCode.ServiceUnavailable);
expect(result).toBeTruthy();
})
it.each(SERVER_ERROR_CODES.concat(CONNECTION_TIMEOUT_CODES.slice(HttpStatusCode.ServiceUnavailable)))
('should return false on %i', (statusCode: number) => {
var result: boolean = isServiceUnavailable(statusCode);
expect(result).toBeFalsy();
})
})