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

add allowd_methods tests

This commit is contained in:
geeksilva97 2024-02-04 10:58:40 -03:00
parent 3df6822ade
commit 1288e68f31
2 changed files with 10 additions and 6 deletions

View file

@ -340,8 +340,7 @@ allowed_methods(Req, State=#state{method=Method}) ->
next(Req, State#state{allowed_methods=DefaultAllowedMethods}, next(Req, State#state{allowed_methods=DefaultAllowedMethods},
fun malformed_request/2); fun malformed_request/2);
no_call -> no_call ->
method_not_allowed(Req, State, method_not_allowed(Req, State, DefaultAllowedMethods);
[<<"HEAD">>, <<"GET">>, <<"OPTIONS">>]);
{stop, Req2, State2} -> {stop, Req2, State2} ->
terminate(Req2, State2); terminate(Req2, State2);
{Switch, Req2, State2} when element(1, Switch) =:= switch_handler -> {Switch, Req2, State2} when element(1, Switch) =:= switch_handler ->
@ -349,12 +348,14 @@ allowed_methods(Req, State=#state{method=Method}) ->
{List, Req2, State2} -> {List, Req2, State2} ->
case lists:member(Method, List) of case lists:member(Method, List) of
true when Method =:= <<"OPTIONS">> -> true when Method =:= <<"OPTIONS">> ->
<< ", ", Allow/binary >> = << << ", ", M/binary >> || M <- List >>, Allow = stringify_allowed_methods(List),
Req3 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req2), Req3 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req2),
next(Req3, State2#state{allowed_methods=List}, next(Req3, State2#state{allowed_methods=List},
fun malformed_request/2); fun malformed_request/2);
true -> true ->
next(Req2, State2, fun malformed_request/2); Allow = stringify_allowed_methods(List),
Req3 = cowboy_req:set_resp_header(<<"allow">>, Allow, Req2),
next(Req3, State2, fun malformed_request/2);
false -> false ->
method_not_allowed(Req2, State2, List) method_not_allowed(Req2, State2, List)
end end

View file

@ -472,7 +472,8 @@ delete_resource_missing(Config) ->
Ref = gun:delete(ConnPid, "/delete_resource?missing", [ Ref = gun:delete(ConnPid, "/delete_resource?missing", [
{<<"accept-encoding">>, <<"gzip">>} {<<"accept-encoding">>, <<"gzip">>}
]), ]),
{response, _, 500, _} = gun:await(ConnPid, Ref), {response, _, 500, Headers} = gun:await(ConnPid, Ref),
{_, <<"DELETE">>} = lists:keyfind(<<"allow">>, 1, Headers),
ok. ok.
create_resource_created(Config) -> create_resource_created(Config) ->
@ -483,7 +484,8 @@ create_resource_created(Config) ->
Ref = gun:post(ConnPid, "/create_resource?created", [ Ref = gun:post(ConnPid, "/create_resource?created", [
{<<"content-type">>, <<"application/text">>} {<<"content-type">>, <<"application/text">>}
], <<"hello">>, #{}), ], <<"hello">>, #{}),
{response, _, 201, _} = gun:await(ConnPid, Ref), {response, _, 201, Headers} = gun:await(ConnPid, Ref),
{_, <<"POST">>} = lists:keyfind(<<"allow">>, 1, Headers),
ok. ok.
create_resource_see_other(Config) -> create_resource_see_other(Config) ->
@ -496,6 +498,7 @@ create_resource_see_other(Config) ->
], <<"hello">>, #{}), ], <<"hello">>, #{}),
{response, _, 303, RespHeaders} = gun:await(ConnPid, Ref), {response, _, 303, RespHeaders} = gun:await(ConnPid, Ref),
{_, _} = lists:keyfind(<<"location">>, 1, RespHeaders), {_, _} = lists:keyfind(<<"location">>, 1, RespHeaders),
{_, <<"POST">>} = lists:keyfind(<<"allow">>, 1, RespHeaders),
ok. ok.
error_on_malformed_accept(Config) -> error_on_malformed_accept(Config) ->