Skip to content

Commit

Permalink
Fix bug with slash in query param (#29)
Browse files Browse the repository at this point in the history
* fix bug

* fix test file
  • Loading branch information
ugolas authored and idanto committed Aug 5, 2019
1 parent 07c0c2a commit 256638c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 3 additions & 3 deletions src/express-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class ExpressMiddleware {
route = req.originalUrl.split('?')[0];
} else {
const splittedRoute = route.split('/');
const splittedUrl = req.originalUrl.split('/');
const splittedUrl = req.originalUrl.split('?')[0].split('/');
const routeIndex = splittedUrl.length - splittedRoute.length + 1;

const baseUrl = splittedUrl.slice(0, routeIndex).join('/');
Expand All @@ -69,12 +69,12 @@ class ExpressMiddleware {
route = route.replace(req.params[paramName], ':' + paramName);
});
}

// this condition will evaluate to true only in
// express framework and no route was found for the request. if we log this metrics
// we'll risk in a memory leak since the route is not a pattern but a hardcoded string.
if (!route || route === '') {
// if (!req.route && res && res.statusCode === 404) {
// if (!req.route && res && res.statusCode === 404) {
route = 'N/A';
}

Expand Down
18 changes: 17 additions & 1 deletion test/integration-tests/express/middleware-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('when using express framework', () => {
});
});
});
describe('when calling a GET endpoint with query parmas', () => {
describe('when calling a GET endpoint with query params', () => {
before(() => {
return supertest(app)
.get('/v2?test=test')
Expand All @@ -323,6 +323,22 @@ describe('when using express framework', () => {
});
});
});
describe('when calling a GET endpoint with query params with special character /', () => {
before(() => {
return supertest(app)
.get('/checkout?test=test/test1')
.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('http_request_duration_seconds_bucket{le="+Inf",method="GET",route="/checkout",code="200"} 1');
});
});
});
});
describe('sub-sub app with error handler in the sub app', function () {
describe('when calling a GET endpoint with path params and sub router', () => {
Expand Down

0 comments on commit 256638c

Please sign in to comment.