diff options
Diffstat (limited to 'lib/inets/test/httpc_cookie_SUITE.erl')
-rw-r--r-- | lib/inets/test/httpc_cookie_SUITE.erl | 201 |
1 files changed, 157 insertions, 44 deletions
diff --git a/lib/inets/test/httpc_cookie_SUITE.erl b/lib/inets/test/httpc_cookie_SUITE.erl index 7d91631934..ad5df656c6 100644 --- a/lib/inets/test/httpc_cookie_SUITE.erl +++ b/lib/inets/test/httpc_cookie_SUITE.erl @@ -1,19 +1,19 @@ %% %% %CopyrightBegin% -%% -%% Copyright Ericsson AB 2005-2009. All Rights Reserved. -%% +%% +%% Copyright Ericsson AB 2005-2010. All Rights Reserved. +%% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in %% compliance with the License. You should have received a copy of the %% Erlang Public License along with this software. If not, it can be %% retrieved online at http://www.erlang.org/. -%% +%% %% Software distributed under the License is distributed on an "AS IS" %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See %% the License for the specific language governing rights and limitations %% under the License. -%% +%% %% %CopyrightEnd% %% %% @@ -31,7 +31,7 @@ domain_cookie/1, secure_cookie/1, update_cookie/1, update_cookie_session/1, cookie_attributes/1]). --define(URL, "http://myhost.cookie.test.org"). +-define(URL, "http://myhost.cookie.test.org"). -define(URL_DOMAIN, "http://myhost2.cookie.test.org"). -define(URL_SECURE, "https://myhost.cookie.test.org"). @@ -50,15 +50,21 @@ %% variable, but should NOT alter/remove any existing entries. %% Description: Initiation before each test case %%-------------------------------------------------------------------- -init_per_testcase(session_cookies_only, Config) -> +init_per_testcase(session_cookies_only = Case, Config0) -> + tsp("init_per_testcase(~p) -> entry with" + "~n Config0: ~p", [Case, Config0]), + Config = init_workdir(Case, Config0), application:start(inets), http:set_options([{cookies, verify}]), watch_dog(Config); -init_per_testcase(_, Config) -> - PrivDir = ?config(priv_dir, Config), +init_per_testcase(Case, Config0) -> + tsp("init_per_testcase(~p) -> entry with" + "~n Config0: ~p", [Case, Config0]), + Config = init_workdir(Case, Config0), + CaseDir = ?config(case_top_dir, Config), application:load(inets), - application:set_env(inets, services, [{httpc,{default, PrivDir}}]), + application:set_env(inets, services, [{httpc, {default, CaseDir}}]), application:start(inets), http:set_options([{cookies, verify}]), watch_dog(Config). @@ -68,6 +74,24 @@ watch_dog(Config) -> NewConfig = lists:keydelete(watchdog, 1, Config), [{watchdog, Dog} | NewConfig]. +init_workdir(Case, Config) -> + PrivDir = ?config(priv_dir, Config), + SuiteTopDir = filename:join(PrivDir, ?MODULE), + case file:make_dir(SuiteTopDir) of + ok -> + ok; + {error, eexist} -> + ok; + Error -> + tsf({failed_creating_subsuite_top_dir, Error}) + end, + + CaseTopDir = filename:join(SuiteTopDir, Case), + ?line ok = file:make_dir(CaseTopDir), + [{suite_top_dir, SuiteTopDir}, + {case_top_dir, CaseTopDir} | Config]. + + %%-------------------------------------------------------------------- %% Function: end_per_testcase(TestCase, Config) -> _ %% Case - atom() @@ -76,10 +100,10 @@ watch_dog(Config) -> %% A list of key/value pairs, holding the test case configuration. %% Description: Cleanup after each test case %%-------------------------------------------------------------------- -end_per_testcase(_TestCase, Config) -> +end_per_testcase(Case, Config) -> + tsp("end_per_testcase(~p) -> entry with" + "~n Config: ~p", [Case, Config]), application:stop(inets), - File = filename:join(?config(priv_dir, Config), "http_default_cookie_db"), - file:delete(File), Dog = ?config(watchdog, Config), test_server:timetrap_cancel(Dog), ok. @@ -96,9 +120,18 @@ all(doc) -> ["Describe the main purpose of this suite"]; all(suite) -> - [session_cookies_only, netscape_cookies, cookie_cancel, - cookie_expires, persistent_cookie, domain_cookie, secure_cookie, - update_cookie, update_cookie_session, cookie_attributes]. + [ + session_cookies_only, + netscape_cookies, + cookie_cancel, + cookie_expires, + persistent_cookie, + domain_cookie, + secure_cookie, + update_cookie, + update_cookie_session, + cookie_attributes + ]. %% Test cases starts here. %%-------------------------------------------------------------------- @@ -108,6 +141,8 @@ session_cookies_only(doc) -> session_cookies_only(suite) -> []; session_cookies_only(Config) when is_list(Config) -> + tsp("session_cookies_only -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/;" ";max-age=60000"}], http:verify_cookies(SetCookieHeaders, ?URL), @@ -115,8 +150,9 @@ session_cookies_only(Config) when is_list(Config) -> = http:cookie_header(?URL), application:stop(inets), application:start(inets), - {"cookie",""} - = http:cookie_header(?URL), + {"cookie",""} = http:cookie_header(?URL), + + tsp("session_cookies_only -> Cookies 2: ~p", [httpc:which_cookies()]), ok. netscape_cookies(doc) -> @@ -124,12 +160,16 @@ netscape_cookies(doc) -> netscape_cookies(suite) -> []; netscape_cookies(Config) when is_list(Config) -> + tsp("netscape_cookies -> Cookies 1: ~p", [httpc:which_cookies()]), + Expires = future_netscape_date(), SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/; " "expires=" ++ Expires}], http:verify_cookies(SetCookieHeaders, ?URL), - {"cookie","$Version=0; test_cookie=true; $Path=/"} - = http:cookie_header(?URL), + {"cookie","$Version=0; test_cookie=true; $Path=/"} = + http:cookie_header(?URL), + + tsp("netscape_cookies -> Cookies 2: ~p", [httpc:which_cookies()]), ok. cookie_cancel(doc) -> @@ -138,6 +178,8 @@ cookie_cancel(doc) -> cookie_cancel(suite) -> []; cookie_cancel(Config) when is_list(Config) -> + tsp("cookie_cancel -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/;" "max-age=60000"}], http:verify_cookies(SetCookieHeaders, ?URL), @@ -147,6 +189,8 @@ cookie_cancel(Config) when is_list(Config) -> "max-age=0"}], http:verify_cookies(NewSetCookieHeaders, ?URL), {"cookie", ""} = http:cookie_header(?URL), + + tsp("cookie_cancel -> Cookies 2: ~p", [httpc:which_cookies()]), ok. cookie_expires(doc) -> @@ -154,6 +198,8 @@ cookie_expires(doc) -> cookie_expires(suite) -> []; cookie_expires(Config) when is_list(Config) -> + tsp("cookie_expires -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/;" "max-age=5"}], http:verify_cookies(SetCookieHeaders, ?URL), @@ -161,56 +207,95 @@ cookie_expires(Config) when is_list(Config) -> = http:cookie_header(?URL), test_server:sleep(10000), {"cookie", ""} = http:cookie_header(?URL), + + tsp("cookie_expires -> Cookies 2: ~p", [httpc:which_cookies()]), ok. -persistent_cookie(doc)-> +persistent_cookie(doc) -> ["Test domian cookie attribute"]; persistent_cookie(suite) -> []; persistent_cookie(Config) when is_list(Config)-> + tsp("persistent_cookie -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/;" "max-age=60000"}], http:verify_cookies(SetCookieHeaders, ?URL), - {"cookie","$Version=0; test_cookie=true; $Path=/"} - = http:cookie_header(?URL), - PrivDir = ?config(priv_dir, Config), + {"cookie","$Version=0; test_cookie=true; $Path=/"} = + http:cookie_header(?URL), + CaseDir = ?config(case_top_dir, Config), application:stop(inets), application:load(inets), - application:set_env(inets, services, [{httpc,{default, PrivDir}}]), + application:set_env(inets, services, [{httpc, {default, CaseDir}}]), application:start(inets), - http:set_options([{cookies, enabled}]), - {"cookie","$Version=0; test_cookie=true; $Path=/"} - = http:cookie_header(?URL), + http:set_options([{cookies, enabled}]), + {"cookie","$Version=0; test_cookie=true; $Path=/"} = http:cookie_header(?URL), + + tsp("persistent_cookie -> Cookies 2: ~p", [httpc:which_cookies()]), ok. -domain_cookie(doc)-> + +domain_cookie(doc) -> ["Test the domian cookie attribute"]; domain_cookie(suite) -> []; -domain_cookie(Config) when is_list(Config)-> +domain_cookie(Config) when is_list(Config) -> + tsp("domain_cookie -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/;" "domain=.cookie.test.org"}], http:verify_cookies(SetCookieHeaders, ?URL), {"cookie","$Version=0; test_cookie=true; $Path=/; " - "$Domain=.cookie.test.org"} - = http:cookie_header(?URL_DOMAIN), + "$Domain=.cookie.test.org"} = + http:cookie_header(?URL_DOMAIN), + + tsp("domain_cookie -> Cookies 2: ~p", [httpc:which_cookies()]), ok. -secure_cookie(doc)-> + +secure_cookie(doc) -> ["Test the secure cookie attribute"]; secure_cookie(suite) -> []; -secure_cookie(Config) when is_list(Config)-> +secure_cookie(Config) when is_list(Config) -> + tsp("secure_cookie -> entry with" + "~n Config: ~p", [Config]), + + inets:enable_trace(max, io, httpc), + + %% httpc:reset_cookies(), + + tsp("secure_cookie -> Cookies 1: ~p", [httpc:which_cookies()]), + SetCookieHeaders = [{"set-cookie", "test_cookie=true; path=/; secure"}], - http:verify_cookies(SetCookieHeaders, ?URL), - {"cookie","$Version=0; test_cookie=true; $Path=/"} - = http:cookie_header(?URL_SECURE), - {"cookie",""} - = http:cookie_header(?URL), + tsp("secure_cookie -> verify cookies (1)"), + ok = http:verify_cookies(SetCookieHeaders, ?URL), + + tsp("secure_cookie -> Cookies 2: ~p", [httpc:which_cookies()]), + + tsp("secure_cookie -> check cookie (secure)"), + check_cookie("$Version=0; test_cookie=true; $Path=/", ?URL_SECURE), + + tsp("secure_cookie -> check cookie (plain)"), + check_cookie("", ?URL), + + tsp("secure_cookie -> verify cookies (2)"), SetCookieHeaders1 = [{"set-cookie", "test1_cookie=true; path=/; secure"}], - http:verify_cookies(SetCookieHeaders1, ?URL), - {"cookie","$Version=0; test_cookie=true; $Path=/; " - "test1_cookie=true; $Path=/"} = http:cookie_header(?URL_SECURE), + ok = http:verify_cookies(SetCookieHeaders1, ?URL), + + tsp("secure_cookie -> Cookies 3: ~p", [httpc:which_cookies()]), + + tsp("secure_cookie -> cookie header (3)"), + check_cookie("$Version=0; test_cookie=true; $Path=/; " + "test1_cookie=true; $Path=/", + ?URL_SECURE), +%% {"cookie","$Version=0; test_cookie=true; $Path=/; " +%% "test1_cookie=true; $Path=/"} = http:cookie_header(?URL_SECURE), + + tsp("secure_cookie -> Cookies 4: ~p", [httpc:which_cookies()]), + + inets:disable_trace(), + tsp("secure_cookie -> done"), ok. update_cookie(doc)-> @@ -257,13 +342,31 @@ cookie_attributes(Config) when is_list(Config) -> "foo=bar;" %% Nonsense should be ignored "max-age=60000"}], http:verify_cookies(SetCookieHeaders, ?URL), - {"cookie","$Version=1; test_cookie=true"} - = http:cookie_header(?URL), + {"cookie","$Version=1; test_cookie=true"} = http:cookie_header(?URL), ok. + %%-------------------------------------------------------------------- %%% Internal functions %%-------------------------------------------------------------------- + +check_cookie(Expect, URL) -> + case http:cookie_header(URL) of + {"cookie", Expect} -> + ok; + {"cookie", Unexpected} -> + case lists:prefix(Expect, Unexpected) of + true -> + Extra = Unexpected -- Expect, + tsf({extra_cookie_info, Extra}); + false -> + tsf({unknown_cookie, Expect, Unexpected}) + end; + Bad -> + tsf({bad_cookies, Bad}) + end. + + future_netscape_date() -> [Day, DD, Mon, YYYY] = netscape_date(date()), lists:flatten(io_lib:format("~s, ~s ~s ~s 12:30:00 GMT", @@ -336,3 +439,13 @@ month_str(9) ->"Sep"; month_str(10) ->"Oct"; month_str(11) ->"Nov"; month_str(12) ->"Dec". + + +tsp(F) -> + tsp(F, []). +tsp(F, A) -> + test_server:format("~p ~p:" ++ F ++ "~n", [self(), ?MODULE | A]). + +tsf(Reason) -> + test_server:fail(Reason). + |