Skip to content

Commit 74a94a9

Browse files
committed
Add tests for remote proxy
- Add "remote" proxy groups - Add ncat as dummy proxy
1 parent 123d7ef commit 74a94a9

File tree

2 files changed

+124
-9
lines changed

2 files changed

+124
-9
lines changed

lib/inets/test/httpc_proxy_SUITE.erl

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
-compile(export_all).
3636

3737
-define(LOCAL_PROXY_SCRIPT, "server_proxy.sh").
38+
-define(DUMMY_PROXY_SCRIPT, "dummy_proxy.sh").
3839
-define(p(F, A), % Debug printout
3940
begin
4041
io:format(
@@ -53,18 +54,24 @@ suite() ->
5354

5455
all() ->
5556
[{group,local_proxy},
56-
{group,local_proxy_https}].
57+
{group,local_proxy_https},
58+
{group, remote_proxy},
59+
{group, remote_proxy_https},
60+
{group, dummy_proxy}].
5761

5862
groups() ->
5963
[{local_proxy,[],
6064
[http_emulate_lower_versions
61-
|local_proxy_cases()]},
65+
|proxy_cases()]},
6266
{local_proxy_https,[],
63-
local_proxy_cases() ++ local_proxy_https_cases()}].
67+
proxy_cases() ++ proxy_https_cases()},
68+
{remote_proxy, [], proxy_cases()},
69+
{remote_proxy_https, [], proxy_cases() ++ proxy_https_cases()},
70+
{dummy_proxy, [], [proxy_upgrade_connect_error]}].
6471

6572
%% internal functions
6673

67-
local_proxy_cases() ->
74+
proxy_cases() ->
6875
[http_head,
6976
http_get,
7077
http_options,
@@ -79,7 +86,7 @@ local_proxy_cases() ->
7986
http_stream,
8087
http_not_modified_otp_6821].
8188

82-
local_proxy_https_cases() ->
89+
proxy_https_cases() ->
8390
[https_connect_error,
8491
http_timeout].
8592

@@ -108,20 +115,56 @@ suite_apps() ->
108115
init_per_group(local_proxy, Config) ->
109116
init_local_proxy([{protocol,http}|Config]);
110117
init_per_group(local_proxy_https, Config) ->
111-
init_local_proxy([{protocol,https}|Config]).
118+
init_local_proxy([{protocol,https}|Config]);
119+
120+
init_per_group(remote_proxy, Config) ->
121+
Config1 = init_local_proxy([{protocol,http}|Config]),
122+
case Config1 of
123+
{skip, _} -> Config1;
124+
_ ->
125+
{local,{{"localhost",Port},[]}} = proplists:get_value(proxy, Config1),
126+
lists:keyreplace(proxy, 1, Config1, {proxy, {local, {{"127.0.0.1", Port}, []}}})
127+
end;
128+
init_per_group(remote_proxy_https, Config) ->
129+
Config1 = init_local_proxy([{protocol,https}|Config]),
130+
case Config1 of
131+
{skip, _} -> Config1;
132+
_ ->
133+
{local,{{"localhost",Port},[]}} = proplists:get_value(proxy, Config1),
134+
lists:keyreplace(proxy, 1, Config1, {proxy, {local, {{"127.0.0.1", Port}, []}}})
135+
end;
136+
137+
init_per_group(dummy_proxy, Config) ->
138+
ProxyPort = 8000,
139+
ProxyAddress = "127.0.0.1",
140+
DummyPort = 8080,
141+
DummyServer = "localhost",
142+
[{proxy, {local, {{ProxyAddress, ProxyPort}, []}}}, {http, {DummyServer, DummyPort}} | Config].
143+
112144

113145
end_per_group(Group, Config)
114146
when
115147
Group =:= local_proxy;
116-
Group =:= local_proxy_https ->
148+
Group =:= local_proxy_https;
149+
Group =:= remote_proxy;
150+
Group =:= remote_proxy_https ->
117151
rcmd_local_proxy(["stop"], Config),
118152
Config;
119153
end_per_group(_, Config) ->
120154
Config.
121155

122156
%%--------------------------------------------------------------------
123157

124-
init_per_testcase(Case, Config0) ->
158+
init_per_testcase(proxy_upgrade_connect_error = Case, Config) ->
159+
Response = "HTTP/1.1 500",
160+
init_dummy_proxy(Response, Config),
161+
do_init_per_testcase(Case, Config);
162+
init_per_testcase(Case, Config) ->
163+
do_init_per_testcase(Case, Config).
164+
165+
do_init_per_testcase(_, {skip, _} = Config) ->
166+
Config;
167+
do_init_per_testcase(Case, Config0) ->
125168
ct:timetrap({seconds,30}),
126169
Apps = apps(Case, Config0),
127170
case init_apps(Apps, Config0) of
@@ -137,6 +180,12 @@ init_per_testcase(Case, Config0) ->
137180
E3
138181
end.
139182

183+
end_per_testcase(proxy_upgrade_connect_error, Config) ->
184+
DataDir = proplists:get_value(data_dir, Config),
185+
PrivDir = proplists:get_value(priv_dir, Config),
186+
Script = filename:join(DataDir, ?DUMMY_PROXY_SCRIPT),
187+
rcmd(Script, ["stop"], [{cd, PrivDir}]),
188+
Config;
140189
end_per_testcase(_Case, Config) ->
141190
app_stop(inets),
142191
Config.
@@ -447,6 +496,26 @@ https_connect_error(Config) when is_list(Config) ->
447496
{error,{failed_connect,[_,{tls,_,_}]}} =
448497
httpc:request(Method, Request, HttpOpts, Opts).
449498

499+
%%--------------------------------------------------------------------
500+
proxy_upgrade_connect_error(doc) ->
501+
["This targets verification of upgrade process
502+
when proxy sends back response code that is not 200"];
503+
proxy_upgrade_connect_error(Config) when is_list(Config) ->
504+
{HttpServer,HttpPort} = proplists:get_value(http, Config),
505+
Method = get,
506+
%% using HTTPS scheme to test upgrade connection
507+
URL = "https://" ++ HttpServer ++ ":" ++
508+
integer_to_list(HttpPort) ++ "/index.html",
509+
Opts = [],
510+
HttpOpts = [?SSL_NO_VERIFY],
511+
Request = {URL,[]},
512+
%% This is a dummy proxy so no further connection will be established
513+
%% We are only interested in testing parsing of the proxy response
514+
{error,{failed_connect,[_,{_,_,econnrefused}]}} =
515+
httpc:request(Method, Request, HttpOpts, Opts).
516+
517+
518+
450519
%%--------------------------------------------------------------------
451520
http_timeout(doc) ->
452521
["Test http/https connect and upgrade timeouts."];
@@ -455,10 +524,11 @@ http_timeout(Config) when is_list(Config) ->
455524
URL = url("/index.html", Config),
456525
Request = {URL,[]},
457526
Timeout = timer:seconds(1),
527+
{_,{{ProxyAddr, ProxyPort}, []}} = proplists:get_value(proxy, Config),
458528
HttpOpts1 = [{timeout, Timeout}, {connect_timeout, 0}, ?SSL_NO_VERIFY],
459529
{error,
460530
{failed_connect,
461-
[{to_address,{"localhost",8000}},
531+
[{to_address,{ProxyAddr, ProxyPort}},
462532
{inet,[inet],timeout}]}}
463533
= httpc:request(Method, Request, HttpOpts1, []),
464534
ok.
@@ -538,6 +608,30 @@ url(AbsPath, Config) ->
538608

539609
%%--------------------------------------------------------------------
540610

611+
init_dummy_proxy(Response, Config) ->
612+
case os:type() of
613+
{unix, _} ->
614+
case os:cmd("which ncat") of
615+
[] ->
616+
{skip, "Ncat not available on the system"};
617+
_ ->
618+
Proxy = proplists:get_value(proxy, Config),
619+
{_, {{ProxyAddress, ProxyPort}, _}} = Proxy,
620+
621+
DataDir = proplists:get_value(data_dir, Config),
622+
PrivDir = proplists:get_value(priv_dir, Config),
623+
Script = filename:join(DataDir, ?DUMMY_PROXY_SCRIPT),
624+
625+
spawn(fun() -> rcmd(Script, ["start",
626+
ProxyAddress,
627+
integer_to_list(ProxyPort),
628+
Response], [{cd, PrivDir}])
629+
end)
630+
end;
631+
_ ->
632+
{skip, "Platform cannot run dummy proxy script"}
633+
end.
634+
541635
init_local_proxy(Config) ->
542636
case os:type() of
543637
{unix,_} ->
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/sh
2+
3+
# %CopyrightBegin%
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
#
7+
# Copyright Ericsson AB 2025. All Rights Reserved.
8+
#
9+
# %CopyrightEnd%
10+
11+
case :"${1:?}" in
12+
:start)
13+
/bin/echo -ne "${4}\r\n\r\n" | ncat -l ${2} ${3} & echo $! > dummy_proxy.pid
14+
;;
15+
:stop)
16+
Pid=`pgrep -F dummy_proxy.pid`
17+
if [ ! "$Pid" = "" ]; then
18+
kill "$Pid" || true
19+
fi
20+
;;
21+
esac

0 commit comments

Comments
 (0)