From 8639ffba87bc1c17176934ba39174e5b8c65fb8b Mon Sep 17 00:00:00 2001 From: Thomas Hunter II Date: Thu, 6 Jul 2023 16:23:36 -0700 Subject: [PATCH] express: recognize regex with flags (#3301) --- .../datadog-plugin-express/test/index.spec.js | 36 +++++++++++++++++++ packages/datadog-plugin-router/src/index.js | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/packages/datadog-plugin-express/test/index.spec.js b/packages/datadog-plugin-express/test/index.spec.js index ec5ca7618a7..14299a50280 100644 --- a/packages/datadog-plugin-express/test/index.spec.js +++ b/packages/datadog-plugin-express/test/index.spec.js @@ -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() diff --git a/packages/datadog-plugin-router/src/index.js b/packages/datadog-plugin-router/src/index.js index 86864b6bcea..682a4301580 100644 --- a/packages/datadog-plugin-router/src/index.js +++ b/packages/datadog-plugin-router/src/index.js @@ -156,7 +156,7 @@ function isMoreSpecificThan (routeA, routeB) { } function routeIsRegex (route) { - return route.includes('(/') && route.includes('/)') + return route.includes('(/') } module.exports = RouterPlugin