src/nine_elli_util.erl

-module(nine_elli_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#{resp => {404, [], <<"Not Found">>}}.

-spec redirect(map(), binary()) -> map().
redirect(Context, RedirectPath) ->
    Context#{resp => {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(#{resp => {404, [], <<"Not Found">>}}, not_found(#{})).

redirect_test() ->
    ?assertEqual(#{resp => {302, [{<<"Location">>, <<"/foo">>}], <<>>}},
                 redirect(#{}, <<"/foo">>)).

-endif.