Skip to content
Merged
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
45 changes: 45 additions & 0 deletions __tests__/basePath.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,49 @@ describe('Base Path Tests:', function() {
expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"path":"/v1/test/test2/test3","method":"get","status":"ok"}', isBase64Encoded: false })
}) // end it

it('Middleware stack is inherited for root path when basepathed', async function() {
const testApi = require('../index')({ base: 'base-path' })
testApi.use((req, res, next) => {
req.middlewareState = {
segments: req.path.split('/').filter(Boolean)
}
next()
})
testApi.use((req, res, next) => {
req.middlewareState.trace = req.middlewareState.segments.join(':')
res.header('x-middleware-trace', req.middlewareState.trace)
next()
})
testApi.get('/', async req => ({
status: 'ok',
trace: req.middlewareState.trace,
segmentCount: req.middlewareState.segments.length
}))

let _event = Object.assign({},event,{ path: '/base-path/' })
let result = await new Promise(r => testApi.run(_event,{},(e,res) => { r(res) }))
expect(result).toEqual({
multiValueHeaders: {
'content-type': ['application/json'],
'x-middleware-trace': ['base-path']
},
statusCode: 200,
body: '{"status":"ok","trace":"base-path","segmentCount":1}',
isBase64Encoded: false
})
}) // end it

it('Middleware runs for root path when basepathed (CORS regression)', async function() {
const testApi = require('../index')({ base: 'base-path' })
testApi.use((req, res, next) => {
res.cors()
next()
})
testApi.get('/', async () => ({ status: 'ok' }))

let _event = Object.assign({},event,{ path: '/base-path/' })
let result = await new Promise(r => testApi.run(_event,{},(e,res) => { r(res) }))
expect(result.multiValueHeaders).toHaveProperty('access-control-allow-origin')
}) // end it

}) // end BASEPATH tests
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ class API {
routes['ROUTES'][route[i]]['MIDDLEWARE'].stack;
} // end if

// Check for a wildcard child middleware (e.g. base path + '/')
if (
parsedPath.length === 0 &&
route[i] !== '*' &&
routes['ROUTES'][route[i]] &&
routes['ROUTES'][route[i]]['ROUTES'] &&
routes['ROUTES'][route[i]]['ROUTES']['*'] &&
routes['ROUTES'][route[i]]['ROUTES']['*']['MIDDLEWARE']
) {
_stack['*'][method] =
routes['ROUTES'][route[i]]['ROUTES']['*']['MIDDLEWARE'].stack;
} // end if

// Generate the route/method meta data
let meta = {
vars: pathVars,
Expand Down
Loading