Skip to content

Commit

Permalink
Merge pull request #4621 from snyk/fix/stream-snyk-iac-test
Browse files Browse the repository at this point in the history
Download snyk-iac-test using streamRequest()
  • Loading branch information
francescomari authored May 25, 2023
2 parents b6d2aab + 69bf8aa commit 6989e20
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 35 deletions.
32 changes: 25 additions & 7 deletions src/lib/iac/test/v2/local-cache/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import * as createDebugLogger from 'debug';
import * as path from 'path';

import { CustomError } from '../../../../errors';
import { makeRequest } from '../../../../request';
import { streamRequest } from '../../../../request/request';
import { ReadableStream } from 'needle';

const debugLogger = createDebugLogger('snyk-iac');

Expand Down Expand Up @@ -41,13 +42,30 @@ export class InvalidUserPathError extends CustomError {
}

export async function fetchCacheResource(url: string): Promise<Buffer> {
const { res, body: cacheResourceBuffer } = await makeRequest({
url,
const stream = await streamRequest({
body: null,
headers: {},
method: 'get',
url: url,
});

if (res.statusCode !== 200) {
throw new CustomError(`Failed to download cache resource from ${url}`);
}
return streamToBuffer(stream);
}

async function streamToBuffer(stream: ReadableStream): Promise<Buffer> {
return new Promise((resolve, reject) => {
const chunks: Buffer[] = [];

return cacheResourceBuffer;
stream.on('data', (chunk) => {
chunks.push(chunk);
});

stream.on('end', () => {
resolve(Buffer.concat(chunks));
});

stream.on('error', (err) => {
reject(err);
});
});
}
35 changes: 7 additions & 28 deletions test/jest/unit/lib/iac/test/v2/setup/local-cache/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import * as pathLib from 'path';
import * as cloneDeep from 'lodash.clonedeep';

import * as requestLib from '../../../../../../../../../src/lib/request';
import * as requestLib from '../../../../../../../../../src/lib/request/request';
import {
fetchCacheResource,
InvalidUserPathError,
lookupLocal,
} from '../../../../../../../../../src/lib/iac/test/v2/local-cache/utils';
import { CustomError } from '../../../../../../../../../src/lib/errors';
import { Readable } from 'stream';

describe('lookupLocal', () => {
const iacCachePath = pathLib.join('iac', 'cache', 'path');
Expand Down Expand Up @@ -115,21 +115,18 @@ describe('fetchCacheResource', () => {
const testCacheResourcBuffer = Buffer.from('test-cache-resource-content');

const makeRequestSpy = jest
.spyOn(requestLib, 'makeRequest')
.mockResolvedValue({
body: testCacheResourcBuffer,
res: {
statusCode: 200,
body: testCacheResourcBuffer,
} as any,
} as any);
.spyOn(requestLib, 'streamRequest')
.mockResolvedValue(Readable.from(testCacheResourcBuffer));

// Act
await fetchCacheResource(testCacheResourceUrl);

// Assert
expect(makeRequestSpy).toHaveBeenCalledWith({
url: testCacheResourceUrl,
body: null,
headers: {},
method: 'get',
});
});

Expand All @@ -146,22 +143,4 @@ describe('fetchCacheResource', () => {
).rejects.toThrowError();
});
});
describe('when an error response is received', () => {
it('throws an error', async () => {
// Arrange
const testCacheResourceUrl = 'test-cache-resource-url';

jest.spyOn(requestLib, 'makeRequest').mockResolvedValue({
body: '',
res: {
statusCode: 500,
},
} as any);

// Act + Assert
await expect(fetchCacheResource(testCacheResourceUrl)).rejects.toThrow(
CustomError,
);
});
});
});

0 comments on commit 6989e20

Please sign in to comment.