From 20f31646a8ce209def0d01c95617919b95213fd5 Mon Sep 17 00:00:00 2001 From: Jeremy Daly Date: Wed, 16 May 2018 19:53:04 -0400 Subject: [PATCH] close #32 by looping methods --- index.js | 32 +++++++++++++-------- test/routes.js | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 1da54f8..2224142 100644 --- a/index.js +++ b/index.js @@ -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) @@ -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 diff --git a/test/routes.js b/test/routes.js index 9bb8392..155f35e 100644 --- a/test/routes.js +++ b/test/routes.js @@ -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', @@ -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 ***/ @@ -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() {