From 277aed3e60680db326b6652950905c06723213ba Mon Sep 17 00:00:00 2001 From: hg-pyun Date: Fri, 28 Jun 2019 18:54:17 +0900 Subject: [PATCH] Implement response/error specs --- src/common/string-builder.ts | 5 +++++ src/logger/__test__/error.spec.js | 19 +++++++++++++++++++ src/logger/__test__/response.spec.js | 17 ++++++++++++++++- src/logger/error.ts | 13 +++++++++++++ src/logger/response.ts | 6 +++++- 5 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/common/string-builder.ts b/src/common/string-builder.ts index 009fe91..d9ebac3 100644 --- a/src/common/string-builder.ts +++ b/src/common/string-builder.ts @@ -37,6 +37,11 @@ class StringBuilder { return this; } + makeStatus(status?:number, statusText?: string) { + if(status && statusText) this.printQueue.push(`${status}:${statusText}`); + return this; + } + build() { return this.printQueue.join(' '); } diff --git a/src/logger/__test__/error.spec.js b/src/logger/__test__/error.spec.js index 88e9dc3..1119b35 100644 --- a/src/logger/__test__/error.spec.js +++ b/src/logger/__test__/error.spec.js @@ -6,6 +6,16 @@ jest.mock('../../common/print'); const axiosError = { code: 500, + config: { + url: 'https://github.com/hg-pyun', + method: 'GET', + }, + response: { + data: 'dummy data', + status: 500, + statusText: 'internal server error', + headers: '', + }, }; beforeEach(() => { @@ -19,9 +29,18 @@ test('response should be return immutable axiosError', () => { }); test('if config is undefined, logger make default log', () => { + const { + config: { url, method }, + response: { data, status, statusText }, + } = axiosError; + errorLoggerWithoutPromise(axiosError); expect(printLog).toHaveBeenCalled(); expect(printLog).toBeCalledWith(expect.stringContaining('[Axios][Error]')); + expect(printLog).toBeCalledWith(expect.stringContaining(url)); + expect(printLog).toBeCalledWith(expect.stringContaining(method)); + expect(printLog).toBeCalledWith(expect.stringContaining(`${status}:${statusText}`)); + expect(printLog).toBeCalledWith(expect.stringContaining(data)); }); test('if global config is defined only, logger make log with options', () => { diff --git a/src/logger/__test__/response.spec.js b/src/logger/__test__/response.spec.js index 7e12835..e410644 100644 --- a/src/logger/__test__/response.spec.js +++ b/src/logger/__test__/response.spec.js @@ -4,7 +4,11 @@ import { printLog } from '../../common/print'; jest.mock('../../common/print'); const axiosResponse = { - data: 'data', + config: { + url: 'https://github.com/hg-pyun', + method: 'GET', + }, + data: 'dummy data', status: 500, statusText: 'internal server error', headers: '', @@ -21,9 +25,20 @@ test('response should be return immutable AxiosResponse', () => { }); test('if config is undefined, logger make default log', () => { + const { + status, + statusText, + data, + config: { url, method }, + } = axiosResponse; + responseLogger(axiosResponse); expect(printLog).toHaveBeenCalled(); expect(printLog).toBeCalledWith(expect.stringContaining('[Axios][Response]')); + expect(printLog).toBeCalledWith(expect.stringContaining(url)); + expect(printLog).toBeCalledWith(expect.stringContaining(method)); + expect(printLog).toBeCalledWith(expect.stringContaining(`${status}:${statusText}`)); + expect(printLog).toBeCalledWith(expect.stringContaining(data)); }); test('if global config is defined only, logger make log with options', () => { diff --git a/src/logger/error.ts b/src/logger/error.ts index 6e08ff6..8c82b5f 100644 --- a/src/logger/error.ts +++ b/src/logger/error.ts @@ -6,12 +6,25 @@ import { printLog } from '../common/print'; function errorLoggerWithoutPromise(error: AxiosError, config?: ErrorLogConfig) { + const {config: {url, method}, response} = error; + + let status, statusText, data; + if(response){ + status = response.status; + statusText = response.statusText; + data = response.data; + } + const buildConfig = config ? config : mergeWithGlobalConfig(config); const stringBuilder = new StringBuilder(buildConfig); const log = stringBuilder .makePrefix('Error') .makeDateFormat() + .makeUrl(url) + .makeMethod(method) + .makeStatus(status, statusText) + .makeData(data) .build(); printLog(log); diff --git a/src/logger/response.ts b/src/logger/response.ts index e8cea66..bab6033 100644 --- a/src/logger/response.ts +++ b/src/logger/response.ts @@ -5,13 +5,17 @@ import StringBuilder from '../common/string-builder'; import { printLog } from '../common/print'; function responseLogger(response: AxiosResponse, config?: ResponseLogConfig) { - + const {config: {url, method}, status, statusText, data} = response; const buildConfig = config ? config : mergeWithGlobalConfig(config); const stringBuilder = new StringBuilder(buildConfig); const log = stringBuilder .makePrefix('Response') .makeDateFormat() + .makeUrl(url) + .makeMethod(method) + .makeStatus(status, statusText) + .makeData(data) .build(); printLog(log);