Skip to content

Commit

Permalink
handling mixture of string and regex routing
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter committed Jun 1, 2023
1 parent 614f250 commit bc874d8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
40 changes: 39 additions & 1 deletion packages/datadog-plugin-express/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ describe('Plugin', () => {
})
})

it('should not lose the current path when route handler preceeded by a longer middleware resource', done => {
it('long regex should not steal path', done => {
const app = express()

try {
Expand Down Expand Up @@ -741,6 +741,44 @@ describe('Plugin', () => {
})
})

it('long regex child of string router should not steal path', done => {
const app = express()
const router = express.Router()

try {
router.use(/\/(bar|baz|bez)/, (req, res, next) => {
next()
})
app.use('/foo', router)
} catch (err) {
// eslint-disable-next-line no-console
console.log('This version of Express (>4.0 <4.6) has broken support for regex routing. Skipping this test.')
this.skip && this.skip() // mocha allows dynamic skipping, tap does not
return done()
}

app.get('/foo/bar', (req, res) => {
res.status(200).send('')
})

getPort().then(port => {
agent
.use(traces => {
const spans = sort(traces[0])

expect(spans[0]).to.have.property('resource', 'GET /foo/bar')
})
.then(done)
.catch(done)

appListener = app.listen(port, 'localhost', () => {
axios
.get(`http://localhost:${port}/foo/bar`)
.catch(done)
})
})
})

it('should not lose the current path on next', done => {
const app = express()
const Router = express.Router
Expand Down
2 changes: 1 addition & 1 deletion packages/datadog-plugin-router/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function isMoreSpecificThan (routeA, routeB) {
function routeIsRegex (route) {
// console.log('ROUTE', typeof route, route)
// return route instanceof RegExp
return route.startsWith('(/') && route.endsWith('/)')
return route.includes('(/') && route.includes('/)')
}

module.exports = RouterPlugin

0 comments on commit bc874d8

Please sign in to comment.