Skip to content

Commit

Permalink
Throw an error when http_middelware is processing a wrong handler (#296)
Browse files Browse the repository at this point in the history
  • Loading branch information
yngvar-antonsson authored Sep 9, 2021
1 parent ec73907 commit df62af8
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
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 @@ -72,6 +72,10 @@ end
-- @return value from observable function
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

0 comments on commit df62af8

Please sign in to comment.