Fixing incorrect scoping of the createRequesterFn function (#1413)

This commit is contained in:
Justin Dalrymple 2020-12-28 12:46:25 -05:00 committed by GitHub
parent 28fc40a241
commit cb340693c2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 70 additions and 12 deletions

View File

@ -55,8 +55,8 @@ export interface CommitAction {
}
export interface GPGSignature {
signature_type: "PGP",
verification_status: "verified"|"unverified";
signature_type: 'PGP';
verification_status: 'verified' | 'unverified';
gpg_key_id: number;
gpg_key_primary_keyid: string;
gpg_key_user_name: string;
@ -66,8 +66,8 @@ export interface GPGSignature {
}
export interface X509Signature {
signature_type: "X509";
verification_status: "verified"|"unverified",
signature_type: 'X509';
verification_status: 'verified' | 'unverified';
x509_certificate: {
id: number;
subject: string;
@ -190,7 +190,11 @@ export class Commits extends BaseService {
);
}
signature(projectId: string | number, sha: string, options?: BaseRequestOptions): Promise<CommitSignature> {
signature(
projectId: string | number,
sha: string,
options?: BaseRequestOptions,
): Promise<CommitSignature> {
const pId = encodeURIComponent(projectId);
return RequestHelper.get(

View File

@ -78,13 +78,13 @@ export function createRequesterFn(
optionsHandler,
requestHandler,
): (serviceOptions: DefaultServiceOptions) => RequesterType {
const requester: RequesterType = {} as RequesterType;
const methods = ['get', 'post', 'put', 'delete', 'stream'];
return (serviceOptions) => {
const requester: RequesterType = {} as RequesterType;
methods.forEach((m) => {
/* eslint func-names:0 */
requester[m] = function (endpoint: string, options: Record<string, unknown>) {
requester[m] = (endpoint: string, options: Record<string, unknown>) => {
const requestOptions = optionsHandler(serviceOptions, { ...options, method: m });
return requestHandler(endpoint, requestOptions);
@ -110,6 +110,7 @@ export function modifyServices<T>(services: T, customConfig: Record<string, unkn
Object.entries(services).forEach(([k, s]) => {
updated[k] = extendClass(s, customConfig);
if (k === 'GroupMembers') console.log(updated[k]);
});
return updated as T;

View File

@ -1,6 +1,11 @@
/* eslint-disable max-classes-per-file */
import FormData from 'form-data';
import { createRequesterFn, defaultOptionsHandler, modifyServices } from '../../src/RequesterUtils';
import {
createRequesterFn,
defaultOptionsHandler,
modifyServices,
formatQuery,
} from '../../src/RequesterUtils';
const methods = ['get', 'put', 'delete', 'stream', 'post'];
@ -53,6 +58,12 @@ describe('defaultOptionsHandler', () => {
expect(headers.sudo).toBe('testsudo');
});
it('should assign the prefixUrl property if passed', async () => {
const { prefixUrl } = defaultOptionsHandler(serviceOptions);
expect(prefixUrl).toBe('testurl');
});
it('should default searchParams to an empty string if undefined', async () => {
const { searchParams } = defaultOptionsHandler(serviceOptions, {
query: undefined,
@ -88,11 +99,11 @@ describe('createInstance', () => {
requestTimeout: 50,
};
it('should have a createInstance function', async () => {
it('should have a createInstance function', () => {
expect(createRequesterFn).toBeFunction();
});
it('should return an object with function names equal to those in the methods array when the createInstance function is called', async () => {
it('should return an object with function names equal to those in the methods array when the createInstance function is called', () => {
const requester = createRequesterFn(optionsHandler, handler)(serviceOptions);
expect(requester).toContainAllKeys(methods);
@ -102,16 +113,44 @@ describe('createInstance', () => {
});
});
it('should call the handler with the correct endpoint when passed to any of the method functions', async () => {
it('should call the handler with the correct endpoint when passed to any of the method functions', () => {
const testEndpoint = 'test endpoint';
const requester = createRequesterFn(optionsHandler, handler)(serviceOptions);
methods.forEach((m) => {
requester[m](testEndpoint, {});
expect(optionsHandler).toBeCalledWith(serviceOptions, { method: m });
expect(handler).toBeCalledWith(testEndpoint, {});
});
});
it('should respect the closure variables', () => {
const serviceOptions1 = {
headers: { test: '5' },
url: 'testurl',
rejectUnauthorized: false,
requestTimeout: 50,
};
const serviceOptions2 = {
headers: { test: '5' },
url: 'testurl2',
rejectUnauthorized: true,
requestTimeout: 100,
};
const requesterFn = createRequesterFn(optionsHandler, handler);
const requesterA = requesterFn(serviceOptions1);
const requesterB = requesterFn(serviceOptions2);
requesterA.get('test');
expect(optionsHandler).toBeCalledWith(serviceOptions1, { method: 'get' });
requesterB.get('test');
expect(optionsHandler).toBeCalledWith(serviceOptions2, { method: 'get' });
});
});
describe('modifyServices', () => {
@ -205,3 +244,17 @@ describe('modifyServices', () => {
expect(b.x).toBe(5);
});
});
describe('formatQuery', () => {
it('should decamelize keys and stringify the object', async () => {
const string = formatQuery({ test: 6 });
expect(string).toBe('test=6');
});
it('should decamelize sub keys in not property and stringify the object', async () => {
const string = formatQuery({ test: 6, not: { test: 7 } });
expect(string).toBe('not=%7B%22test%22%3A7%7D&test=6');
});
});