Skip to content

Commit

Permalink
feat: pass subject in DiffError
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-bonnefond authored and ybonnefond committed Oct 22, 2024
1 parent 1ffaa08 commit 2a096e1
Show file tree
Hide file tree
Showing 16 changed files with 167 additions and 68 deletions.
3 changes: 2 additions & 1 deletion src/@types/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IncomingMessage, ServerResponse } from 'http';
import { DIFF_TYPES, METHODS, WILDCARD } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES, METHODS, WILDCARD } from '../constants';

export type JsonPrimitive = string | number | boolean | null;
export type JsonObject = { [member: string]: JsonValue };
Expand Down Expand Up @@ -100,6 +100,7 @@ export type ResponseHeaders = {
export type ResponseBody = JsonValue | Buffer;

export interface DiffError {
subject: DIFF_SUBJECTS;
type: DIFF_TYPES;
definition: string | null;
value: JsonValue;
Expand Down
8 changes: 8 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const EVENTS = {

export const WILDCARD = Symbol.for('WILDCARD');

export enum DIFF_SUBJECTS {
METHOD = 'METHOD',
HEADERS = 'HEADERS',
PATH = 'PATH',
QUERY = 'QUERY',
BODY = 'BODY',
}

export enum DIFF_TYPES {
FAIL_EQUALITY = 'FAIL_EQUALITY',
FAIL_MATCHING = 'FAIL_MATCHING',
Expand Down
17 changes: 10 additions & 7 deletions src/diff/bodyDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BodyDefinition, JsonValue, RequestBody } from '../@types';
import { DIFF_TYPES, WILDCARD } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES, WILDCARD } from '../constants';
import { bodyDiff } from './bodyDiff';
import { makeExpectDiffError } from '../../test';

describe('bodyDiff', () => {
const LC_STRING = /^[a-z]+$/;
Expand Down Expand Up @@ -79,7 +80,7 @@ describe('bodyDiff', () => {
])('should pass with def %p and value %p', (...args) => {
const [def, val] = args;
const result = bodyDiff(def as BodyDefinition, val);
expect(result).toEqual([]);
expectErrors(result, []);
});

describe('missing keys', () => {
Expand All @@ -92,7 +93,7 @@ describe('bodyDiff', () => {
a: 'a',
};
const result = bodyDiff(def, val);
expect(result).toEqual([
expectErrors(result, [
{
type: DIFF_TYPES.MISSING,
path: 'b',
Expand Down Expand Up @@ -123,7 +124,7 @@ describe('bodyDiff', () => {
},
};
const result = bodyDiff(def, val);
expect(result).toEqual([
expectErrors(result, [
{
type: DIFF_TYPES.MISSING,
path: 'a.b.c.d',
Expand All @@ -144,7 +145,7 @@ describe('bodyDiff', () => {
b: 'b',
};
const result = bodyDiff(def, val);
expect(result).toEqual([
expectErrors(result, [
{
type: DIFF_TYPES.EXTRA,
path: 'b',
Expand Down Expand Up @@ -175,7 +176,7 @@ describe('bodyDiff', () => {
},
};
const result = bodyDiff(def, val);
expect(result).toEqual([
expectErrors(result, [
{
type: DIFF_TYPES.EXTRA,
path: 'a.b.c.e',
Expand Down Expand Up @@ -243,7 +244,9 @@ describe('bodyDiff', () => {
)('should fail if def %p not equal val %p', (...args) => {
const [def, val, error] = args;
const result = bodyDiff(def as BodyDefinition, val as RequestBody);
expect(result).toEqual([error]);
expectErrors(result, [error]);
});
});

const expectErrors = makeExpectDiffError(DIFF_SUBJECTS.BODY);
});
13 changes: 11 additions & 2 deletions src/diff/bodyDiff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { BodyDefinition, DiffError, RequestBody } from '../@types';
import { checkValue, findErrors } from './utils';
import { DIFF_SUBJECTS } from '../constants';

const subject = DIFF_SUBJECTS.BODY;

export function bodyDiff(
definition: BodyDefinition,
Expand All @@ -22,8 +25,14 @@ function rec({
definition !== null &&
!(definition instanceof RegExp)
) {
return findErrors({ definition, value, validate: rec, prefix: path });
return findErrors({
subject,
definition,
value,
validate: rec,
prefix: path,
});
}

return checkValue({ definition, value, path });
return checkValue({ subject, definition, value, path });
}
33 changes: 18 additions & 15 deletions src/diff/headersDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { JsonValue } from '../@types';
import { DIFF_TYPES, WILDCARD } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES, WILDCARD } from '../constants';
import { headersDiff } from './headersDiff';
import { makeExpectDiffError } from '../../test';

describe('headersDiff', () => {
describe('with extra or missing headers', () => {
it('should fail if header is missing', () => {
const errors = headersDiff({ authorization: /^Bearer/ }, {});
expect(errors).toEqual([
expectErrors(errors, [
{
definition: '/^Bearer/',
path: 'authorization',
Expand All @@ -18,7 +19,7 @@ describe('headersDiff', () => {

it('should fail if header is extra', () => {
const errors = headersDiff({}, { authorization: 'Bearer 123' });
expect(errors).toEqual([
expectErrors(errors, [
{
definition: 'undefined',
path: 'authorization',
Expand All @@ -33,7 +34,7 @@ describe('headersDiff', () => {
{ accept: 'application/json' },
{ authorization: 'Bearer 456' },
);
expect(errors).toEqual([
expectErrors(errors, [
{
definition: 'application/json',
path: 'accept',
Expand All @@ -53,28 +54,28 @@ describe('headersDiff', () => {
describe('using wildcard', () => {
it('should pass if headers are wildcarded', () => {
const errors = headersDiff(WILDCARD, { authorization: 'basic 123' });
expect(errors).toEqual([]);
expectErrors(errors, []);
});

it('should pass if a header is wildcarded', () => {
const errors = headersDiff(
{ authorization: WILDCARD },
{ authorization: 'basic 123' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});

it('should pass if a header is wildcarded and case is different', () => {
const errors = headersDiff(
{ authorization: WILDCARD },
{ AUTHORIZATION: 'basic 123' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});

it('should pass if a header is wildcarded and NOT in request', () => {
const errors = headersDiff({ authorization: WILDCARD }, {});
expect(errors).toEqual([]);
expectErrors(errors, []);
});
});

Expand All @@ -84,7 +85,7 @@ describe('headersDiff', () => {
{ accept: 'application/json' },
{ accept: 'application/text' },
);
expect(errors).toEqual([
expectErrors(errors, [
{
definition: 'application/json',
path: 'accept',
Expand All @@ -99,15 +100,15 @@ describe('headersDiff', () => {
{ accept: 'application/json' },
{ accept: 'application/json' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});

it('should pass if value is equal to definition and header case is different', () => {
const errors = headersDiff(
{ accept: 'application/json' },
{ Accept: 'application/json' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});
});

Expand All @@ -117,7 +118,7 @@ describe('headersDiff', () => {
{ accept: /json/ },
{ accept: 'application/text' },
);
expect(errors).toEqual([
expectErrors(errors, [
{
definition: '/json/',
path: 'accept',
Expand All @@ -132,7 +133,7 @@ describe('headersDiff', () => {
{ accept: /json/ },
{ accept: 'application/json' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});
});

Expand All @@ -145,7 +146,7 @@ describe('headersDiff', () => {
{ accept: fn },
{ accept: 'application/text' },
);
expect(errors).toEqual([
expectErrors(errors, [
{
definition: fnString,
path: 'accept',
Expand All @@ -160,7 +161,9 @@ describe('headersDiff', () => {
{ accept: (val: JsonValue) => (val as string).endsWith('json') },
{ accept: 'application/json' },
);
expect(errors).toEqual([]);
expectErrors(errors, []);
});
});

const expectErrors = makeExpectDiffError(DIFF_SUBJECTS.HEADERS);
});
10 changes: 8 additions & 2 deletions src/diff/headersDiff.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DiffError, HeadersDefinition, RequestHeaders } from '../@types';
import { WILDCARD } from '../constants';
import { DIFF_SUBJECTS, WILDCARD } from '../constants';
import { checkValue, findErrors } from './utils';

export function headersDiff(
Expand All @@ -10,7 +10,13 @@ export function headersDiff(
WILDCARD === _definitions ? WILDCARD : keysToLowerCase(_definitions);
const value = keysToLowerCase(_values);

return findErrors({ definition, value, validate: checkValue, prefix: '' });
return findErrors({
subject: DIFF_SUBJECTS.HEADERS,
definition,
value,
validate: checkValue,
prefix: '',
});
}

function keysToLowerCase(o: Record<string, any>): Record<string, any> {
Expand Down
10 changes: 8 additions & 2 deletions src/diff/methodDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DIFF_TYPES } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES } from '../constants';
import { methodDiff } from './methodDiff';

describe('methodDiff', () => {
Expand All @@ -15,7 +15,13 @@ describe('methodDiff', () => {
const errors = run(def, value);

expect(errors).toEqual([
{ type, definition: String(def), value, path: '' },
{
subject: DIFF_SUBJECTS.METHOD,
type,
definition: String(def),
value,
path: '',
},
]);
}

Expand Down
2 changes: 2 additions & 0 deletions src/diff/methodDiff.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { DiffError } from '../@types';
import { checkValue } from './utils';
import { DIFF_SUBJECTS } from '../constants';

export function methodDiff(definition: string, value: string): DiffError[] {
return checkValue({
subject: DIFF_SUBJECTS.METHOD,
definition: definition.toLowerCase(),
value: value.toLowerCase(),
path: '',
Expand Down
10 changes: 8 additions & 2 deletions src/diff/pathDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsonValue } from '../@types';
import { DIFF_TYPES } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES } from '../constants';
import { pathDiff } from './pathDiff';

describe('pathDiff', () => {
Expand All @@ -16,7 +16,13 @@ describe('pathDiff', () => {
const errors = run(def, value);

expect(errors).toEqual([
{ type, definition: String(def), value, path: '' },
{
subject: DIFF_SUBJECTS.PATH,
type,
definition: String(def),
value,
path: '',
},
]);
}

Expand Down
8 changes: 7 additions & 1 deletion src/diff/pathDiff.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { DiffError, PathDefinition } from '../@types';
import { checkValue } from './utils';
import { DIFF_SUBJECTS } from '../constants';

export function pathDiff(
definition: PathDefinition,
value: string,
): DiffError[] {
return checkValue({ definition, value, path: '' });
return checkValue({
subject: DIFF_SUBJECTS.PATH,
definition,
value,
path: '',
});
}
Loading

0 comments on commit 2a096e1

Please sign in to comment.