0
Fork 0
mirror of https://github.com/ninenines/cowboy.git synced 2025-07-14 04:10:24 +00:00

add minor tests

This commit is contained in:
geeksilva97 2024-04-19 09:23:36 -03:00
parent 9af49cd4cd
commit d43807d19b

View file

@ -68,6 +68,7 @@
-export([resp_headers/1]).
-export([set_resp_header/3]).
-export([set_resp_headers/2]).
-export([set_resp_headers_list/2]).
-export([has_resp_header/2]).
-export([delete_resp_header/2]).
-export([set_resp_body/2]).
@ -366,6 +367,26 @@ uri2_test() ->
<<"http://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{port => 80})),
<<"https://localhost/path?dummy=2785">> = iolist_to_binary(uri(Req, #{scheme => "https", port => 443})),
ok.
resp_headers_test() ->
Req = #{
scheme => <<"http">>, host => <<"localhost">>, port => 8080,
path => <<"/path">>, qs => <<"dummy=2785">>
},
#{resp_headers := RespHeaders} = set_resp_headers_list([
{<<"Name">>, <<"Cormano">>},
{<<"Name">>, <<"Paco">>},
{<<"X-MyHeader">>, <<"custom-header">>},
{<<"api-key">>, "My api"},
{<<"api-key">>, "KEY"}
], #{}, Req),
#{
<<"Name">> := <<"Cormano, Paco">>,
<<"X-MyHeader">> := <<"custom-header">>
} = RespHeaders,
ok.
-endif.
-spec binding(atom(), req()) -> any() | undefined.
@ -726,24 +747,27 @@ set_resp_header(Name, Value, Req=#{resp_headers := RespHeaders}) ->
set_resp_header(Name,Value, Req) ->
Req#{resp_headers => #{Name => Value}}.
% @todo process headers list - reduce to a map and concat the values of repeated headers, except for set-cookie that will be treated differently
% @todo define the correct spec
-spec set_resp_headers_list(list(term()), req())
% @todo make it work with iodata(), for now only bitstrings are accepted
-spec set_resp_headers_list(list({ binary(), iodata() }), Req)
-> Req when Req::req().
set_resp_headers_list(HeaderTupleList, Req) ->
set_resp_headers_list(HeaderTupleList, #{}, Req).
set_resp_headers_list([], Map, Req) ->
% @todo merge Map with Req headers
Req;
set_resp_headers(Map, Req);
set_resp_headers_list([{<<"set-cookie">>, Value} | Headers], Map, Req) ->
set_resp_headers_list(Headers, Map, Req);
set_resp_headers_list([{Name, Value} | Headers], Map, Req) ->
NewHeaderValue = case maps:get(Name, Map, undefined) of
undefined -> Value,
undefined -> Value;
ExistingValue -> <<ExistingValue/binary, ", ", Value/binary>>
end,
Map1 = maps:put(Name, NewHeaderValue, Map),
set_resp_headers_list(Headers, Map1, Req1).
set_resp_headers_list(Headers, Map1, Req).
-spec set_resp_headers(cowboy:http_headers(), Req)
-> Req when Req::req().