Skip to content

Commit

Permalink
fix(interceptor): allows for no data to be returned and not fail
Browse files Browse the repository at this point in the history
  • Loading branch information
jmcdo29 committed Jan 2, 2020
1 parent a575fe1 commit ab2857e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 100 deletions.
153 changes: 56 additions & 97 deletions src/ogma.interceptor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ const method = 'GET';
const url = '/';
const time = '50 ms';

function mockExecContext(code: number, type: string = 'http') {
return createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: jest.fn(),
getResponse: () => ({
statusCode: code,
}),
}),
getType: () => type,
});
}

describe.each([
{
format: 'dev' as 'dev',
Expand Down Expand Up @@ -222,93 +234,37 @@ describe('OgmaInterceptor with getRequest and getResponse options', () => {
});
describe('OgmaInterceptor shouldLog', () => {
const ogmaOptions = { ogma: { options: { color: true, json: false } } };
it('should not log based on the options', () => {
const interceptorOptions = {
it.each([
{
skip: jest.fn((req: any, res: any) => res.statusCode === 200),
};
const interceptor = new OgmaInterceptor(interceptorOptions, {
info: jest.fn(),
...ogmaOptions,
} as any);
expect(
interceptor.shouldLog(
createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: jest.fn(),
getResponse: () => ({
statusCode: 200,
}),
}),
getType: () => 'http',
}),
),
).toBe(false);
expect(interceptorOptions.skip).toBeCalledTimes(1);
});
it('should log based on the options', () => {
const interceptorOptions = {
context: mockExecContext(200),
log: false,
},
{
skip: jest.fn((req: any, res: any) => res.statusCode === 300),
};
const interceptor = new OgmaInterceptor(interceptorOptions, {
info: jest.fn(),
...ogmaOptions,
} as any);
expect(
interceptor.shouldLog(
createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: jest.fn(),
getResponse: () => ({
statusCode: 200,
}),
}),
getType: () => 'http',
}),
),
).toBe(true);
expect(interceptorOptions.skip).toBeCalledTimes(1);
});
it('should not log based on the context', () => {
context: mockExecContext(200),
log: true,
},
{
context: mockExecContext(200, 'ws'),
log: false,
},
{
context: mockExecContext(200),
log: true,
},
])('should try to log based on options %j', (options) => {
const interceptorOptions = {
skip: jest.fn((req: any, res: any) => res.statusCode === 300),
skip: options.skip,
};
const interceptor = new OgmaInterceptor(interceptorOptions, {
info: jest.fn(),
...ogmaOptions,
} as any);
expect(
interceptor.shouldLog(
createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: jest.fn(),
getResponse: () => ({
statusCode: 200,
}),
}),
getType: () => 'ws',
}),
),
).toBe(false);
});
it('should log based on lack of skip option', () => {
const interceptorOptions = {};
const interceptor = new OgmaInterceptor(interceptorOptions, {
info: jest.fn(),
...ogmaOptions,
} as any);
expect(
interceptor.shouldLog(
createMock<ExecutionContext>({
switchToHttp: () => ({
getRequest: jest.fn(),
getResponse: () => ({
statusCode: 200,
}),
}),
getType: () => 'http',
}),
),
).toBe(true);
expect(interceptor.shouldLog(options.context)).toBe(options.log);
if (options.skip) {
expect(options.skip).toBeCalledTimes(1);
}
});
});
describe('OgmaInterceptor testing intercept', () => {
Expand Down Expand Up @@ -362,23 +318,26 @@ describe('OgmaInterceptor testing intercept', () => {
});
},
);
it('should intercept the response and log it', (done) => {
const observer = Observable.create((obs: Observer<any>) => {
obs.next(data);
obs.complete();
return;
});
interceptor.intercept(context, { handle: () => observer }).subscribe({
next: (val) => {
expect(val).toEqual(data);
},
complete: () => {
if (!skipOption.skip) {
expect(ogmaOptions.info).toBeCalledTimes(1);
}
done();
},
});
});
it.each([undefined, data])(
'should intercept the response and log it',
(value: string | undefined, done: any) => {
const observer = Observable.create((obs: Observer<any>) => {
obs.next(value);
obs.complete();
return;
});
interceptor.intercept(context, { handle: () => observer }).subscribe({
next: (val) => {
expect(val).toEqual(value);
},
complete: () => {
if (!skipOption.skip) {
expect(ogmaOptions.info).toBeCalledTimes(1);
}
done();
},
});
},
);
});
});
2 changes: 1 addition & 1 deletion src/ogma.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class OgmaInterceptor implements NestInterceptor {
const callingHandler = context.getHandler().name;
const req = this.getRequest(context);
const res = this.getResponse(context);
data = JSON.stringify(data);
data = data ? JSON.stringify(data) : '';
let logString: string | object = '';
if (this.options.format === 'dev') {
logString = this.devContext(req, res, Buffer.from(data), startTime);
Expand Down
1 change: 0 additions & 1 deletion src/ogma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export class OgmaService implements LoggerService {
@Inject(OGMA_INSTANCE) ogma?: Ogma,
@Optional() @Inject(OGMA_CONTEXT) context?: string,
) {
// console.log(ogma);
this.context = context || '';
this.ogma = ogma ?? new Ogma();
}
Expand Down
2 changes: 1 addition & 1 deletion tslint.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": ["tslint:recommended", "tslint-config-prettier"],
"extends": ["tslint:recommended", "tslint-config-prettier", "tslint-sonarts"],
"rules": {
"arrow-return-shorthand": true,
"callable-types": true,
Expand Down

0 comments on commit ab2857e

Please sign in to comment.