Skip to content

Commit

Permalink
fix: options request does not work with query params (#573)
Browse files Browse the repository at this point in the history
* fix: options request does not work with query params

* fix: send appropriate method in request mock

* test: add missing test case branch for non options requests
  • Loading branch information
zacharygolba authored Dec 27, 2016
1 parent 93df71f commit c54cc20
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/packages/router/route/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,18 @@ class Route extends FreezeableSet<Action<any>> {

async visit(req: Request, res: Response): Promise<any> {
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') {
Expand Down
74 changes: 74 additions & 0 deletions src/packages/router/route/test/route.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
});
});
});
});
});
});
8 changes: 5 additions & 3 deletions test/utils/mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -59,7 +60,8 @@ export const createRequestBuilder = ({
query: {},
pathname: path,
path: path,
href: path
href: path,
params: []
},
headers: new Map([
['host', 'localhost:4000']
Expand Down

0 comments on commit c54cc20

Please sign in to comment.