Skip to content

Commit

Permalink
feat: Supporting route match (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored and potato4d committed Apr 16, 2019
1 parent c0533f9 commit 2c9d4e9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
14 changes: 13 additions & 1 deletion lib/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,19 @@ const createBasicMessage = (options = {}) =>
const createMiddleware = options => {
return (req, res, next) => {
try {
if (auth(options, req)) {
let enabled = true
let match = options.match
if (typeof match === 'function') {
enabled = match(req)
}
else if (match instanceof RegExp) {
enabled = match.test(req.url)
}
else if (typeof match === 'string') {
enabled = match === req.url
}

if (!enabled || auth(options, req)) {
return next()
}
} catch (e) {
Expand Down
68 changes: 68 additions & 0 deletions test/unit/middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,72 @@ describe('middleware.js', () => {
})
})
})

describe('option.match', () => {
const res = { setHeader: ([_, message]) => message, end: () => 'res.end' }
const next = () => 'next'
const configs = { name: 'foo', pass: 'bar' }

const makeReq = (url, auth=false) => ({
url,
headers: { authorization: auth ? b64credential : '' }
})

test('match option set to undefined', () => {
const match = undefined
const middleware = createMiddleware({ ...configs, match })

// enabled on all routes
expect(middleware(makeReq('/', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/foo/bar', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/', true), res, next)).toBe('next')
})

test('match option set to string', () => {
const match = '/admin'
const middleware = createMiddleware({ ...configs, match })

// disabled routes
expect(middleware(makeReq('/', false), res, next)).toBe('next')
expect(middleware(makeReq('/foo/bar', false), res, next)).toBe('next')
expect(middleware(makeReq('/admin/foo', false), res, next)).toBe('next')

// enabled routes
expect(middleware(makeReq('/admin', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/admin', true), res, next)).toBe('next')
})

test('match option set to regex', () => {
// enable auth only digits route (eg."/1245")
const match = /^\/\d+$/
const middleware = createMiddleware({ ...configs, match })

// disabled routes
expect(middleware(makeReq('/', false), res, next)).toBe('next')
expect(middleware(makeReq('/foo/bar', false), res, next)).toBe('next')
expect(middleware(makeReq('/231/234', false), res, next)).toBe('next')

// enabled routes
expect(middleware(makeReq('/0', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/123', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/123', true), res, next)).toBe('next')
})

test('match option set to function', () => {
const match = ({url}) => {
return url === '/auth' || url.toLowerCase().startsWith('/admin')
}
const middleware = createMiddleware({ ...configs, match })

// disabled routes
expect(middleware(makeReq('/', false), res, next)).toBe('next')
expect(middleware(makeReq('/foo/bar', false), res, next)).toBe('next')

// enabled routes
expect(middleware(makeReq('/auth', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/admin', false), res, next)).toBe('res.end')
expect(middleware(makeReq('/admin/foo', false), res, next)).toBe('res.end')
})

})
})

0 comments on commit 2c9d4e9

Please sign in to comment.