Files
EventHubBack/test/unit/admin_handler_health_tests.erl

49 lines
2.0 KiB
Erlang

-module(admin_handler_health_tests).
-include_lib("eunit/include/eunit.hrl").
%% ------------------------------------------------------------------
%% Фикстуры
%% ------------------------------------------------------------------
setup() ->
ok = meck:new(cowboy_req, [non_strict]),
ok.
cleanup(_) ->
meck:unload(cowboy_req).
%% ------------------------------------------------------------------
%% Тесты
%% ------------------------------------------------------------------
admin_handler_health_test_() ->
{setup, fun setup/0, fun cleanup/1, [
{"GET /admin/health returns 200 with status ok",
fun test_health_get/0},
{"POST /admin/health returns 405 Method not allowed",
fun test_health_post/0}
]}.
%% ── Успешный GET ─────────────────────────────────────────────
test_health_get() ->
% Мокаем method → <<"GET">>
ok = meck:expect(cowboy_req, method, fun(_) -> <<"GET">> end),
% reply/4 будем перехватывать
ok = meck:expect(cowboy_req, reply,
fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_health:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(200, Status),
?assertEqual(#{<<"status">> => <<"ok">>}, jsx:decode(RespBody, [return_maps])).
%% ── Метод не разрешён ───────────────────────────────────────
test_health_post() ->
ok = meck:expect(cowboy_req, method, fun(_) -> <<"POST">> end),
ok = meck:expect(cowboy_req, reply,
fun(Code, Headers, Body, Req) ->
put(test_reply, {Code, Headers, Body, Req})
end),
{ok, _, _} = admin_handler_health:init(req, []),
{Status, _, RespBody, _} = erase(test_reply),
?assertEqual(405, Status),
?assertEqual(#{<<"error">> => <<"Method not allowed">>}, jsx:decode(RespBody, [return_maps])).