Skip to content

Commit

Permalink
🚧 fail if matchOrigin is given an valid match expression
Browse files Browse the repository at this point in the history
  • Loading branch information
yjaaidi committed Oct 16, 2019
1 parent 9281182 commit 91f2c0d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { OriginMatchExpression } from './origin-match-expression';
import { OriginMatcher } from './origin-matcher';

export function invalidOriginMatchExpression(matchExpression) {
return new Error(
`InvalidOriginMatchExpression: ${JSON.stringify(
matchExpression
)} is an invalid origin match expression.`
);
}

export function findMatcherOrThrow({
matchExpression,
matcherList
}: {
matchExpression: OriginMatchExpression;
matcherList: OriginMatcher[];
}) {
const matcher = matcherList.find(_matcher =>
_matcher.canHandle(matchExpression)
);
if (matcher == null) {
throw invalidOriginMatchExpression(matchExpression);
}
return matcher;
}
12 changes: 0 additions & 12 deletions libs/http-ext/src/lib/matchers/match-origin/find-matcher.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ describe('matchOrigin', () => {
beforeEach(() => (request = createRequest({ url: 'https://test.com' })));

it('🚧 should throw when given an object', () => {
expect(() => matchOrigin({} as any)({ request })).toThrow();
expect(() => matchOrigin({} as any)({ request })).toThrow(
'InvalidOriginMatchExpression: {} is an invalid origin match expression.'
);
});

it('🚧 should throw when given an number', () => {
expect(() => matchOrigin(123 as any)({ request })).toThrow();
expect(() => matchOrigin(123 as any)({ request })).toThrow(
'InvalidOriginMatchExpression: 123 is an invalid origin match expression.'
);
});
});
4 changes: 2 additions & 2 deletions libs/http-ext/src/lib/matchers/match-origin/match-origin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { RequestCondition } from '../../plugin';
import { findMatcher } from './find-matcher';
import { findMatcherOrThrow } from './find-matcher-or-throw';
import { getOrigin } from './get-origin';
import { originArrayMatcher } from './origin-array-matcher';
import { OriginMatchExpression } from './origin-match-expression';
Expand All @@ -14,7 +14,7 @@ export const matchOrigin = (
const origin = getOrigin(request.url);

/* Find the right matcher. */
const matcher = findMatcher({
const matcher = findMatcherOrThrow({
matchExpression: matchExpression,
matcherList: [
originStringMatcher,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isArray } from '../../utils/is-array';
import { findMatcher } from './find-matcher';
import { findMatcherOrThrow } from './find-matcher-or-throw';
import { originStringMatcher } from './origin-string-matcher';

export const originArrayMatcher = {
Expand All @@ -10,7 +10,7 @@ export const originArrayMatcher = {
/* Loop through expressions... */
return matchExpression.some(childExpression => {
/* ... find the right matcher for each expression... */
const matcher = findMatcher({
const matcher = findMatcherOrThrow({
matchExpression: childExpression,
matcherList: [originStringMatcher]
});
Expand Down

0 comments on commit 91f2c0d

Please sign in to comment.