-module(nine_util).
-export([get_method/1, not_found/1, redirect/2]).
-include_lib("elli/include/elli.hrl").
-spec get_method(elli:req()) -> atom().
get_method(Req) ->
Req#req.method.
-spec not_found(map()) -> map().
not_found(Context) ->
Context#{response => {404, [], <<"Not Found">>}}.
-spec redirect(map(), binary()) -> map().
redirect(Context, RedirectPath) ->
Context#{response => {302, [{<<"Location">>, RedirectPath}], <<>>}}.
-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").
get_method_test() ->
Req = #req{method = 'GET'},
?assertEqual('GET', get_method(Req)).
not_found_test() ->
?assertEqual(#{response => {404, [], <<"Not Found">>}}, not_found(#{})).
redirect_test() ->
?assertEqual(#{response => {302, [{<<"Location">>, <<"/foo">>}], <<>>}},
redirect(#{}, <<"/foo">>)).
-endif.