diff --git a/src/packages/router/route/index.js b/src/packages/router/route/index.js index 8745894f..88189eda 100644 --- a/src/packages/router/route/index.js +++ b/src/packages/router/route/index.js @@ -148,13 +148,18 @@ class Route extends FreezeableSet> { async visit(req: Request, res: Response): Promise { const { defaultParams } = this; + let params = { + ...req.params, + ...this.parseParams(req.url.params) + }; + + if (req.method !== 'OPTIONS') { + params = this.params.validate(params); + } Object.assign(req, { - defaultParams, - params: this.params.validate({ - ...req.params, - ...this.parseParams(req.url.params) - }) + params, + defaultParams }); if (this.type === 'member' && req.method === 'PATCH') { diff --git a/src/packages/router/route/test/route.test.js b/src/packages/router/route/test/route.test.js index ba1bef48..2b8169d4 100644 --- a/src/packages/router/route/test/route.test.js +++ b/src/packages/router/route/test/route.test.js @@ -325,5 +325,79 @@ describe('module "router/route"', () => { }); }); }); + + describe('#visit', () => { + let controller: Controller; + + before(async () => { + const { controllers } = await getTestApp(); + + // $FlowIgnore + controller = await controllers.get('posts'); + }); + + ['GET', 'OPTIONS'].forEach(method => { + describe(`- method "${method}"`, () => { + let subject; + let createRequest; + + before(async () => { + + + subject = new Route({ + method, + controller, + type: 'collection', + path: 'posts', + action: 'preflight' + }); + }); + + describe('- with params', () => { + before(() => { + createRequest = createRequestBuilder({ + method, + controller, + path: '/posts', + route: subject, + params: { + filter: { + title: 'New Post' + } + } + }); + }); + + it('works', async () => { + const result = await subject.visit( + createRequest(), + createResponse() + ); + + expect(result).to.be.ok; + }); + }); + + describe('- without params', () => { + before(() => { + createRequest = createRequestBuilder({ + method, + path: '/posts', + route: subject + }); + }); + + it('works', async () => { + const result = await subject.visit( + createRequest(), + createResponse() + ); + + expect(result).to.be.ok; + }); + }); + }); + }); + }); }); }); diff --git a/test/utils/mocks.js b/test/utils/mocks.js index ee1117bf..34db73aa 100644 --- a/test/utils/mocks.js +++ b/test/utils/mocks.js @@ -40,12 +40,13 @@ export const createResponse = (): Response => ({ export const createRequestBuilder = ({ path, route, - params + params, + method = 'GET' //$FlowIgnore }) => (): Request => ({ route, params, - method: 'GET', + method, httpVersion: '1.1', url: { protocol: null, @@ -59,7 +60,8 @@ export const createRequestBuilder = ({ query: {}, pathname: path, path: path, - href: path + href: path, + params: [] }, headers: new Map([ ['host', 'localhost:4000']