diff --git a/src/express-middleware.js b/src/express-middleware.js index a1ef3d4..6ae9d43 100644 --- a/src/express-middleware.js +++ b/src/express-middleware.js @@ -57,6 +57,11 @@ class ExpressMiddleware { route = route ? route + req.route.path : req.route.path; } + // #112 - aggregated express default route + if (route === '*') { + return route; + } + if (!route || route === '' || typeof route !== 'string') { route = req.originalUrl.split('?')[0]; } else { diff --git a/test/integration-tests/express/middleware-default-route-test.js b/test/integration-tests/express/middleware-default-route-test.js new file mode 100644 index 0000000..f765910 --- /dev/null +++ b/test/integration-tests/express/middleware-default-route-test.js @@ -0,0 +1,74 @@ +'use strict'; +const { expect } = require('chai'); +const Prometheus = require('prom-client'); +const supertest = require('supertest'); + +let app, config; + +describe('when using express framework (default route)', function() { + this.timeout(4000); + before(() => { + config = require('./server/config'); + config.useUniqueHistogramName = true; + app = require('./server/express-server-default-route'); + }); + after(() => { + Prometheus.register.clear(); + delete require.cache[require.resolve('./server/express-server-default-route.js')]; + delete require.cache[require.resolve('../../../src/index.js')]; + delete require.cache[require.resolve('../../../src/metrics-middleware.js')]; + }); + + describe('when start up with default route', () => { + describe('when calling default endpoint', () => { + before(() => { + return supertest(app) + .get('/dne') + .expect(200) + .then((res) => {}); + }); + it('should add it to the histogram', () => { + return supertest(app) + .get('/metrics') + .expect(200) + .then((res) => { + expect(res.text).to.contain('method="GET",route="*",code="200"'); + }); + }); + }); + + describe('when calling existing GET endpoint', () => { + before(() => { + return supertest(app) + .get('/hello') + .expect(200) + .then((res) => {}); + }); + it('should add it to the histogram', () => { + return supertest(app) + .get('/metrics') + .expect(200) + .then((res) => { + expect(res.text).to.contain('method="GET",route="/hello",code="200"'); + }); + }); + }); + + describe('when calling a GET endpoint with one query param', () => { + before(() => { + return supertest(app) + .get('/hello?test=test') + .expect(200) + .then((res) => {}); + }); + it('should add it to the histogram', () => { + return supertest(app) + .get('/metrics') + .expect(200) + .then((res) => { + expect(res.text).to.contain('method="GET",route="*",code="200"'); + }); + }); + }); + }); +}); diff --git a/test/integration-tests/express/server/express-server-default-route.js b/test/integration-tests/express/server/express-server-default-route.js new file mode 100644 index 0000000..c098caa --- /dev/null +++ b/test/integration-tests/express/server/express-server-default-route.js @@ -0,0 +1,18 @@ +'use strict'; + +const express = require('express'); +const middleware = require('../../../../src/index.js').expressMiddleware; + +const app = express(); +app.use(middleware({ includeQueryParams: true })); + +app.get('/hello', (req, res) => { + res.json({ message: 'Hello World!' }); +}) + +app.all('*', (req, res) => { + res.status(200); + res.json({ message: 'Default' }); +}); + +module.exports = app;