diff options
author | Loïc Hoguin <[email protected]> | 2024-01-08 15:13:18 +0100 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2024-01-08 15:13:18 +0100 |
commit | e4a78aaeb110a3eda5269b618230b8bcb18fbcc2 (patch) | |
tree | 9c85e90adbe50b118ba0846473b17929bbda5e7d /test | |
parent | c1490d7d5503636e7995583222cf8edf5f882db5 (diff) | |
download | cowboy-e4a78aaeb110a3eda5269b618230b8bcb18fbcc2.tar.gz cowboy-e4a78aaeb110a3eda5269b618230b8bcb18fbcc2.tar.bz2 cowboy-e4a78aaeb110a3eda5269b618230b8bcb18fbcc2.zip |
Document body reading in auto mode
It is now tested both via cowboy_req:read_body and
via cowboy_req:cast.
Removes a bad example from the guide of body reading
with period of infinity, which does not work.
Diffstat (limited to 'test')
-rw-r--r-- | test/handlers/echo_h.erl | 21 | ||||
-rw-r--r-- | test/req_SUITE.erl | 8 |
2 files changed, 29 insertions, 0 deletions
diff --git a/test/handlers/echo_h.erl b/test/handlers/echo_h.erl index 1b672d1..f50bc79 100644 --- a/test/handlers/echo_h.erl +++ b/test/handlers/echo_h.erl @@ -25,6 +25,8 @@ echo(<<"read_body">>, Req0, Opts) -> timer:sleep(500), cowboy_req:read_body(Req0); <<"/full", _/bits>> -> read_body(Req0, <<>>); + <<"/auto-sync", _/bits>> -> read_body_auto_sync(Req0, <<>>); + <<"/auto-async", _/bits>> -> read_body_auto_async(Req0, <<>>); <<"/length", _/bits>> -> {_, _, Req1} = read_body(Req0, <<>>), Length = cowboy_req:body_length(Req1), @@ -122,6 +124,25 @@ read_body(Req0, Acc) -> {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>) end. +read_body_auto_sync(Req0, Acc) -> + Opts = #{length => auto, period => infinity}, + case cowboy_req:read_body(Req0, Opts) of + {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req}; + {more, Data, Req} -> read_body_auto_sync(Req, << Acc/binary, Data/binary >>) + end. + +read_body_auto_async(Req, Acc) -> + read_body_auto_async(Req, make_ref(), Acc). + +read_body_auto_async(Req, ReadBodyRef, Acc) -> + cowboy_req:cast({read_body, self(), ReadBodyRef, auto, infinity}, Req), + receive + {request_body, ReadBodyRef, nofin, Data} -> + read_body_auto_async(Req, ReadBodyRef, <<Acc/binary, Data/binary>>); + {request_body, ReadBodyRef, fin, _, Data} -> + {ok, <<Acc/binary, Data/binary>>, Req} + end. + value_to_iodata(V) when is_integer(V) -> integer_to_binary(V); value_to_iodata(V) when is_atom(V) -> atom_to_binary(V, latin1); value_to_iodata(V) when is_list(V); is_tuple(V); is_map(V) -> io_lib:format("~999999p", [V]); diff --git a/test/req_SUITE.erl b/test/req_SUITE.erl index d608767..92c8092 100644 --- a/test/req_SUITE.erl +++ b/test/req_SUITE.erl @@ -64,6 +64,8 @@ init_dispatch(Config) -> {"/opts/:key/timeout", echo_h, #{timeout => 1000, crash => true}}, {"/100-continue/:key", echo_h, []}, {"/full/:key", echo_h, []}, + {"/auto-sync/:key", echo_h, []}, + {"/auto-async/:key", echo_h, []}, {"/spawn/:key", echo_h, []}, {"/no/:key", echo_h, []}, {"/direct/:key/[...]", echo_h, []}, @@ -524,6 +526,12 @@ do_read_body_timeout(Path, Body, Config) -> {response, _, 500, _} = gun:await(ConnPid, Ref, infinity), gun:close(ConnPid). +read_body_auto(Config) -> + doc("Read the request body using auto mode."), + <<0:80000000>> = do_body("POST", "/auto-sync/read_body", [], <<0:80000000>>, Config), + <<0:80000000>> = do_body("POST", "/auto-async/read_body", [], <<0:80000000>>, Config), + ok. + read_body_spawn(Config) -> doc("Confirm we can use cowboy_req:read_body/1,2 from another process."), <<"hello world!">> = do_body("POST", "/spawn/read_body", [], "hello world!", Config), |