forked from Kong/httpsnippet
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathrunCustomFixtures.ts
More file actions
46 lines (37 loc) · 1.46 KB
/
runCustomFixtures.ts
File metadata and controls
46 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import type { HTTPSnippetOptions, Request } from '../index.js';
import type { ClientId, TargetId } from '../targets/index.js';
import { writeFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { describe, it, expect } from 'vitest';
import { HTTPSnippet } from '../index.js';
export interface CustomFixture {
clientId: ClientId;
targetId: TargetId;
tests: {
/** a file path pointing to the expected custom fixture result */
expected: string;
input: Request;
it: string;
options: any;
}[];
}
export const runCustomFixtures = ({ targetId, clientId, tests }: CustomFixture): void => {
describe(`custom fixtures for ${targetId}:${clientId}`, () => {
it.each(tests.map(t => [t.it, t]))('%s', async (_, { expected: fixtureFile, options, input: request }) => {
const opts: HTTPSnippetOptions = {};
if (options.harIsAlreadyEncoded) {
opts.harIsAlreadyEncoded = options.harIsAlreadyEncoded;
}
const snippet = new HTTPSnippet(request, opts);
const result = snippet.convert(targetId, clientId, options)[0];
const filePath = path.join(__dirname, '..', 'targets', targetId, clientId, 'fixtures', fixtureFile);
if (process.env.OVERWRITE_EVERYTHING) {
writeFileSync(filePath, String(result));
}
const buffer = await readFile(filePath);
const fixture = String(buffer);
expect(result).toStrictEqual(fixture);
});
});
};