Skip to content

Commit

Permalink
Update contraint check to throw with 32 handlers (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
valeneiko authored Jan 17, 2024
1 parent ca54d8d commit f3170bd
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/handler-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class HandlerStorage {
}

const isMergedTree = constraintsNames.includes(httpMethodStrategy.name)
if (!isMergedTree && this.handlers.length >= 32) {
throw new Error('find-my-way supports a maximum of 32 route handlers per node when there are constraints, limit reached')
if (!isMergedTree && this.handlers.length >= 31) {
throw new Error('find-my-way supports a maximum of 31 route handlers per node when there are constraints, limit reached')
}

this.handlers.push(handlerObject)
Expand Down
29 changes: 29 additions & 0 deletions test/constraint.host.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,32 @@ test('A route supports multiple host constraints (lookup)', t => {
headers: { host: 'bar.fancy.ca' }
})
})

test('A route supports up to 31 host constraints', (t) => {
t.plan(1)

const findMyWay = FindMyWay()

for (let i = 0; i < 31; i++) {
const host = `h${i.toString().padStart(2, '0')}`
findMyWay.on('GET', '/', { constraints: { host } }, alpha)
}

t.equal(findMyWay.find('GET', '/', { host: 'h01' }).handler, alpha)
})

test('A route throws when constraint limit exceeded', (t) => {
t.plan(1)

const findMyWay = FindMyWay()

for (let i = 0; i < 31; i++) {
const host = `h${i.toString().padStart(2, '0')}`
findMyWay.on('GET', '/', { constraints: { host } }, alpha)
}

t.throws(
() => findMyWay.on('GET', '/', { constraints: { host: 'h31' } }, beta),
'find-my-way supports a maximum of 31 route handlers per node when there are constraints, limit reached'
)
})

0 comments on commit f3170bd

Please sign in to comment.