Skip to content

Commit

Permalink
refactor: harmonize diff function parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
yannick-bonnefond authored and ybonnefond committed Oct 23, 2024
1 parent 7cd4a01 commit 28c970c
Show file tree
Hide file tree
Showing 14 changed files with 161 additions and 113 deletions.
9 changes: 5 additions & 4 deletions src/@types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ export type RequestInfo = {
method: RequestMethod;
};

export type Request = IncomingMessage &
RequestInfo & {
searchParams?: URLSearchParams;
};
export type RequestInfoWithSearchParam = RequestInfo & {
searchParams: URLSearchParams;
};

export type Request = IncomingMessage & RequestInfoWithSearchParam;

export type Response = ServerResponse;
export type NextFunction = (err?: any) => void;
Expand Down
41 changes: 32 additions & 9 deletions src/diff/bodyDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BodyDefinition, JsonValue, RequestBody } from '../@types';
import { DIFF_SUBJECTS, DIFF_TYPES, WILDCARD } from '../constants';
import { BodyDefinition, JsonValue } from '../@types';
import { DIFF_SUBJECTS, DIFF_TYPES, METHODS, WILDCARD } from '../constants';
import { bodyDiff } from './bodyDiff';
import { makeExpectDiffError } from '../../test';
import { makeExpectDiffError, makeRequestInfo } from '../../test';
import { Route } from '../Route';

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

const route = new Route(METHODS.GET, '/').setBody(def as BodyDefinition);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);

expectErrors(result, []);
});

Expand All @@ -92,7 +97,11 @@ describe('bodyDiff', () => {
const val = {
a: 'a',
};
const result = bodyDiff(def, val);

const route = new Route(METHODS.GET, '/').setBody(def);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);

expectErrors(result, [
{
type: DIFF_TYPES.MISSING,
Expand Down Expand Up @@ -123,7 +132,11 @@ describe('bodyDiff', () => {
},
},
};
const result = bodyDiff(def, val);

const route = new Route(METHODS.GET, '/').setBody(def);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);

expectErrors(result, [
{
type: DIFF_TYPES.MISSING,
Expand All @@ -144,7 +157,11 @@ describe('bodyDiff', () => {
a: 'a',
b: 'b',
};
const result = bodyDiff(def, val);

const route = new Route(METHODS.GET, '/').setBody(def);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);

expectErrors(result, [
{
type: DIFF_TYPES.EXTRA,
Expand Down Expand Up @@ -175,7 +192,11 @@ describe('bodyDiff', () => {
},
},
};
const result = bodyDiff(def, val);

const route = new Route(METHODS.GET, '/').setBody(def);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);

expectErrors(result, [
{
type: DIFF_TYPES.EXTRA,
Expand Down Expand Up @@ -243,7 +264,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);
const route = new Route(METHODS.GET, '/').setBody(def);
const request = makeRequestInfo({ body: val });
const result = bodyDiff(route, request);
expectErrors(result, [error]);
});
});
Expand Down
12 changes: 6 additions & 6 deletions src/diff/bodyDiff.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { BodyDefinition, DiffError, RequestBody } from '../@types';
import { BodyDefinition, DiffError, RequestInfo, RequestBody } from '../@types';
import { checkValue, findErrors } from './utils';
import { DIFF_SUBJECTS } from '../constants';
import { Route } from '../Route';

const subject = DIFF_SUBJECTS.BODY;

export function bodyDiff(
definition: BodyDefinition,
value: RequestBody,
): DiffError[] {
return rec({ definition, value, path: '' });
export function bodyDiff(route: Route, request: RequestInfo): DiffError[] {
const value =
request.body instanceof Buffer ? String(request.body) : request.body;
return rec({ definition: route.getBody(), value, path: '' });
}

function rec({
Expand Down
50 changes: 24 additions & 26 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_SUBJECTS, DIFF_TYPES, WILDCARD } from '../constants';
import { HeadersDefinition, JsonValue, RequestHeaders } from '../@types';
import { DIFF_SUBJECTS, DIFF_TYPES, METHODS, WILDCARD } from '../constants';
import { headersDiff } from './headersDiff';
import { makeExpectDiffError } from '../../test';
import { makeExpectDiffError, makeRequestInfo } from '../../test';
import { Route } from '../Route';

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

it('should fail if header is extra', () => {
const errors = headersDiff({}, { authorization: 'Bearer 123' });
const errors = run({}, { authorization: 'Bearer 123' });
expectErrors(errors, [
{
definition: 'undefined',
Expand All @@ -30,7 +31,7 @@ describe('headersDiff', () => {
});

it('should fail with both missing and extra headers', () => {
const errors = headersDiff(
const errors = run(
{ accept: 'application/json' },
{ authorization: 'Bearer 456' },
);
Expand All @@ -53,35 +54,35 @@ describe('headersDiff', () => {

describe('using wildcard', () => {
it('should pass if headers are wildcarded', () => {
const errors = headersDiff(WILDCARD, { authorization: 'basic 123' });
const errors = run(WILDCARD, { authorization: 'basic 123' });
expectErrors(errors, []);
});

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

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

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

describe('with string definition', () => {
it('should fail if value is not equal to definition', () => {
const errors = headersDiff(
const errors = run(
{ accept: 'application/json' },
{ accept: 'application/text' },
);
Expand All @@ -96,15 +97,15 @@ describe('headersDiff', () => {
});

it('should pass if value is equal to definition', () => {
const errors = headersDiff(
const errors = run(
{ accept: 'application/json' },
{ accept: 'application/json' },
);
expectErrors(errors, []);
});

it('should pass if value is equal to definition and header case is different', () => {
const errors = headersDiff(
const errors = run(
{ accept: 'application/json' },
{ Accept: 'application/json' },
);
Expand All @@ -114,10 +115,7 @@ describe('headersDiff', () => {

describe('with RexExp definition', () => {
it('should fail if value does not match definition', () => {
const errors = headersDiff(
{ accept: /json/ },
{ accept: 'application/text' },
);
const errors = run({ accept: /json/ }, { accept: 'application/text' });
expectErrors(errors, [
{
definition: '/json/',
Expand All @@ -129,10 +127,7 @@ describe('headersDiff', () => {
});

it('should pass if value matches definition', () => {
const errors = headersDiff(
{ accept: /json/ },
{ accept: 'application/json' },
);
const errors = run({ accept: /json/ }, { accept: 'application/json' });
expectErrors(errors, []);
});
});
Expand All @@ -142,10 +137,7 @@ describe('headersDiff', () => {
const fn = (val: JsonValue) => (val as string).endsWith('json');
const fnString = String(fn);

const errors = headersDiff(
{ accept: fn },
{ accept: 'application/text' },
);
const errors = run({ accept: fn }, { accept: 'application/text' });
expectErrors(errors, [
{
definition: fnString,
Expand All @@ -157,13 +149,19 @@ describe('headersDiff', () => {
});

it('should pass if value passes function definition', () => {
const errors = headersDiff(
const errors = run(
{ accept: (val: JsonValue) => (val as string).endsWith('json') },
{ accept: 'application/json' },
);
expectErrors(errors, []);
});
});

function run(def: HeadersDefinition, val: RequestHeaders) {
const route = new Route(METHODS.GET, '/').setHeaders(def);
const request = makeRequestInfo({ headers: val });
return headersDiff(route, request);
}

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

export function headersDiff(route: Route, request: RequestInfo): DiffError[] {
const headersDefinitions = route.getHeaders();
const headersValues = request.headers;

export function headersDiff(
_definitions: HeadersDefinition,
_values: RequestHeaders,
): DiffError[] {
const definition =
WILDCARD === _definitions ? WILDCARD : keysToLowerCase(_definitions);
const value = keysToLowerCase(_values);
WILDCARD === headersDefinitions
? WILDCARD
: keysToLowerCase(headersDefinitions);
const value = keysToLowerCase(headersValues);

return findErrors({
subject: DIFF_SUBJECTS.HEADERS,
Expand Down
8 changes: 6 additions & 2 deletions src/diff/methodDiff.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { DIFF_SUBJECTS, DIFF_TYPES } from '../constants';
import { DIFF_SUBJECTS, DIFF_TYPES, METHODS } from '../constants';
import { methodDiff } from './methodDiff';
import { Route } from '../Route';
import { makeRequestInfo } from '../../test';

describe('methodDiff', () => {
function run(def: any, value: any) {
return methodDiff(def, value);
const route = new Route(METHODS.GET, '/').setHeaders(def);
const request = makeRequestInfo({ method: value });
return methodDiff(route, request);
}

function expectNoErrors(def: any, value: any) {
Expand Down
8 changes: 5 additions & 3 deletions src/diff/methodDiff.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { DiffError } from '../@types';
import { checkValue } from './utils';
import { DIFF_SUBJECTS } from '../constants';
import { RequestInfo } from '../@types';
import { Route } from '../Route';

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

describe('pathDiff', () => {
function run(def: any, value: any) {
return pathDiff(def, value);
const route = new Route(METHODS.GET, def);
const request = makeRequestInfo({ path: value });
return pathDiff(route, request);
}

function expectNoErrors(def: any, value: any) {
Expand Down
12 changes: 5 additions & 7 deletions src/diff/pathDiff.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { DiffError, PathDefinition } from '../@types';
import { DiffError, RequestInfo } from '../@types';
import { checkValue } from './utils';
import { DIFF_SUBJECTS } from '../constants';
import { Route } from '../Route';

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

0 comments on commit 28c970c

Please sign in to comment.