test(core): updated test cases for `findRoute`
MODIFY
packages/core/src/router/router.spec.ts
packages/core/src/router/router.spec.ts
import { mapTo } from 'rxjs/operators';
import { Effect } from '../effects/effects.interface';
import { findRoute, resolveRouting } from './router';
-import { HttpRequest, HttpResponse } from '../http.interface';
+import { HttpRequest, HttpResponse, HttpMethod } from '../http.interface';
import { RouteMatched, Routing } from './router.interface';
describe('Router', () => {
@@ -13,6 +13,7 @@ describe('Router', () => {
const e2$: Effect = req$ => req$.pipe(mapTo({ body: 'test' }));
const e3$: Effect = req$ => req$.pipe(mapTo({ body: 'test' }));
const e4$: Effect = req$ => req$.pipe(mapTo({ body: 'test' }));
+
const routing: Routing = [
{ regExp: /^\/?$/, methods: { GET: { effect: e1$ }, POST: { effect: e4$ } } },
{ regExp: /^\/group\/?$/, methods: { GET: { effect: e2$ } } },
@@ -22,10 +23,31 @@ describe('Router', () => {
// when
const route1 = findRoute(routing, '/group/nested/foo', 'POST');
const route2 = findRoute(routing, '/group/nested/fo', 'POST');
+ const route3 = findRoute(routing, '/group/nested/foo', 'TEST' as HttpMethod);
// then
- expect(route1).toEqual({ effect: e3$, middleware: undefined, params: {} });
+ expect(route1).toEqual({ effect: e3$, params: {} });
expect(route2).toBeUndefined();
+ expect(route3).toBeUndefined();
+ });
+
+ test('finds parametrized route inside collection', () => {
+ // given
+ const e$: Effect = req$ => req$.pipe(mapTo({ body: 'test' }));
+
+ const routing: Routing = [{
+ regExp: /^\/group\/([^\/]+)\/foo$/,
+ methods: { GET: { effect: e$, parameters: ['param'] } }
+ }];
+
+ // when
+ const route = findRoute(routing, '/group/nested/foo', 'GET');
+
+ // then
+ expect(route).toEqual({
+ effect: e$,
+ params: { param: 'nested' }
+ });
});
});

MODIFY
packages/core/src/router/router.ts
packages/core/src/router/router.ts
@@ -11,32 +11,38 @@ export const findRoute = (
): RouteMatched | undefined => {
for (let i = 0; i < routing.length; ++i) {
const { regExp, methods } = routing[i];
-
const match = url.match(regExp);
+
if (!match) {
continue;
}
const routingMethod = methods[method];
+
if (!routingMethod) {
return undefined;
}
+
const { parameters, effect, middleware } = routingMethod;
const params = {};
+
if (parameters) {
for (let p = 0; p < parameters.length; p++) {
params[parameters[p]] = decodeURIComponent(match[p + 1]);
}
}
+
return { middleware, effect, params };
}
return undefined;
};
export const resolveRouting =
- (routing: Routing) => (res: HttpResponse) => (req: HttpRequest): Observable<EffectResponse> => {
+ (routing: Routing) =>
+ (res: HttpResponse) =>
+ (req: HttpRequest): Observable<EffectResponse> => {
const [urlPath, urlQuery] = req.url.split('?');
- const routeMatched: RouteMatched | undefined = findRoute(routing, urlPath, req.method);
+ const routeMatched = findRoute(routing, urlPath, req.method);
if (!routeMatched) {
return EMPTY;
@@ -44,10 +50,11 @@ export const resolveRouting =
req.query = queryParamsFactory(urlQuery);
req.params = routeMatched.params;
+
const middleware = routeMatched.middleware;
const req$ = of(req);
- if (middleware) {
- return routeMatched.effect(middleware(req$, res, null), res, null);
- }
- return routeMatched.effect(req$, res, null);
+
+ return middleware
+ ? routeMatched.effect(middleware(req$, res, null), res, null)
+ : routeMatched.effect(req$, res, null);
};

test(core): updated test cases for `findRoute`