Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating agent property based on new changes in Got #712

Merged
merged 2 commits into from
Apr 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions packages/gitbeaker-browser/src/KyRequester.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Ky from 'ky';
import { Agent } from 'https';
import {
Service,
DefaultRequestOptions,
Expand All @@ -20,6 +21,12 @@ function responseHeadersAsObject(response): Record<string, string> {
export function defaultRequest(service: Service, options: DefaultRequestOptions = {}) {
const opts = baseDefaultRequest(service, options);

if (service.url.includes('https')) {
opts.agent = new Agent({
rejectUnauthorized: service.rejectUnauthorized,
});
}

return { ...opts, headers: new Headers(service.headers as Record<string, string>) };
}

Expand Down
8 changes: 8 additions & 0 deletions packages/gitbeaker-browser/test/unit/KyRequester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ky from 'ky';
import fetch from 'node-fetch';
import { Agent } from 'https';
import { processBody, handler, defaultRequest } from '../../src/KyRequester';

// Set globals for testing purposes
Expand Down Expand Up @@ -173,4 +174,11 @@ describe('defaultRequest', () => {
expect(headers).toBeInstanceOf(Headers);
expect(body).toBe(JSON.stringify(testBody));
});

it('should assign the agent property if given https url', async () => {
const options = defaultRequest({ ...service, url: 'https://test.com' }, { method: 'post' });

expect(options.agent).toBeInstanceOf(Agent);
expect(options.agent.rejectUnauthorized).toBeFalsy();
});
});
9 changes: 9 additions & 0 deletions packages/gitbeaker-node/src/GotRequester.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Got from 'got';
import FormData from 'form-data';
import { decamelizeKeys } from 'xcase';
import { Agent } from 'https';
import {
Service,
DefaultRequestOptions,
Expand All @@ -20,6 +21,14 @@ export function defaultRequest(
delete options.body;
}

if (service.url.includes('https')) {
options.agent = {
https: new Agent({
rejectUnauthorized: service.rejectUnauthorized,
}),
};
}

return options;
}

Expand Down
8 changes: 8 additions & 0 deletions packages/gitbeaker-node/test/unit/GotRequester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import got from 'got';
import FormData from 'form-data';
import { Agent } from 'https';
import { processBody, handler, defaultRequest } from '../../src/GotRequester';

jest.mock('got');
Expand Down Expand Up @@ -129,4 +130,11 @@ describe('defaultRequest', () => {
expect(output2.body).toBeInstanceOf(FormData);
expect(output2.json).toBeUndefined();
});

it('should assign the agent property if given https url', async () => {
const options = defaultRequest({ ...service, url: 'https://test.com' }, { method: 'post' });

expect(options.agent.https).toBeInstanceOf(Agent);
expect(options.agent.https.rejectUnauthorized).toBeFalsy();
});
});
7 changes: 0 additions & 7 deletions packages/gitbeaker-requester-utils/src/RequesterUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import FormData from 'form-data';
import { decamelizeKeys } from 'xcase';
import { Agent } from 'https';
import { stringify } from 'query-string';

export interface RequesterType {
Expand Down Expand Up @@ -48,12 +47,6 @@ export function defaultRequest(
bod = body;
}

if (service.url.includes('https')) {
agent = new Agent({
rejectUnauthorized: service.rejectUnauthorized,
});
}

return {
agent,
headers,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import FormData from 'form-data';
import { Agent } from 'https';
import { createInstance, defaultRequest } from '../../src/RequesterUtils';

const methods = ['get', 'put', 'delete', 'stream', 'post'];
Expand Down Expand Up @@ -36,13 +35,6 @@ describe('defaultRequest', () => {
expect(options.agent).toBeUndefined();
});

it('should assign the agent property if given https url', async () => {
const options = defaultRequest({ ...service, url: 'https://test.com' }, { method: 'post' });

expect(options.agent).toBeInstanceOf(Agent);
expect(options.agent.rejectUnauthorized).toBeFalsy();
});

it('should not assign the sudo property if omitted', async () => {
const { headers } = defaultRequest(service, {
sudo: undefined,
Expand Down