From 9e29808ab06cdcfddab6d3f7d9f1f3299dd977d9 Mon Sep 17 00:00:00 2001 From: Viachaslau Tyshkavets Date: Fri, 15 Apr 2022 16:07:56 +0400 Subject: [PATCH] test(repeater): silent logging in request runner unit tests relates-to #22 --- .../protocols/HttpRequestRunner.spec.ts | 6 +++++- .../protocols/HttpRequestRunner.ts | 17 +++++++++++------ .../protocols/WsRequestRunner.spec.ts | 3 ++- .../request-runner/protocols/WsRequestRunner.ts | 15 ++++++++++----- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/repeater/src/request-runner/protocols/HttpRequestRunner.spec.ts b/packages/repeater/src/request-runner/protocols/HttpRequestRunner.spec.ts index 26e4fed1..4fe7e042 100644 --- a/packages/repeater/src/request-runner/protocols/HttpRequestRunner.spec.ts +++ b/packages/repeater/src/request-runner/protocols/HttpRequestRunner.spec.ts @@ -5,6 +5,7 @@ import { Protocol } from '../../models'; import nock from 'nock'; import 'reflect-metadata'; import { anything, reset, spy, verify, when } from 'ts-mockito'; +import { Logger, LogLevel } from '@secbox/core'; const createRequest = (options?: Partial) => { const requestOptions = { url: 'https://foo.bar', headers: {}, ...options }; @@ -22,7 +23,10 @@ describe('HttpRequestRunner', () => { let runner!: HttpRequestRunner; beforeEach(() => { - runner = new HttpRequestRunner(executorOptions); + runner = new HttpRequestRunner( + executorOptions, + new Logger(LogLevel.SILENT) + ); }); afterEach(() => reset(spiedExecutorOptions)); diff --git a/packages/repeater/src/request-runner/protocols/HttpRequestRunner.ts b/packages/repeater/src/request-runner/protocols/HttpRequestRunner.ts index 61ce9ec1..aef667b4 100644 --- a/packages/repeater/src/request-runner/protocols/HttpRequestRunner.ts +++ b/packages/repeater/src/request-runner/protocols/HttpRequestRunner.ts @@ -3,7 +3,7 @@ import { RequestRunner } from '../RequestRunner'; import { RequestRunnerOptions } from '../RequestRunnerOptions'; import { Response } from '../Response'; import { Protocol } from '../../models'; -import { logger } from '@secbox/core'; +import { Logger } from '@secbox/core'; import request from 'request-promise'; import { Response as IncomingResponse } from 'request'; import { SocksProxyAgent } from 'socks-proxy-agent'; @@ -26,7 +26,9 @@ export class HttpRequestRunner implements RequestRunner { constructor( @inject(RequestRunnerOptions) - private readonly options: RequestRunnerOptions + private readonly options: RequestRunnerOptions, + @inject(Logger) + private readonly logger?: Logger ) { if (this.options.proxyUrl) { this.proxy = new SocksProxyAgent({ @@ -57,7 +59,10 @@ export class HttpRequestRunner implements RequestRunner { options.setHeaders(this.options.headers); } - logger.debug('Executing HTTP request with following params: %j', options); + this.logger?.debug( + 'Executing HTTP request with following params: %j', + options + ); const response = await this.request(options); @@ -82,12 +87,12 @@ export class HttpRequestRunner implements RequestRunner { const message = err.cause?.message ?? err.message; const errorCode = err.cause?.code ?? err.error?.syscall ?? err.name; - logger.error( + this.logger?.error( 'Error executing request: "%s %s HTTP/1.1"', options.method, options.url ); - logger.error('Cause: %s', message); + this.logger?.error('Cause: %s', message); return new Response({ protocol: this.protocol, @@ -180,7 +185,7 @@ export class HttpRequestRunner implements RequestRunner { } if (truncated) { - logger.debug( + this.logger?.debug( 'Truncate original response body to %i bytes', options.maxBodySize ); diff --git a/packages/repeater/src/request-runner/protocols/WsRequestRunner.spec.ts b/packages/repeater/src/request-runner/protocols/WsRequestRunner.spec.ts index bb9df82a..e58c8887 100644 --- a/packages/repeater/src/request-runner/protocols/WsRequestRunner.spec.ts +++ b/packages/repeater/src/request-runner/protocols/WsRequestRunner.spec.ts @@ -5,6 +5,7 @@ import { Protocol } from '../../models'; import 'reflect-metadata'; import { reset, spy, when } from 'ts-mockito'; import { Server } from 'ws'; +import { Logger, LogLevel } from '@secbox/core'; import { once } from 'events'; describe('WsRequestRunner', () => { @@ -16,7 +17,7 @@ describe('WsRequestRunner', () => { beforeEach(() => { // ADHOC: ts-mockito resets object's property descriptor as well Object.assign(executorOptions, { timeout: 2000 }); - runner = new WsRequestRunner(executorOptions); + runner = new WsRequestRunner(executorOptions, new Logger(LogLevel.SILENT)); }); afterEach(() => reset(spiedExecutorOptions)); diff --git a/packages/repeater/src/request-runner/protocols/WsRequestRunner.ts b/packages/repeater/src/request-runner/protocols/WsRequestRunner.ts index 59eb186a..256c816a 100644 --- a/packages/repeater/src/request-runner/protocols/WsRequestRunner.ts +++ b/packages/repeater/src/request-runner/protocols/WsRequestRunner.ts @@ -3,7 +3,7 @@ import { RequestRunner } from '../RequestRunner'; import { RequestRunnerOptions } from '../RequestRunnerOptions'; import { Response } from '../Response'; import { Protocol } from '../../models'; -import { logger } from '@secbox/core'; +import { Logger } from '@secbox/core'; import WebSocket from 'ws'; import { inject, injectable } from 'tsyringe'; import { SocksProxyAgent } from 'socks-proxy-agent'; @@ -28,7 +28,9 @@ export class WsRequestRunner implements RequestRunner { constructor( @inject(RequestRunnerOptions) - private readonly options: RequestRunnerOptions + private readonly options: RequestRunnerOptions, + @inject(Logger) + private readonly logger?: Logger ) { this.agent = this.options.proxyUrl ? new SocksProxyAgent({ @@ -46,7 +48,10 @@ export class WsRequestRunner implements RequestRunner { let client: WebSocket | null = null; try { - logger.debug('Executing HTTP request with following params: %j', options); + this.logger?.debug( + 'Executing HTTP request with following params: %j', + options + ); client = new WebSocket(options.url, { agent: this.agent, @@ -74,8 +79,8 @@ export class WsRequestRunner implements RequestRunner { const message = err.info ?? err.message; const errorCode = err.code ?? err.syscall; - logger.error('Error executing request: %s', options.url); - logger.error('Cause: %s', message); + this.logger?.error('Error executing request: %s', options.url); + this.logger?.error('Cause: %s', message); return new Response({ message,