Skip to content

Commit

Permalink
close #32 by looping methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydaly committed May 16, 2018
1 parent a4323d7 commit 20f3164
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 12 deletions.
32 changes: 20 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class API {
// METHOD: Adds method and handler to routes
METHOD(method, path, handler) {

// Ensure method is an array
let methods = Array.isArray(method) ? method : method.split(',')

// Parse the path
let parsedPath = this.parseRoute(path)

Expand All @@ -102,18 +105,23 @@ class API {
route[i] = '__VAR__'
} // end if variable

// Add the route to the global _routes
this.setRoute(
this._routes,
(i === route.length-1 ? {
['__'+method.toUpperCase()]: {
vars: pathVars,
handler: handler,
route: '/'+parsedPath.join('/'),
path: '/'+this._prefix.concat(parsedPath).join('/') }
} : {}),
route.slice(0,i+1)
)
methods.forEach(_method => {
if (typeof _method === 'string') {
// Add the route to the global _routes
this.setRoute(
this._routes,
(i === route.length-1 ? {
['__'+_method.trim().toUpperCase()]: {
vars: pathVars,
handler: handler,
route: '/'+parsedPath.join('/'),
path: '/'+this._prefix.concat(parsedPath).join('/') }
} : {}),
route.slice(0,i+1)
)
}
}) // end methods loop


} // end for loop

Expand Down
78 changes: 78 additions & 0 deletions test/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const expect = require('chai').expect // Assertion library
// Init API instance
const api = require('../index')({ version: 'v1.0' })
const api2 = require('../index')({ version: 'v1.0' })
const api3 = require('../index')({ version: 'v1.0' })

// NOTE: Set test to true
api._test = true;
api2._test = true;
api3._test = true;

let event = {
httpMethod: 'get',
Expand Down Expand Up @@ -206,6 +208,16 @@ api.head('/head/*', (req,res) => {
res.status(200).header('wildcard',true).json({ })
})

// Multi methods
api3.METHOD('get,post','/multimethod/test', (req,res) => {
res.status(200).json({ method: req.method, path: '/multimethod/test' })
})
api3.METHOD(['get','put','delete'],'/multimethod/:var', (req,res) => {
res.status(200).json({ method: req.method, path: '/multimethod/:var' })
})
api3.METHOD([1,'DELETE'],'/multimethod/badtype', (req,res) => {
res.status(200).json({ method: req.method, path: '/multimethod/badtype' })
})

/******************************************************************************/
/*** BEGIN TESTS ***/
Expand Down Expand Up @@ -744,6 +756,72 @@ describe('Route Tests:', function() {
})
}) // end it

it('Multiple methods GET (string creation)', async function() {
let _event = Object.assign({},event,{ path: '/multimethod/test', httpMethod: 'get' })
let result = await new Promise(r => api3.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 200,
body: '{"method":"GET","path":"/multimethod/test"}',
isBase64Encoded: false
})
}) // end it

it('Multiple methods POST (string creation)', async function() {
let _event = Object.assign({},event,{ path: '/multimethod/test', httpMethod: 'post' })
let result = await new Promise(r => api3.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 200,
body: '{"method":"POST","path":"/multimethod/test"}',
isBase64Encoded: false
})
}) // end it

it('Multiple methods GET (array creation)', async function() {
let _event = Object.assign({},event,{ path: '/multimethod/x', httpMethod: 'get' })
let result = await new Promise(r => api3.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 200,
body: '{"method":"GET","path":"/multimethod/:var"}',
isBase64Encoded: false
})
}) // end it

it('Multiple methods PUT (array creation)', async function() {
let _event = Object.assign({},event,{ path: '/multimethod/x', httpMethod: 'put' })
let result = await new Promise(r => api3.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 200,
body: '{"method":"PUT","path":"/multimethod/:var"}',
isBase64Encoded: false
})
}) // end it

it('Multiple methods POST (method not allowed)', async function() {
let _event = Object.assign({},event,{ path: '/multimethod/x', httpMethod: 'post' })
let result = await new Promise(r => api3.run(_event,{},(e,res) => { r(res) }))
expect(result).to.deep.equal({
headers: { 'content-type': 'application/json' },
statusCode: 405,
body: '{"error":"Method not allowed"}',
isBase64Encoded: false
})
}) // end it

it('Expected routes', function() {
expect(api3.routes()).to.deep.equal([
[ 'POST', '/multimethod/test' ],
[ 'GET', '/multimethod/test' ],
[ 'DELETE', '/multimethod/:var' ],
[ 'PUT', '/multimethod/:var' ],
[ 'GET', '/multimethod/:var' ],
[ 'DELETE', '/multimethod/badtype' ]
])
}) // end it

}) // end method tests

describe('routes() (debug method)', function() {
Expand Down

0 comments on commit 20f3164

Please sign in to comment.