Skip to content

Commit

Permalink
Implement response/error specs
Browse files Browse the repository at this point in the history
  • Loading branch information
hg-pyun committed Jun 28, 2019
1 parent 2f2dcfd commit 277aed3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/string-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(' ');
}
Expand Down
19 changes: 19 additions & 0 deletions src/logger/__test__/error.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -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', () => {
Expand Down
17 changes: 16 additions & 1 deletion src/logger/__test__/response.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '',
Expand All @@ -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', () => {
Expand Down
13 changes: 13 additions & 0 deletions src/logger/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 5 additions & 1 deletion src/logger/response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 277aed3

Please sign in to comment.