Skip to content

Commit ac7e2fa

Browse files
authored
fix: middleware inheritance for base-pathed root (/) routes (#319)
* Initial plan * fix middleware for base-pathed root route * style: normalize middleware callback spacing in regression test * test: strengthen base-path root middleware regression * test: restore CORS root middleware regression case ---------
1 parent cd2f50a commit ac7e2fa

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

__tests__/basePath.unit.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,49 @@ describe('Base Path Tests:', function() {
5858
expect(result).toEqual({ multiValueHeaders: { 'content-type': ['application/json'] }, statusCode: 200, body: '{"path":"/v1/test/test2/test3","method":"get","status":"ok"}', isBase64Encoded: false })
5959
}) // end it
6060

61+
it('Middleware stack is inherited for root path when basepathed', async function() {
62+
const testApi = require('../index')({ base: 'base-path' })
63+
testApi.use((req, res, next) => {
64+
req.middlewareState = {
65+
segments: req.path.split('/').filter(Boolean)
66+
}
67+
next()
68+
})
69+
testApi.use((req, res, next) => {
70+
req.middlewareState.trace = req.middlewareState.segments.join(':')
71+
res.header('x-middleware-trace', req.middlewareState.trace)
72+
next()
73+
})
74+
testApi.get('/', async req => ({
75+
status: 'ok',
76+
trace: req.middlewareState.trace,
77+
segmentCount: req.middlewareState.segments.length
78+
}))
79+
80+
let _event = Object.assign({},event,{ path: '/base-path/' })
81+
let result = await new Promise(r => testApi.run(_event,{},(e,res) => { r(res) }))
82+
expect(result).toEqual({
83+
multiValueHeaders: {
84+
'content-type': ['application/json'],
85+
'x-middleware-trace': ['base-path']
86+
},
87+
statusCode: 200,
88+
body: '{"status":"ok","trace":"base-path","segmentCount":1}',
89+
isBase64Encoded: false
90+
})
91+
}) // end it
92+
93+
it('Middleware runs for root path when basepathed (CORS regression)', async function() {
94+
const testApi = require('../index')({ base: 'base-path' })
95+
testApi.use((req, res, next) => {
96+
res.cors()
97+
next()
98+
})
99+
testApi.get('/', async () => ({ status: 'ok' }))
100+
101+
let _event = Object.assign({},event,{ path: '/base-path/' })
102+
let result = await new Promise(r => testApi.run(_event,{},(e,res) => { r(res) }))
103+
expect(result.multiValueHeaders).toHaveProperty('access-control-allow-origin')
104+
}) // end it
105+
61106
}) // end BASEPATH tests

index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ class API {
207207
routes['ROUTES'][route[i]]['MIDDLEWARE'].stack;
208208
} // end if
209209

210+
// Check for a wildcard child middleware (e.g. base path + '/')
211+
if (
212+
parsedPath.length === 0 &&
213+
route[i] !== '*' &&
214+
routes['ROUTES'][route[i]] &&
215+
routes['ROUTES'][route[i]]['ROUTES'] &&
216+
routes['ROUTES'][route[i]]['ROUTES']['*'] &&
217+
routes['ROUTES'][route[i]]['ROUTES']['*']['MIDDLEWARE']
218+
) {
219+
_stack['*'][method] =
220+
routes['ROUTES'][route[i]]['ROUTES']['*']['MIDDLEWARE'].stack;
221+
} // end if
222+
210223
// Generate the route/method meta data
211224
let meta = {
212225
vars: pathVars,

0 commit comments

Comments
 (0)