Skip to content

Commit

Permalink
Fix echo_post example
Browse files Browse the repository at this point in the history
  • Loading branch information
essen committed Jun 8, 2016
1 parent 1470f88 commit 4ced1d0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
6 changes: 3 additions & 3 deletions examples/echo_post/src/echo_post_app.erl
Expand Up @@ -16,9 +16,9 @@ start(_Type, _Args) ->
{"/", toppage_handler, []}
]}
]),
{ok, _} = cowboy:start_http(http, 100, [{port, 8080}], [
{env, [{dispatch, Dispatch}]}
]),
{ok, _} = cowboy:start_clear(http, 100, [{port, 8080}], #{
env => #{dispatch => Dispatch}
}),
echo_post_sup:start_link().

stop(_State) ->
Expand Down
15 changes: 9 additions & 6 deletions examples/echo_post/src/toppage_handler.erl
Expand Up @@ -14,16 +14,19 @@ init(Req, Opts) ->
maybe_echo(<<"POST">>, true, Req) ->
{ok, PostVals, Req2} = cowboy_req:body_qs(Req),
Echo = proplists:get_value(<<"echo">>, PostVals),
echo(Echo, Req2);
echo(Echo, Req2),
Req2;
maybe_echo(<<"POST">>, false, Req) ->
cowboy_req:reply(400, [], <<"Missing body.">>, Req);
cowboy_req:reply(400, [], <<"Missing body.">>, Req),
Req;
maybe_echo(_, _, Req) ->
%% Method not allowed.
cowboy_req:reply(405, Req).
cowboy_req:reply(405, Req),
Req.

echo(undefined, Req) ->
cowboy_req:reply(400, [], <<"Missing echo parameter.">>, Req);
echo(Echo, Req) ->
cowboy_req:reply(200, [
{<<"content-type">>, <<"text/plain; charset=utf-8">>}
], Echo, Req).
cowboy_req:reply(200, #{
<<"content-type">> => <<"text/plain; charset=utf-8">>
}, Echo, Req).
19 changes: 19 additions & 0 deletions test/examples_SUITE.erl
Expand Up @@ -111,3 +111,22 @@ do_echo_get(Transport, Protocol, Config) ->
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
ok.

echo_post(Config) ->
doc("POST parameter echo example."),
try
do_compile_and_start(echo_post),
do_echo_post(tcp, http, Config),
do_echo_post(tcp, http2, Config)
after
do_stop(echo_post)
end.

do_echo_post(Transport, Protocol, Config) ->
ConnPid = gun_open([{port, 8080}, {type, Transport}, {protocol, Protocol}|Config]),
Ref = gun:post(ConnPid, "/", [
{<<"content-type">>, <<"application/octet-stream">>}
], <<"echo=this+is+fun">>),
{response, nofin, 200, _} = gun:await(ConnPid, Ref),
{ok, <<"this is fun">>} = gun:await_body(ConnPid, Ref),
ok.

0 comments on commit 4ced1d0

Please sign in to comment.