Skip to content

Commit

Permalink
fix(rest): make the route binding key friendly for find
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondfeng committed Feb 26, 2018
1 parent 871ddef commit e3577ab
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/rest/src/rest-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ export class RestServer extends Context implements Server {
): Binding {
if (typeof routeOrVerb === 'object') {
const r = routeOrVerb;
return this.bind(`routes.${r.verb} ${r.path}`).to(r);
// Encode the path to escape special chars
const encodedPath = encodeURIComponent(r.path).replace(/\./g, '%2E');
return this.bind(`routes.${r.verb} ${encodedPath}`)
.to(r)
.tag('route');
}

if (!path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,31 @@ describe('RestServer.getApiSpec()', () => {
});
});

it('binds a route via app.route(route)', () => {
function greet() {}
const binding = server.route(
new Route('get', '/greet', {responses: {}}, greet),
);
expect(binding.key).to.eql('routes.get %2Fgreet');
expect(binding.tags.has('route')).to.be.true();
});

it('binds a route via app.route(..., Controller, method)', () => {
class MyController {
greet() {}
}

const binding = server.route(
'get',
'/greet.json',
{responses: {}},
MyController,
'greet',
);
expect(binding.key).to.eql('routes.get %2Fgreet%2Ejson');
expect(binding.tags.has('route')).to.be.true();
});

it('returns routes registered via app.route(route)', () => {
function greet() {}
server.route(new Route('get', '/greet', {responses: {}}, greet));
Expand Down

0 comments on commit e3577ab

Please sign in to comment.