Skip to content

Commit f788f0c

Browse files
committed
fix: content-type header for metrics endpoints
Issue #13: - JSON endpoints now return the correct Content-Type: `application/json; charset=utf-8`. Additionally: - Added tests to verify the correctness of the Content-Type header. This fix ensures proper header formatting and alignment with client expectations.
1 parent fc13434 commit f788f0c

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ tags
1010
doc/apidoc/
1111
.idea
1212
*.swp
13+
.DS_Store

roles/metrics-export.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ end
100100
local http_handlers = {
101101
json = function(req)
102102
local json_exporter = require('metrics.plugins.json')
103-
return req:render({ text = json_exporter.export() })
103+
return req:render({ json = json_exporter.export() })
104104
end,
105105
prometheus = function(...)
106106
local http_handler = require('metrics.plugins.prometheus').collect_http

test/unit/http_test.lua

+50
Original file line numberDiff line numberDiff line change
@@ -1140,3 +1140,53 @@ for name, case in pairs(test_tls_cases) do
11401140
end
11411141
end
11421142
end
1143+
1144+
local function assert_content_type(uri, expected_content_type, tls_opts)
1145+
local response = http_client:get(uri, tls_opts)
1146+
t.assert_equals(response.status, 200) -- Убедимся, что эндпоинт доступен
1147+
t.assert_equals(response.headers['content-type'], expected_content_type) -- Проверка Content-Type
1148+
end
1149+
1150+
local test_content_type_cases = {
1151+
['json'] = {
1152+
cfg = {
1153+
http = {
1154+
{
1155+
listen = 8081,
1156+
endpoints = {
1157+
{
1158+
path = "/json_metrics",
1159+
format = "json",
1160+
},
1161+
},
1162+
},
1163+
},
1164+
},
1165+
expected_url = "http://127.0.0.1:8081/json_metrics",
1166+
expected_content_type = "application/json; charset=utf-8",
1167+
},
1168+
['prometheus'] = {
1169+
cfg = {
1170+
http = {
1171+
{
1172+
listen = 8081,
1173+
endpoints = {
1174+
{
1175+
path = "/prometheus_metrics",
1176+
format = "prometheus",
1177+
},
1178+
},
1179+
},
1180+
},
1181+
},
1182+
expected_url = "http://127.0.0.1:8081/prometheus_metrics",
1183+
expected_content_type = "text/plain; charset=utf8",
1184+
},
1185+
}
1186+
1187+
for name, case in pairs(test_content_type_cases) do
1188+
g['test_content_type_' .. name] = function(cg)
1189+
cg.role.apply(case.cfg)
1190+
assert_content_type(case.expected_url, case.expected_content_type)
1191+
end
1192+
end

0 commit comments

Comments
 (0)