diff options
author | Loïc Hoguin <[email protected]> | 2014-09-23 16:43:29 +0300 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2014-09-23 16:43:29 +0300 |
commit | f1c3b6d76f0c97e1ab927c288bb94891ae4c253b (patch) | |
tree | 5a14c007cdcb487032162b3ca96df648f521321a /test | |
parent | b57f94661f5fd186f55eb0fead49849e0b1399d1 (diff) | |
download | cowboy-f1c3b6d76f0c97e1ab927c288bb94891ae4c253b.tar.gz cowboy-f1c3b6d76f0c97e1ab927c288bb94891ae4c253b.tar.bz2 cowboy-f1c3b6d76f0c97e1ab927c288bb94891ae4c253b.zip |
Breaking update of the cowboy_req interface
Simplify the interface for most cowboy_req functions. They all return
a single value except the four body reading functions. The reply functions
now only return a Req value.
Access functions do not return a Req anymore.
Functions that used to cache results do not have a cache anymore.
The interface for accessing query string and cookies has therefore
been changed.
There are now three query string functions: qs/1 provides access
to the raw query string value; parse_qs/1 returns the query string
as a list of key/values; match_qs/2 returns a map containing the
values requested in the second argument, after applying constraints
and default value.
Similarly, there are two cookie functions: parse_cookies/1 and
match_cookies/2. More match functions will be added in future commits.
None of the functions return an error tuple anymore. It either works
or crashes. Cowboy will attempt to provide an appropriate status code
in the response of crashed handlers.
As a result, the content decode function has its return value changed
to a simple binary, and the body reading functions only return on success.
Diffstat (limited to 'test')
22 files changed, 68 insertions, 89 deletions
diff --git a/test/handlers/long_polling_h.erl b/test/handlers/long_polling_h.erl index 21f1d4d..1c86aed 100644 --- a/test/handlers/long_polling_h.erl +++ b/test/handlers/long_polling_h.erl @@ -15,8 +15,7 @@ init(_, Req, _) -> {loop, Req, 2, 5000, hibernate}. info(timeout, Req, 0) -> - {ok, Req2} = cowboy_req:reply(102, Req), - {ok, Req2, 0}; + {ok, cowboy_req:reply(102, Req), 0}; info(timeout, Req, Count) -> erlang:send_after(200, self(), timeout), {loop, Req, Count - 1, hibernate}. diff --git a/test/handlers/loop_handler_body_h.erl b/test/handlers/loop_handler_body_h.erl index db69b02..559ef90 100644 --- a/test/handlers/loop_handler_body_h.erl +++ b/test/handlers/loop_handler_body_h.erl @@ -17,8 +17,7 @@ init(_, Req, _) -> info(timeout, Req, State) -> {ok, Body, Req2} = cowboy_req:body(Req), 100000 = byte_size(Body), - {ok, Req3} = cowboy_req:reply(200, Req2), - {ok, Req3, State}. + {ok, cowboy_req:reply(200, Req2), State}. terminate({normal, shutdown}, _, _) -> ok. diff --git a/test/handlers/loop_handler_timeout_h.erl b/test/handlers/loop_handler_timeout_h.erl index 1125046..3b158bf 100644 --- a/test/handlers/loop_handler_timeout_h.erl +++ b/test/handlers/loop_handler_timeout_h.erl @@ -16,8 +16,7 @@ init(_, Req, _) -> {loop, Req, undefined, 200, hibernate}. info(timeout, Req, State) -> - {ok, Req2} = cowboy_req:reply(500, Req), - {ok, Req2, State}. + {ok, cowboy_req:reply(500, Req), State}. terminate({normal, timeout}, _, _) -> ok. diff --git a/test/http_SUITE.erl b/test/http_SUITE.erl index 0417ad0..1bc13c1 100644 --- a/test/http_SUITE.erl +++ b/test/http_SUITE.erl @@ -592,13 +592,11 @@ onrequest_reply(Config) -> %% Hook for the above onrequest tests. do_onrequest_hook(Req) -> - case cowboy_req:qs_val(<<"reply">>, Req) of - {undefined, Req2} -> - cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req2); - {_, Req2} -> - {ok, Req3} = cowboy_req:reply( - 200, [], <<"replied!">>, Req2), - Req3 + case cowboy_req:match_qs(Req, [{reply, [], noreply}]) of + #{reply := noreply} -> + cowboy_req:set_resp_header(<<"server">>, <<"Serenity">>, Req); + _ -> + cowboy_req:reply(200, [], <<"replied!">>, Req) end. onresponse_capitalize(Config) -> @@ -612,8 +610,7 @@ onresponse_capitalize(Config) -> do_onresponse_capitalize_hook(Status, Headers, Body, Req) -> Headers2 = [{cowboy_bstr:capitalize_token(N), V} || {N, V} <- Headers], - {ok, Req2} = cowboy_req:reply(Status, Headers2, Body, Req), - Req2. + cowboy_req:reply(Status, Headers2, Body, Req). onresponse_crash(Config) -> ConnPid = gun_open(Config), @@ -630,9 +627,7 @@ onresponse_reply(Config) -> %% Hook for the above onresponse tests. do_onresponse_hook(_, Headers, _, Req) -> - {ok, Req2} = cowboy_req:reply( - <<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req), - Req2. + cowboy_req:reply(<<"777 Lucky">>, [{<<"x-hook">>, <<"onresponse">>}|Headers], Req). parse_host(Config) -> ConnPid = gun_open(Config), diff --git a/test/http_SUITE_data/http_body_qs.erl b/test/http_SUITE_data/http_body_qs.erl index 8a438e6..09eebdb 100644 --- a/test/http_SUITE_data/http_body_qs.erl +++ b/test/http_SUITE_data/http_body_qs.erl @@ -8,10 +8,9 @@ init({_, http}, Req, _) -> {ok, Req, undefined}. handle(Req, State) -> - {Method, Req2} = cowboy_req:method(Req), - HasBody = cowboy_req:has_body(Req2), - {ok, Req3} = maybe_echo(Method, HasBody, Req2), - {ok, Req3, State}. + Method = cowboy_req:method(Req), + HasBody = cowboy_req:has_body(Req), + {ok, maybe_echo(Method, HasBody, Req), State}. maybe_echo(<<"POST">>, true, Req) -> case cowboy_req:body_qs(Req) of @@ -20,7 +19,6 @@ maybe_echo(<<"POST">>, true, Req) -> {ok, PostVals, Req2} -> echo(proplists:get_value(<<"echo">>, PostVals), Req2) end; - maybe_echo(<<"POST">>, false, Req) -> cowboy_req:reply(400, [], <<"Missing body.">>, Req); maybe_echo(_, _, Req) -> diff --git a/test/http_SUITE_data/http_chunked.erl b/test/http_SUITE_data/http_chunked.erl index 447c0f6..7f0d749 100644 --- a/test/http_SUITE_data/http_chunked.erl +++ b/test/http_SUITE_data/http_chunked.erl @@ -8,7 +8,7 @@ init({_Transport, http}, Req, _Opts) -> {ok, Req, undefined}. handle(Req, State) -> - {ok, Req2} = cowboy_req:chunked_reply(200, Req), + Req2 = cowboy_req:chunked_reply(200, Req), timer:sleep(100), cowboy_req:chunk("chunked_handler\r\n", Req2), timer:sleep(100), diff --git a/test/http_SUITE_data/http_echo_body.erl b/test/http_SUITE_data/http_echo_body.erl index 3334b95..986015a 100644 --- a/test/http_SUITE_data/http_echo_body.erl +++ b/test/http_SUITE_data/http_echo_body.erl @@ -9,21 +9,19 @@ init({_, http}, Req, _) -> handle(Req, State) -> true = cowboy_req:has_body(Req), - {ok, Req3} = case cowboy_req:body(Req, [{length, 1000000}]) of + Req3 = case cowboy_req:body(Req, [{length, 1000000}]) of {ok, Body, Req2} -> handle_body(Req2, Body); {more, _, Req2} -> handle_badlength(Req2) end, {ok, Req3, State}. handle_badlength(Req) -> - {ok, Req2} = cowboy_req:reply(413, [], <<"Request entity too large">>, Req), - {ok, Req2}. + cowboy_req:reply(413, [], <<"Request entity too large">>, Req). handle_body(Req, Body) -> - {Size, Req2} = cowboy_req:body_length(Req), + Size = cowboy_req:body_length(Req), Size = byte_size(Body), - {ok, Req3} = cowboy_req:reply(200, [], Body, Req2), - {ok, Req3}. + cowboy_req:reply(200, [], Body, Req). terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_errors.erl b/test/http_SUITE_data/http_errors.erl index 35ac3bd..57fd684 100644 --- a/test/http_SUITE_data/http_errors.erl +++ b/test/http_SUITE_data/http_errors.erl @@ -5,18 +5,18 @@ -export([init/3, handle/2, terminate/3]). init({_Transport, http}, Req, _Opts) -> - {Case, Req1} = cowboy_req:qs_val(<<"case">>, Req), - case_init(Case, Req1). + #{'case' := Case} = cowboy_req:match_qs(Req, ['case']), + case_init(Case, Req). case_init(<<"init_before_reply">> = Case, _Req) -> cowboy_error_h:ignore(?MODULE, case_init, 2), erlang:error(Case); case_init(<<"init_after_reply">> = Case, Req) -> cowboy_error_h:ignore(?MODULE, case_init, 2), - {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), + _ = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case); case_init(<<"init_reply_handle_error">> = Case, Req) -> - {ok, Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), + Req1 = cowboy_req:reply(200, [], "http_handler_crashes", Req), {ok, Req1, Case}; case_init(<<"handle_before_reply">> = Case, Req) -> {ok, Req, Case}; @@ -31,7 +31,7 @@ handle(_Req, <<"handle_before_reply">> = Case) -> erlang:error(Case); handle(Req, <<"handle_after_reply">> = Case) -> cowboy_error_h:ignore(?MODULE, handle, 2), - {ok, _Req1} = cowboy_req:reply(200, [], "http_handler_crashes", Req), + _ = cowboy_req:reply(200, [], "http_handler_crashes", Req), erlang:error(Case). terminate(_, _, _) -> diff --git a/test/http_SUITE_data/http_handler.erl b/test/http_SUITE_data/http_handler.erl index e1f1665..296c918 100644 --- a/test/http_SUITE_data/http_handler.erl +++ b/test/http_SUITE_data/http_handler.erl @@ -12,8 +12,7 @@ init({_Transport, http}, Req, Opts) -> {ok, Req, #state{headers=Headers, body=Body}}. handle(Req, State=#state{headers=Headers, body=Body}) -> - {ok, Req2} = cowboy_req:reply(200, Headers, Body, Req), - {ok, Req2, State}. + {ok, cowboy_req:reply(200, Headers, Body, Req), State}. terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_init_shutdown.erl b/test/http_SUITE_data/http_init_shutdown.erl index 1445569..512132e 100644 --- a/test/http_SUITE_data/http_init_shutdown.erl +++ b/test/http_SUITE_data/http_init_shutdown.erl @@ -5,12 +5,12 @@ -export([init/3, handle/2, terminate/3]). init({_Transport, http}, Req, _Opts) -> - {ok, Req2} = cowboy_req:reply(<<"666 Init Shutdown Testing">>, + Req2 = cowboy_req:reply(<<"666 Init Shutdown Testing">>, [{<<"connection">>, <<"close">>}], Req), {shutdown, Req2, undefined}. handle(Req, State) -> - {ok, Req2} = cowboy_req:reply(200, [], "Hello world!", Req), + Req2 = cowboy_req:reply(200, [], "Hello world!", Req), {ok, Req2, State}. terminate(_, _, _) -> diff --git a/test/http_SUITE_data/http_loop_stream_recv.erl b/test/http_SUITE_data/http_loop_stream_recv.erl index ce0d1da..77a339b 100644 --- a/test/http_SUITE_data/http_loop_stream_recv.erl +++ b/test/http_SUITE_data/http_loop_stream_recv.erl @@ -16,8 +16,7 @@ info(stream, Req, undefined) -> stream(Req, ID, Acc) -> case cowboy_req:body(Req) of {ok, <<>>, Req2} -> - {ok, Req3} = cowboy_req:reply(200, Req2), - {ok, Req3, undefined}; + {ok, cowboy_req:reply(200, Req2), undefined}; {_, Data, Req2} -> parse_id(Req2, ID, << Acc/binary, Data/binary >>) end. diff --git a/test/http_SUITE_data/http_multipart.erl b/test/http_SUITE_data/http_multipart.erl index 79bfeb8..43ff6ab 100644 --- a/test/http_SUITE_data/http_multipart.erl +++ b/test/http_SUITE_data/http_multipart.erl @@ -9,8 +9,7 @@ init({_Transport, http}, Req, []) -> handle(Req, State) -> {Result, Req2} = acc_multipart(Req, []), - {ok, Req3} = cowboy_req:reply(200, [], term_to_binary(Result), Req2), - {ok, Req3, State}. + {ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}. terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_multipart_stream.erl b/test/http_SUITE_data/http_multipart_stream.erl index 926d150..bde7531 100644 --- a/test/http_SUITE_data/http_multipart_stream.erl +++ b/test/http_SUITE_data/http_multipart_stream.erl @@ -9,8 +9,7 @@ init(_, Req, []) -> handle(Req, State) -> Req2 = multipart(Req), - {ok, Req3} = cowboy_req:reply(200, Req2), - {ok, Req3, State}. + {ok, cowboy_req:reply(200, Req2), State}. terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_req_attr.erl b/test/http_SUITE_data/http_req_attr.erl index eb5e70e..2a4a55d 100644 --- a/test/http_SUITE_data/http_req_attr.erl +++ b/test/http_SUITE_data/http_req_attr.erl @@ -5,15 +5,14 @@ -export([init/3, handle/2, terminate/3]). init({_, http}, Req, _) -> - {Attr, Req2} = cowboy_req:qs_val(<<"attr">>, Req), - {ok, Req2, Attr}. + #{attr := Attr} = cowboy_req:match_qs(Req, [attr]), + {ok, Req, Attr}. handle(Req, <<"host_and_port">> = Attr) -> - {Host, Req2} = cowboy_req:host(Req), - {Port, Req3} = cowboy_req:port(Req2), + Host = cowboy_req:host(Req), + Port = cowboy_req:port(Req), Value = [Host, "\n", integer_to_list(Port)], - {ok, Req4} = cowboy_req:reply(200, [], Value, Req3), - {ok, Req4, Attr}. + {ok, cowboy_req:reply(200, [], Value, Req), Attr}. terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_set_resp.erl b/test/http_SUITE_data/http_set_resp.erl index 821cc1d..628f745 100644 --- a/test/http_SUITE_data/http_set_resp.erl +++ b/test/http_SUITE_data/http_set_resp.erl @@ -20,10 +20,10 @@ handle(Req, State) -> false -> {ok, Req, State}; true -> case cowboy_req:has_resp_body(Req) of - false -> {ok, Req, State}; + false -> + {ok, Req, State}; true -> - {ok, Req2} = cowboy_req:reply(200, Req), - {ok, Req2, State} + {ok, cowboy_req:reply(200, Req), State} end end. diff --git a/test/http_SUITE_data/http_stream_body.erl b/test/http_SUITE_data/http_stream_body.erl index d896797..9be79a1 100644 --- a/test/http_SUITE_data/http_stream_body.erl +++ b/test/http_SUITE_data/http_stream_body.erl @@ -25,8 +25,7 @@ handle(Req, State=#state{headers=_Headers, body=Body, reply=Reply}) -> SFun2 = fun(SendFun) -> lists:foreach(SendFun, Body) end, cowboy_req:set_resp_body_fun(chunked, SFun2, Req) end, - {ok, Req3} = cowboy_req:reply(200, Req2), - {ok, Req3, State}. + {ok, cowboy_req:reply(200, Req2), State}. terminate(_, _, _) -> ok. diff --git a/test/http_SUITE_data/http_streamed.erl b/test/http_SUITE_data/http_streamed.erl index 674cc40..5f90077 100644 --- a/test/http_SUITE_data/http_streamed.erl +++ b/test/http_SUITE_data/http_streamed.erl @@ -9,7 +9,7 @@ init({_Transport, http}, Req, _Opts) -> handle(Req, State) -> Req2 = cowboy_req:set([{resp_state, waiting_stream}], Req), - {ok, Req3} = cowboy_req:chunked_reply(200, Req2), + Req3 = cowboy_req:chunked_reply(200, Req2), timer:sleep(100), cowboy_req:chunk("streamed_handler\r\n", Req3), timer:sleep(100), diff --git a/test/http_SUITE_data/rest_forbidden_resource.erl b/test/http_SUITE_data/rest_forbidden_resource.erl index 920ba31..d055193 100644 --- a/test/http_SUITE_data/rest_forbidden_resource.erl +++ b/test/http_SUITE_data/rest_forbidden_resource.erl @@ -27,5 +27,4 @@ to_text(Req, State) -> {<<"This is REST!">>, Req, State}. from_text(Req, State) -> - {Path, Req2} = cowboy_req:path(Req), - {{true, Path}, Req2, State}. + {{true, cowboy_req:path(Req)}, Req, State}. diff --git a/test/http_SUITE_data/rest_param_all.erl b/test/http_SUITE_data/rest_param_all.erl index 09b8cd3..22daac7 100644 --- a/test/http_SUITE_data/rest_param_all.erl +++ b/test/http_SUITE_data/rest_param_all.erl @@ -17,17 +17,16 @@ content_types_provided(Req, State) -> {[{{<<"text">>, <<"plain">>, '*'}, get_text_plain}], Req, State}. get_text_plain(Req, State) -> - {{_, _, Param}, Req2} = - cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}), + {_, _, Param} = cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}), Body = if - Param == '*' -> - <<"'*'">>; - Param == [] -> - <<"[]">>; - Param /= [] -> - iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param]) + Param == '*' -> + <<"'*'">>; + Param == [] -> + <<"[]">>; + Param /= [] -> + iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param]) end, - {Body, Req2, State}. + {Body, Req, State}. content_types_accepted(Req, State) -> {[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}. diff --git a/test/http_SUITE_data/rest_patch_resource.erl b/test/http_SUITE_data/rest_patch_resource.erl index e265f6f..7b9b76e 100644 --- a/test/http_SUITE_data/rest_patch_resource.erl +++ b/test/http_SUITE_data/rest_patch_resource.erl @@ -16,17 +16,16 @@ get_text_plain(Req, State) -> content_types_accepted(Req, State) -> case cowboy_req:method(Req) of - {<<"PATCH">>, Req0} -> - {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req0, State}; - {_, Req0} -> - {[], Req0, State} + <<"PATCH">> -> + {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req, State}; + _ -> + {[], Req, State} end. patch_text_plain(Req, State) -> case cowboy_req:body(Req) of {ok, <<"halt">>, Req0} -> - {ok, Req1} = cowboy_req:reply(400, Req0), - {halt, Req1, State}; + {halt, cowboy_req:reply(400, Req0), State}; {ok, <<"false">>, Req0} -> {false, Req0, State}; {ok, _Body, Req0} -> diff --git a/test/http_SUITE_data/rest_resource_etags.erl b/test/http_SUITE_data/rest_resource_etags.erl index 2652f57..fb266d1 100644 --- a/test/http_SUITE_data/rest_resource_etags.erl +++ b/test/http_SUITE_data/rest_resource_etags.erl @@ -5,24 +5,25 @@ init(_Transport, _Req, _Opts) -> {upgrade, protocol, cowboy_rest}. generate_etag(Req, State) -> - case cowboy_req:qs_val(<<"type">>, Req) of + #{type := Type} = cowboy_req:match_qs(Req, [type]), + case Type of %% Correct return values from generate_etag/2. - {<<"tuple-weak">>, Req2} -> - {{weak, <<"etag-header-value">>}, Req2, State}; - {<<"tuple-strong">>, Req2} -> - {{strong, <<"etag-header-value">>}, Req2, State}; + <<"tuple-weak">> -> + {{weak, <<"etag-header-value">>}, Req, State}; + <<"tuple-strong">> -> + {{strong, <<"etag-header-value">>}, Req, State}; %% Backwards compatible return values from generate_etag/2. - {<<"binary-weak-quoted">>, Req2} -> - {<<"W/\"etag-header-value\"">>, Req2, State}; - {<<"binary-strong-quoted">>, Req2} -> - {<<"\"etag-header-value\"">>, Req2, State}; + <<"binary-weak-quoted">> -> + {<<"W/\"etag-header-value\"">>, Req, State}; + <<"binary-strong-quoted">> -> + {<<"\"etag-header-value\"">>, Req, State}; %% Invalid return values from generate_etag/2. - {<<"binary-strong-unquoted">>, Req2} -> + <<"binary-strong-unquoted">> -> cowboy_error_h:ignore(cowboy_http, quoted_string, 2), - {<<"etag-header-value">>, Req2, State}; - {<<"binary-weak-unquoted">>, Req2} -> + {<<"etag-header-value">>, Req, State}; + <<"binary-weak-unquoted">> -> cowboy_error_h:ignore(cowboy_http, quoted_string, 2), - {<<"W/etag-header-value">>, Req2, State} + {<<"W/etag-header-value">>, Req, State} end. content_types_provided(Req, State) -> diff --git a/test/ws_SUITE_data/ws_init_shutdown.erl b/test/ws_SUITE_data/ws_init_shutdown.erl index 2b1dd99..30bf66e 100644 --- a/test/ws_SUITE_data/ws_init_shutdown.erl +++ b/test/ws_SUITE_data/ws_init_shutdown.erl @@ -10,8 +10,7 @@ init(_Any, _Req, _Opts) -> {upgrade, protocol, cowboy_websocket}. websocket_init(_TransportName, Req, _Opts) -> - {ok, Req2} = cowboy_req:reply(403, Req), - {shutdown, Req2}. + {shutdown, cowboy_req:reply(403, Req)}. websocket_handle(_Frame, _Req, _State) -> exit(badarg). |