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

Make the new cowboy_req tests more realistic

This commit is contained in:
Loïc Hoguin 2017-01-04 19:45:35 +01:00
parent f34ef2ceae
commit e5a8088e68
No known key found for this signature in database
GPG key ID: 71366FF21851DF03
3 changed files with 28 additions and 23 deletions

View file

@ -580,7 +580,6 @@ set_resp_headers(Headers, Req=#{resp_headers := RespHeaders}) ->
set_resp_headers(Headers, Req) -> set_resp_headers(Headers, Req) ->
Req#{resp_headers => Headers}. Req#{resp_headers => Headers}.
-spec resp_header(binary(), req()) -> binary() | undefined. -spec resp_header(binary(), req()) -> binary() | undefined.
resp_header(Name, Req) -> resp_header(Name, Req) ->
resp_header(Name, Req, undefined). resp_header(Name, Req, undefined).

View file

@ -26,28 +26,34 @@ do(<<"set_resp_header">>, Req0, Opts) ->
Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0), Req = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts}; {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
do(<<"set_resp_headers">>, Req0, Opts) -> do(<<"set_resp_headers">>, Req0, Opts) ->
Req = cowboy_req:set_resp_headers(#{<<"x-header-test1">> => <<"test1">>, <<"x-header-test2">> => <<"test2">>}, Req0), Req = cowboy_req:set_resp_headers(#{
<<"content-type">> => <<"text/plain">>,
<<"content-encoding">> => <<"gzip">>
}, Req0),
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
do(<<"resp_header_defined">>, Req0, Opts) ->
Req1 = cowboy_req:set_resp_header(<<"content-type">>, <<"text/plain">>, Req0),
<<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1),
<<"text/plain">> = cowboy_req:resp_header(<<"content-type">>, Req1, default),
{ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
do(<<"resp_header_default">>, Req, Opts) ->
undefined = cowboy_req:resp_header(<<"content-type">>, Req),
default = cowboy_req:resp_header(<<"content-type">>, Req, default),
{ok, cowboy_req:reply(200, #{}, "OK", Req), Opts}; {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
do(<<"resp_headers">>, Req0, Opts) -> do(<<"resp_headers">>, Req0, Opts) ->
Req1 = cowboy_req:set_resp_header(<<"x-header-test1">>, <<"test1">>, Req0), Req1 = cowboy_req:set_resp_header(<<"server">>, <<"nginx">>, Req0),
Req2 = cowboy_req:set_resp_headers(#{<<"x-header-test2">> => <<"test2">>, <<"x-header-test3">> => <<"test3">>}, Req1), Req = cowboy_req:set_resp_headers(#{
Headers = cowboy_req:resp_headers(Req2), <<"content-type">> => <<"text/plain">>,
true = maps:is_key(<<"x-header-test1">>, Headers), <<"content-encoding">> => <<"gzip">>
true = maps:is_key(<<"x-header-test2">>, Headers), }, Req1),
true = maps:is_key(<<"x-header-test3">>, Headers), Headers = cowboy_req:resp_headers(Req),
{ok, cowboy_req:reply(200, #{}, "OK", Req2), Opts}; true = maps:is_key(<<"server">>, Headers),
do(<<"resp_header_defined">>, Req0, Opts) -> true = maps:is_key(<<"content-type">>, Headers),
Req1 = cowboy_req:set_resp_header(<<"x-header-test1">>, <<"test1">>, Req0), true = maps:is_key(<<"content-encoding">>, Headers),
<<"test1">> = cowboy_req:resp_header(<<"x-header-test1">>, Req1), {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
<<"test1">> = cowboy_req:resp_header(<<"x-header-test1">>, Req1, foo), do(<<"resp_headers_empty">>, Req, Opts) ->
{ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts}; #{} = cowboy_req:resp_headers(Req),
do(<<"resp_header_default">>, Req0, Opts) -> {ok, cowboy_req:reply(200, #{}, "OK", Req), Opts};
undefined = cowboy_req:resp_header(<<"x-header-test1">>, Req0),
<<"ok">> = cowboy_req:resp_header(<<"x-header-test1">>, Req0, <<"ok">>),
{ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
do(<<"resp_headers_empty">>, Req0, Opts) ->
#{} = cowboy_req:resp_headers(Req0),
{ok, cowboy_req:reply(200, #{}, "OK", Req0), Opts};
do(<<"set_resp_body">>, Req0, Opts) -> do(<<"set_resp_body">>, Req0, Opts) ->
Arg = cowboy_req:binding(arg, Req0), Arg = cowboy_req:binding(arg, Req0),
Req1 = case Arg of Req1 = case Arg of

View file

@ -490,8 +490,8 @@ set_resp_header(Config) ->
set_resp_headers(Config) -> set_resp_headers(Config) ->
doc("Response using set_resp_headers."), doc("Response using set_resp_headers."),
{200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config), {200, Headers, <<"OK">>} = do_get("/resp/set_resp_headers", Config),
true = lists:keymember(<<"x-header-test1">>, 1, Headers), true = lists:keymember(<<"content-type">>, 1, Headers),
true = lists:keymember(<<"x-header-test2">>, 1, Headers), true = lists:keymember(<<"content-encoding">>, 1, Headers),
ok. ok.
resp_header(Config) -> resp_header(Config) ->