diff --git a/src/lib/request/requestManager.ts b/src/lib/request/requestManager.ts index 64033a4..0f7aa15 100644 --- a/src/lib/request/requestManager.ts +++ b/src/lib/request/requestManager.ts @@ -37,9 +37,13 @@ interface RequestsManagerParams { function getRESTAPI(endpoint: string): string { // e.g 'https://api.snyk.io/rest/' - const apiData = new URL(endpoint.replace('app.', '')); - - return new URL(`${apiData.protocol}//api.${apiData.host}/rest`).toString(); + const apiData = new URL(endpoint); + if (!apiData.host.startsWith('api.') && process.env.NODE_ENV != 'test') { + console.warn( + `${apiData.host} seems invalid and should look like https://api.snyk.io or https://api..snyk.io.`, + ); + } + return new URL(`${apiData.protocol}//${apiData.host}/rest`).toString(); } const getConfig = (): { endpoint: string; token: string } => { diff --git a/test/lib/request/request-proxy.test.ts b/test/lib/request/request-proxy.test.ts new file mode 100644 index 0000000..21d1a47 --- /dev/null +++ b/test/lib/request/request-proxy.test.ts @@ -0,0 +1,34 @@ +import { makeSnykRequest } from '../../../src/lib/request/request'; +import * as fs from 'fs'; +import * as _ from 'lodash'; +import * as path from 'path'; +import { + NotFoundError, + ApiError, + ApiAuthenticationError, + GenericError, +} from '../../../src/lib/customErrors/apiError'; + +const fixturesFolderPath = path.resolve(__dirname, '../..') + '/fixtures/'; + +const OLD_ENV = process.env; +beforeEach(() => { + jest.resetModules(); // this is important - it clears the cache + process.env = { ...OLD_ENV }; + delete process.env.SNYK_TOKEN; +}); + +afterEach(() => { + process.env = OLD_ENV; +}); + +describe('Test Snyk Utils make request properly', () => { + it('Test GET command on /', async () => { + const response = await makeSnykRequest( + { verb: 'GET', url: '/user/me' }, + process.env.SNYK_TOKEN, + ); + + expect(response.status).toEqual(200); + }); +});