Skip to content

Commit

Permalink
express: recognize regex with flags (#3301)
Browse files Browse the repository at this point in the history
  • Loading branch information
tlhunter authored and Qard committed Jul 14, 2023
1 parent aa08968 commit b3bcce0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
36 changes: 36 additions & 0 deletions packages/datadog-plugin-express/test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,42 @@ describe('Plugin', () => {
})
})

it('should work with regex having flags', done => {
const app = express()

try {
app.use(/\/foo\/(bar|baz|bez)/i, (req, res, next) => {
next()
})
} 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('long regex child of string router should not steal path', 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 @@ -156,7 +156,7 @@ function isMoreSpecificThan (routeA, routeB) {
}

function routeIsRegex (route) {
return route.includes('(/') && route.includes('/)')
return route.includes('(/')
}

module.exports = RouterPlugin

0 comments on commit b3bcce0

Please sign in to comment.