Skip to content

Commit

Permalink
test(repeater): silent logging in request runner unit tests
Browse files Browse the repository at this point in the history
relates-to #22
  • Loading branch information
pmstss committed Apr 15, 2022
1 parent da60f3c commit 9e29808
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RequestOptions>) => {
const requestOptions = { url: 'https://foo.bar', headers: {}, ...options };
Expand All @@ -22,7 +23,10 @@ describe('HttpRequestRunner', () => {
let runner!: HttpRequestRunner;

beforeEach(() => {
runner = new HttpRequestRunner(executorOptions);
runner = new HttpRequestRunner(
executorOptions,
new Logger(LogLevel.SILENT)
);
});

afterEach(() => reset<RequestRunnerOptions>(spiedExecutorOptions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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({
Expand Down Expand Up @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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<RequestRunnerOptions>(spiedExecutorOptions));
Expand Down
15 changes: 10 additions & 5 deletions packages/repeater/src/request-runner/protocols/WsRequestRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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({
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9e29808

Please sign in to comment.