Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router#verbs() #169

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,40 @@ Router.prototype.all = function (name, path, middleware) {
return this;
};

/**
* Register route with concrete verbs.
*
* @param {Array} verbs
* @param {String} name Optional.
* @param {String} path
* @param {Function=} middleware You may also pass multiple middleware.
* @param {Function} callback
* @returns {Router}
*/

Router.prototype.verbs = function (verbs, path, middleware) {

if (!Array.isArray(verbs) || verbs.length === 0) {
throw new Error('You have to provide a list of verbs when adding an verbs handler');
}

if (typeof path === 'string') {
middleware = Array.prototype.slice.call(arguments, 2);
}

// Sanity check to ensure we have a viable path candidate (eg: string|regex|non-empty array)
if (
typeof path !== 'string' &&
!(path instanceof RegExp) &&
(!Array.isArray(path) || path.length === 0)
)
throw new Error('You have to provide a path when adding an all handler');

this.register(path, verbs.map(verb => verb.toLowerCase()), middleware);

return this;
};

/**
* Redirect `source` to `destination` URL with optional 30x status `code`.
*
Expand Down
47 changes: 47 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1131,6 +1131,53 @@ describe('Router', function () {
});
});

describe('Router#verbs()', function () {
it('correctly returns an error when not passed a verbs for "verbs"', function () {
const router = new Router();
try {
router.verbs(function () {});
} catch (err) {
expect(err.message).to.be(
'You have to provide a list of verbs when adding an verbs handler'
);
}
});

it('registers multiple verbs for one route', function (done) {
const app = new Koa();
const router = new Router();

const verbs = ['get', 'post']

router.verbs(
verbs,
'/route',
function (ctx) {
ctx.body = { message: ctx.method };
}
);

app.use(router.routes());

Promise.all(
verbs.map((verb) => {
return request(http.createServer(app.callback()))
[verb](`/route`)
.expect(200);
})
).then(
(resList) => {
for (const res of resList) {
assert.strictEqual(verbs.includes(res.body.message.toLowerCase()), true);
}

done();
},
(err) => done(err)
);
});
})

describe('Router#use()', function () {
it('uses router middleware without path', function (done) {
const app = new Koa();
Expand Down
Loading