Skip to content

Commit

Permalink
test(repeater): cover proxy and reuse connection cases for `HttpReque…
Browse files Browse the repository at this point in the history
…stRunner`

relates-to #22
  • Loading branch information
pmstss committed Apr 16, 2022
1 parent cec2ae1 commit d8c7e14
Showing 1 changed file with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import nock from 'nock';
import 'reflect-metadata';
import { anything, spy, verify } from 'ts-mockito';
import { Logger, LogLevel } from '@secbox/core';
import { SocksProxyAgent } from 'socks-proxy-agent';

const createRequest = (options?: Partial<RequestOptions>) => {
const requestOptions = {
Expand Down Expand Up @@ -33,6 +34,10 @@ describe('HttpRequestRunner', () => {
});

describe('run', () => {
afterEach(() => {
nock.cleanAll();
});

// eslint-disable-next-line jest/expect-expect
it('should call setHeaders on the provided request if additional headers were configured globally', async () => {
const headers = { testHeader: 'test-header-value' };
Expand Down Expand Up @@ -141,10 +146,37 @@ describe('HttpRequestRunner', () => {
const { request, requestOptions } = createRequest();
const bigBody = 'x'.repeat(1025);
nock(requestOptions.url).get('/').reply(204, bigBody);

const response = await runner.run(request);

expect(response.body).toBeUndefined();
});

it('should use SocksProxyAgent if socks proxyUrl provided', async () => {
setupRunner({
proxyUrl: 'socks://proxy.baz'
});
const { request, requestOptions } = createRequest();
const scope = nock(requestOptions.url).get('/').reply(200, 'Dummy');

scope.on('request', req =>
expect(req.options.agent).toBeInstanceOf(SocksProxyAgent)
);

await runner.run(request);
});

it('should use keepAlive agent on if reuseConnection enabled', async () => {
setupRunner({
reuseConnection: true
});
const { request, requestOptions } = createRequest();
const scope = nock(requestOptions.url).get('/').reply(200, 'Dummy');

scope.on('request', req => {
expect(req.options.agent.options.keepAlive).toBeTruthy();
});

await runner.run(request);
});
});
});

0 comments on commit d8c7e14

Please sign in to comment.