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

Simplify the SPDY suite using the new Gun interface

This commit is contained in:
Loïc Hoguin 2014-03-25 13:01:23 +01:00
parent abf246c9aa
commit a01f992ffb

View file

@ -100,24 +100,19 @@ init_dispatch(Config) ->
%% Convenience functions. %% Convenience functions.
quick_get(Pid, Host, Path) -> gun_monitor_open(Config) ->
MRef = monitor(process, Pid), {_, Port} = lists:keyfind(port, 1, Config),
StreamRef = gun:get(Pid, Path, [{":host", Host}]), {ok, ConnPid} = gun:open("localhost", Port, [{retry, 0}]),
receive {ConnPid, monitor(process, ConnPid)}.
{'DOWN', MRef, _, _, Reason} ->
error(Reason); quick_get(ConnPid, MRef, Host, Path) ->
{gun_response, Pid, StreamRef, IsFin, StreamRef = gun:get(ConnPid, Path, [{":host", Host}]),
<< Status:3/binary, _/bits >>, Headers} -> {response, IsFin, Status, _} = gun:await(ConnPid, StreamRef, MRef),
{IsFin, list_to_integer(binary_to_list(Status)), Headers} {IsFin, Status}.
after 1000 ->
error(timeout)
end.
%% Tests. %% Tests.
check_status(Config) -> check_status(Config) ->
{_, Port} = lists:keyfind(port, 1, Config),
{ok, Pid} = gun:open("localhost", Port),
Tests = [ Tests = [
{200, nofin, "localhost", "/"}, {200, nofin, "localhost", "/"},
{200, nofin, "localhost", "/chunked"}, {200, nofin, "localhost", "/chunked"},
@ -126,64 +121,33 @@ check_status(Config) ->
{400, fin, "localhost", "bad-path"}, {400, fin, "localhost", "bad-path"},
{404, fin, "localhost", "/this/path/does/not/exist"} {404, fin, "localhost", "/this/path/does/not/exist"}
], ],
{ConnPid, MRef} = gun_monitor_open(Config),
_ = [{Status, Fin, Host, Path} = begin _ = [{Status, Fin, Host, Path} = begin
{IsFin, Ret, _} = quick_get(Pid, Host, Path), {IsFin, Ret} = quick_get(ConnPid, MRef, Host, Path),
{Ret, IsFin, Host, Path} {Ret, IsFin, Host, Path}
end || {Status, Fin, Host, Path} <- Tests], end || {Status, Fin, Host, Path} <- Tests],
gun:close(Pid). gun:close(ConnPid).
echo_body(Config) -> echo_body(Config) ->
{_, Port} = lists:keyfind(port, 1, Config), {ConnPid, MRef} = gun_monitor_open(Config),
{ok, Pid} = gun:open("localhost", Port),
MRef = monitor(process, Pid),
Body = << 0:800000 >>, Body = << 0:800000 >>,
StreamRef = gun:post(Pid, "/echo/body", [ StreamRef = gun:post(ConnPid, "/echo/body", [
{<<"content-length">>, integer_to_list(byte_size(Body))},
{<<"content-type">>, "application/octet-stream"} {<<"content-type">>, "application/octet-stream"}
], Body), ], Body),
receive {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
{'DOWN', MRef, _, _, Reason} -> {ok, Body} = gun:await_body(ConnPid, StreamRef, MRef),
error(Reason); gun:close(ConnPid).
{gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
ok
after 1000 ->
error(response_timeout)
end,
receive
{'DOWN', MRef, _, _, Reason2} ->
error({gun_data, Reason2});
{gun_data, Pid, StreamRef, fin, Body} ->
ok
after 1000 ->
error(data_timeout)
end,
gun:close(Pid).
echo_body_multi(Config) -> echo_body_multi(Config) ->
{_, Port} = lists:keyfind(port, 1, Config), {ConnPid, MRef} = gun_monitor_open(Config),
{ok, Pid} = gun:open("localhost", Port),
MRef = monitor(process, Pid),
BodyChunk = << 0:80000 >>, BodyChunk = << 0:80000 >>,
StreamRef = gun:post(Pid, "/echo/body", [ StreamRef = gun:post(ConnPid, "/echo/body", [
%% @todo I'm still unhappy with this. It shouldn't be required...
{<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)}, {<<"content-length">>, integer_to_list(byte_size(BodyChunk) * 10)},
{<<"content-type">>, "application/octet-stream"} {<<"content-type">>, "application/octet-stream"}
]), ]),
_ = [gun:data(Pid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)], _ = [gun:data(ConnPid, StreamRef, nofin, BodyChunk) || _ <- lists:seq(1, 9)],
gun:data(Pid, StreamRef, fin, BodyChunk), gun:data(ConnPid, StreamRef, fin, BodyChunk),
receive {response, nofin, 200, _} = gun:await(ConnPid, StreamRef, MRef),
{'DOWN', MRef, _, _, Reason} -> {ok, << 0:800000 >>} = gun:await_body(ConnPid, StreamRef, MRef),
error(Reason); gun:close(ConnPid).
{gun_response, Pid, StreamRef, nofin, << "200", _/bits >>, _} ->
ok
after 1000 ->
error(response_timeout)
end,
receive
{'DOWN', MRef, _, _, Reason2} ->
error({gun_data, Reason2});
{gun_data, Pid, StreamRef, fin, << 0:800000 >>} ->
ok
after 1000 ->
error(data_timeout)
end,
gun:close(Pid).