Justin Dalrymple d9cd4c9a91 refactor: 💡 Migrated to a monorepo structure
Gitbeaker has been split up into 5 subpackages: gitbeaker-core, gitbeaker-node, gitbeaker-cli, gitbeaker-browser and gitbeaker-requester-utils.

gitbeaker-[node,cli,browser] are enviroment
specific sub packages. For example, if you want to use gitbeaker in a NodeJS environment, use gitbeaker-node. gitbeaker-core is where all the
base logic exists, and gitbeaker-requester-utils is a collection of utility functions for making custom requester libraries.

BREAKING CHANGE: 🧨 This migration requires users to import specific subpackages. For NodeJS
usage, that would be @gitbeaker/node.
2020-02-02 16:53:54 +01:00

100 lines
2.9 KiB
TypeScript

import FormData from 'form-data';
import { Agent } from 'https';
import { createInstance, defaultRequest } from '../../src/RequesterUtils';
const methods = ['get', 'put', 'delete', 'stream', 'post'];
describe('defaultRequest', () => {
const service = {
headers: { test: '5' },
url: 'testurl',
rejectUnauthorized: false,
requestTimeout: 50,
};
it('should stringify body if it isnt of type FormData', async () => {
const testBody = { test: 6 };
const { body, headers } = defaultRequest(service, {
method: 'post',
body: testBody,
});
expect(headers).toContainEntry(['content-type', 'application/json']);
expect(body).toBe(JSON.stringify(testBody));
});
it('should not stringify body if it of type FormData', async () => {
const testBody = new FormData();
const { body } = defaultRequest(service, { body: testBody, method: 'post' });
expect(body).toBeInstanceOf(FormData);
});
it('should not assign the agent property if given http url', async () => {
const options = defaultRequest(service, { method: 'post' });
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,
method: 'get',
});
expect(headers.sudo).toBeUndefined();
});
it('should default searchParams to an empty string if undefined', async () => {
const { searchParams } = defaultRequest(service, {
query: undefined,
method: 'get',
});
expect(searchParams).toBe('');
});
});
describe('createInstance', () => {
const handler = jest.fn();
const optionsHandler = jest.fn(() => ({}));
it('should have a createInstance function', async () => {
expect(createInstance).toBeFunction();
});
it('should return an object with function names equal to those in the methods array when the createInstance function is called', async () => {
const requester = createInstance(optionsHandler, handler);
expect(requester).toContainAllKeys(methods);
methods.forEach(m => {
expect(requester[m]).toBeFunction();
});
});
it('should call the handler with the correct endpoint when passed to any of the method functions', async () => {
const requester = createInstance(optionsHandler, handler);
const service = {
headers: { test: '5' },
url: 'testurl',
rejectUnauthorized: false,
requestTimeout: 50,
};
const testEndpoint = 'test endpoint';
methods.forEach(m => {
requester[m](service, testEndpoint, {});
expect(handler).toBeCalledWith(testEndpoint, {});
});
});
});