test cases - response.handler + responseHeaders.factory
MODIFY
jest.config.js
jest.config.js
@@ -4,7 +4,6 @@ module.exports = {
},
testRegex: '(/__tests__/.*|\\.(test|spec))\\.(ts|tsx)$',
coverageDirectory: './coverage/',
- mapCoverage: true,
moduleFileExtensions: [
'ts',
'js',

MODIFY
src/http.interface.ts
src/http.interface.ts
@@ -19,11 +19,22 @@ export type Http = {
export enum HttpStatus {
OK = 200,
+ CREATED = 201,
+ ACCEPTED = 202,
+ NO_CONTENT = 204,
BAD_REQUEST = 400,
UNAUTHORIZED = 401,
FORBIDDEN = 403,
NOT_FOUND = 404,
+ METHOD_NOT_ALLOWED = 405,
+ NOT_ACCEPTABLE = 406,
+ REQUEST_TIMEOUT = 408,
+ CONFLICT = 409,
+ GONE = 410,
FAILED_DEPENDENCY = 424,
INTERNAL_SERVER_ERROR = 500,
+ NOT_IMPLEMENTED = 501,
+ BAD_GATEWAY = 502,
SERVICE_UNAVAILABLE = 503,
+ GATEWAY_TIMEOUT = 504,
}

ADD
src/response/response.handler.spec.ts
+import { HttpResponse, HttpStatus } from '../http.interface';
+import { handleResponse } from './response.handler';
+import { DEFAULT_HEADERS } from './responseHeaders.factory';
+
+const RESPONSE = {
+ writeHead: jest.fn(),
+ end: jest.fn(),
+} as any as HttpResponse;
+
+describe('Response handler', () => {
+
+ const handle = handleResponse(RESPONSE);
+
+ it('sets provided status', () => {
+ handle({ status: HttpStatus.FORBIDDEN });
+ expect(RESPONSE.writeHead).toHaveBeenCalledWith(HttpStatus.FORBIDDEN, DEFAULT_HEADERS);
+ });
+
+ it('sets status OK if is not explicitly set', () => {
+ handle({});
+ expect(RESPONSE.writeHead).toHaveBeenCalledWith(HttpStatus.OK, DEFAULT_HEADERS);
+ });
+
+ it('sets default headers if are not explicitly set', () => {
+ handle({});
+ expect(RESPONSE.writeHead).toHaveBeenCalledWith(HttpStatus.OK, DEFAULT_HEADERS);
+ });
+
+ it('sets provided headers', () => {
+ const CUSTOM_HEADERS = { 'Content-Encoding': 'gzip' };
+ handle({ headers: CUSTOM_HEADERS });
+ expect(RESPONSE.writeHead).toHaveBeenCalledWith(HttpStatus.OK, { ...DEFAULT_HEADERS, ...CUSTOM_HEADERS });
+ });
+
+ it('sets unefined response if body is not provided', () => {
+ handle({});
+ expect(RESPONSE.end).toHaveBeenCalledWith(undefined);
+ });
+
+ it('sets stringified response if object body is provided', () => {
+ handle({ body: { foo: 'bar' } });
+ expect(RESPONSE.end).toHaveBeenCalledWith(JSON.stringify({ foo: 'bar' }));
+ });
+
+});

ADD
src/response/responseHeaders.factory.spec.ts
+import { headersFactory } from './responseHeaders.factory';
+
+describe('Response headers factory', () => {
+
+ it('returns default headers if custom are not provided', () => {
+ const DEFAULT_HEADERS = { 'Content-Type': 'application/json' };
+ expect(headersFactory()).toEqual(DEFAULT_HEADERS);
+ });
+
+ it('returns custom headers if are provided', () => {
+ const DEFAULT_HEADERS = { 'Content-Type': 'application/json' };
+ const CUSTOM_HEADERS = { 'Content-Encoding': 'gzip' };
+ expect(headersFactory(CUSTOM_HEADERS)).toEqual({ ...DEFAULT_HEADERS, ...CUSTOM_HEADERS });
+ });
+
+ it('returns not duplicated headers', () => {
+ const DEFAULT_HEADERS = { 'Content-Type': 'application/json' };
+ const CUSTOM_HEADERS = { 'Content-Type': 'application/json' };
+ expect(headersFactory(CUSTOM_HEADERS)).toEqual({ ...DEFAULT_HEADERS });
+ });
+
+});

test cases - response.handler + responseHeaders.factory