The example test suite shows some tests of an HTTP client that uses a proxy.
-module(httpc_proxy_SUITE).
%% Note: This directive should only be used in test suites.
-compile(export_all).
-include("ct.hrl").
-define(URL, "http://www.erlang.org").
-define(PROXY, "www-proxy.ericsson.se").
-define(PROXY_PORT, 8080).
%%--------------------------------------------------------------------
%% Test server callback functions
%%--------------------------------------------------------------------
%%--------------------------------------------------------------------
%% Function: suite() -> DefaultData
%% DefaultData: [tuple()]
%% Description: Require variables and set default values for the suite
%%--------------------------------------------------------------------
suite() -> [{timetrap,{minutes,1}}].
%%--------------------------------------------------------------------
%% Function: init_per_suite(Config) -> Config
%% Config: [tuple()]
%% A list of key/value pairs, holding the test case configuration.
%% Description: Initiation before the whole suite
%%
%% Note: This function is free to add any key/value pairs to the Config
%% variable, but should NOT alter/remove any existing entries.
%%--------------------------------------------------------------------
init_per_suite(Config) ->
application:start(inets),
http:set_options([{proxy, {{?PROXY, ?PROXY_PORT}, ["localhost"]}}]),
Config.
%%--------------------------------------------------------------------
%% Function: end_per_suite(Config) -> _
%% Config: [tuple()]
%% A list of key/value pairs, holding the test case configuration.
%% Description: Cleanup after the whole suite
%%--------------------------------------------------------------------
end_per_suite(_Config) ->
application:stop(inets),
ok.
%%--------------------------------------------------------------------
%% Function: all() -> TestCases
%% TestCases: [Case]
%% Case: atom()
%% Name of a test case.
%% Description: Returns a list of all test cases in this test suite
%%--------------------------------------------------------------------
all() ->
[options, head, get, trace].
%%-------------------------------------------------------------------------
%% Test cases starts here.
%%-------------------------------------------------------------------------
options() ->
[{userdata,[{doc,"Perform an OPTIONS request that goes through a proxy."}]}].
options(_Config) ->
{ok, {{_,200,_}, Headers, _}} =
http:request(options, {?URL, []}, [], []),
case lists:keysearch("allow", 1, Headers) of
{value, {"allow", _}} ->
ok;
_ ->
ct:fail(http_options_request_failed)
end.
head() ->
[{userdata,[{doc,"Perform a HEAD request that goes through a proxy."}]}].
head(_Config) ->
{ok, {{_,200, _}, [_ | _], []}} =
http:request(head, {?URL, []}, [], []).
get() ->
[{userdata,[{doc, "Perform a GET request that goes through a proxy."}]}].
get(_Config) ->
{ok, {{_,200,_}, [_ | _], Body = [_ | _]}} =
http:request(get, {?URL, []}, [], []),
check_body(Body).
trace() ->
[{userdata,[{doc, "Perform a TRACE request that goes through a proxy."}]}].
trace(_Config) ->
{ok, {{_,200,_}, [_ | _], "TRACE /" ++ _}} =
http:request(trace, {?URL, []}, [], []),
ok.
%%--------------------------------------------------------------------
%% Internal functions
%%--------------------------------------------------------------------
check_body(Body) ->
case string:rstr(Body, "\html>") of
0 ->
ct:fail(did_not_receive_whole_body);
_ ->
ok
end.