Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw an error when http_middelware is processing a wrong handler #296

Merged
merged 3 commits into from
Sep 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Removed
- Average collector

### Fixed
- Throw an error when http_middelware is processing a wrong handler [#199](https://github.com/tarantool/metrics/issues/199)

## [0.10.0] - 2021-08-03
### Changed
- metrics registry refactoring to search with `O(1)` [#188](https://github.com/tarantool/metrics/issues/188)
Expand Down
4 changes: 4 additions & 0 deletions metrics/http_middleware.lua
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ end
-- ... arguments for pcall to instrument
function export.observe(collector, route, ...)
return collector:observe_latency(function(ok, result)
if type(result) ~= 'table' then
error(('incorrect http handler for %s %s: expecting return response object'):
format(route.method, route.path), 0)
end
return {
path = route.path,
method = route.method,
Expand Down
35 changes: 35 additions & 0 deletions test/http_middleware_test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,20 @@ g.test_v1_middleware = function()
)
end

g.test_v1_wrong_handler = function()
local request = {endpoint = table.copy(route)}

local observer = stub_observer(function() end)
local handler = function()
-- we forget to return result!
-- return result
end
t.assert_error_msg_contains(
'incorrect http handler for POST /some/path: expecting return response object',
http_middleware.v1(handler, observer), request
)
end

g.test_v2_middleware = function()
local httpd = require('http.server').new('127.0.0.1', 12345)
t.skip_if(httpd.set_router == nil, 'Skip http 2.x test')
Expand All @@ -170,3 +184,24 @@ g.test_v2_middleware = function()
'number'
)
end

g.test_v2_wrong_handler = function()
local httpd = require('http.server').new('127.0.0.1', 12345)
t.skip_if(httpd.set_router == nil, 'Skip http 2.x test')
local router = require('http.router').new()
router:route(route, function()
-- we forget to return result!
-- return result
end)
router:use(http_middleware.v2(), {name = 'http_instrumentation'})
httpd:set_router(router)
httpd:start()
local client = require('http.client').new()
local response = client:request(route.method, 'http://127.0.0.1:12345' .. route.path)
httpd:stop()
t.assert_equals(response.status, 500)
t.assert_str_contains(
response.body,
'incorrect http handler for POST /some/path: expecting return response object'
)
end