Skip to content

Commit

Permalink
test: add tests for proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed Sep 20, 2024
1 parent 2e75778 commit 1b24565
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/erlang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ jobs:
with:
otp-version: ${{ matrix.otp }}
rebar3-version: 3
- name: setup tinyproxy
run:
sudo apt update && sudo apt install -yq tinyproxy
- name: Compile
run: rebar3 compile
- name: Run tests
Expand Down
8 changes: 6 additions & 2 deletions src/ehttpc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -856,12 +856,16 @@ gun_await_connect_proxy(Pid, StreamRef, ExpireAt, Timeout, Protocol, ProxyOpts,
State = enqueue_req(ResultCallback, Req, State0),
%% keep waiting
NewTimeout = timeout(ExpireAt),
gun_await_connect_proxy(Pid, StreamRef, ExpireAt, NewTimeout, Protocol, ProxyOpts, State);
gun_await_connect_proxy(
Pid, StreamRef, ExpireAt, NewTimeout, Protocol, ProxyOpts, State
);
?GEN_CALL_REQ(From, Call) ->
State = enqueue_req(From, Call, State0),
%% keep waiting
NewTimeout = timeout(ExpireAt),
gun_await_connect_proxy(Pid, StreamRef, ExpireAt, NewTimeout, Protocol, ProxyOpts, State)
gun_await_connect_proxy(
Pid, StreamRef, ExpireAt, NewTimeout, Protocol, ProxyOpts, State
)
after Timeout ->
{{error, connect_timeout}, State0}
end.
Expand Down
61 changes: 60 additions & 1 deletion test/ehttpc_google_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,53 @@ concurrent_callers_test_() ->
{timeout, TestTimeout, fun() -> with_pool(Opts4, F) end}
].

proxy_test_() ->
N = 50,
TestTimeout = 1000,
Host = ?HOST,
Port = ?PORT,
ProxyOpts0 = #{host => "127.0.0.1", port => 8888},
ProxyOpts1 = ProxyOpts0#{username => "user", password => "pass"},
%% host port enable_pipelining prioritise_latest
Opts1_ = pool_opts(Host, Port, true, true),
Opts2_ = pool_opts(Host, Port, true, false),
Opts3_ = pool_opts(Host, Port, false, true),
Opts4_ = pool_opts(Host, Port, false, false),
[Opts1, Opts2, Opts3, Opts4] = [
[{proxy, ProxyOpts0} | O]
|| O <- [Opts1_, Opts2_, Opts3_, Opts4_]
],
[Opts5, Opts6, Opts7, Opts8] = [
[{proxy, ProxyOpts1} | O]
|| O <- [Opts1_, Opts2_, Opts3_, Opts4_]
],
F = fun() -> req_async(?METHOD, N) end,
NoAuthConfPath = filename:absname("test/scripts/tinyproxy.conf"),
BasicAuthConfPath = filename:absname("test/scripts/tinyproxy_with_auth.conf"),
{inorder, [
{setup, fun() -> setup_proxy(NoAuthConfPath) end, fun stop_proxy/1, [
{timeout, TestTimeout, ?_test(with_pool(Opts1, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts2, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts3, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts4, F))}
]},
{setup, fun() -> setup_proxy(BasicAuthConfPath) end, fun stop_proxy/1, [
{"missing auth",
?_test(
with_pool(Opts1, fun() ->
?assertMatch(
{error, {proxy_error, unauthorized}},
do_req_sync(get, 1_000)
)
end)
)},
{timeout, TestTimeout, ?_test(with_pool(Opts5, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts6, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts7, F))},
{timeout, TestTimeout, ?_test(with_pool(Opts8, F))}
]}
]}.

req(get) ->
{?PATH, [{<<"Connection">>, <<"Keep-Alive">>}]};
req(post) ->
Expand All @@ -54,12 +101,15 @@ req(post) ->
req_sync(_Method, 0, _Timeout) ->
ok;
req_sync(Method, N, Timeout) ->
case ehttpc:request(?POOL, Method, req(Method), Timeout, _Retry = 0) of
case do_req_sync(Method, Timeout) of
{ok, _, _Headers, _Body} -> ok;
{error, timeout} -> timeout
end,
req_sync(Method, N - 1, Timeout).

do_req_sync(Method, Timeout) ->
ehttpc:request(?POOL, Method, req(Method), Timeout, _Retry = 0).

req_async(Method, N) ->
{Time, Results} = timer:tc(fun() -> req_async(Method, N, 5_000) end),
{OK, Timeout} = lists:partition(fun(I) -> I =:= ok end, Results),
Expand Down Expand Up @@ -97,3 +147,12 @@ with_pool(Opts, F) ->
after
ehttpc_sup:stop_pool(?POOL)
end.

setup_proxy(ConfPath) ->
_ = os:cmd("tinyproxy -c" ++ ConfPath),
ok.

stop_proxy(_) ->
_ = os:cmd("pkill tinyproxy"),
timer:sleep(500),
ok.
4 changes: 4 additions & 0 deletions test/scripts/tinyproxy.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Port 8888
Listen 127.0.0.1
Timeout 600
Allow 127.0.0.1
6 changes: 6 additions & 0 deletions test/scripts/tinyproxy_with_auth.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Port 8888
Listen 127.0.0.1
Timeout 600
Allow 127.0.0.1

BasicAuth user pass

0 comments on commit 1b24565

Please sign in to comment.