Permalink
Browse files

handle same key for cookies in different domains (TSUN-122)

SVN Revision: 1040
  • Loading branch information...
1 parent 0c95475 commit 81b35d87468a70f03ae0ef7b676b1e6b576791a7 @nniclausse nniclausse committed Aug 11, 2009
Showing with 80 additions and 10 deletions.
  1. +71 −4 src/test/ts_test_http.erl
  2. +9 −6 src/tsung/ts_http_common.erl
View
@@ -34,14 +34,81 @@ test()->ok.
subst_redirect_test()->
myset_env(),
URL="%%_redirect%%",
- Cookie=#cookie{domain="erlang.org",path="/",key="toto",value="bar"},
- Proto=#http_dyndata{cookies=[Cookie],user_agent="Firefox"},
+ Cookie="toto=bar; path=/; domain=erlang.org",
+ Cookies=ts_http_common:add_new_cookie(Cookie,"erlang.org",[]),
+ Proto=#http_dyndata{cookies=Cookies,user_agent="Firefox"},
DynVars=ts_dynvars:new(redirect,"http://erlang.org/bidule/truc"),
{Req,_}=ts_http:add_dynparams(true,#dyndata{proto=Proto,dynvars=DynVars},
#http_request{url=URL},
{"erlang.org",80}),
- Str="GET /bidule/truc HTTP/1.1\r\nHost: erlang.org:80\r\nUser-Agent: Firefox\r\nCookie: toto=bar\r\n\r\n",
- ?assertMatch(Str, binary_to_list(ts_http:get_message(Req))).
+ ?assertEqual("GET /bidule/truc HTTP/1.1\r\nHost: erlang.org:80\r\nUser-Agent: Firefox\r\nCookie: toto=bar\r\n\r\n", binary_to_list(ts_http:get_message(Req))).
+
+cookie_subdomain_test()->
+ myset_env(),
+ URL="/bidule/truc",
+ Cookie="toto=bar; path=/; domain=.domain.org",
+ Cookies=ts_http_common:add_new_cookie(Cookie,"domain.org",[]),
+ Proto=#http_dyndata{cookies=Cookies,user_agent="Firefox"},
+ DynVars=ts_dynvars:new(),
+ Req=ts_http:add_dynparams(false,#dyndata{proto=Proto,dynvars=DynVars},
+ #http_request{url=URL},
+ {"www.domain.org",80}),
+ Str="GET /bidule/truc HTTP/1.1\r\nHost: www.domain.org:80\r\nUser-Agent: Firefox\r\nCookie: toto=bar\r\n\r\n",
+ ?assertEqual(Str, binary_to_list(ts_http:get_message(Req))).
+
+
+add_cookie_samekey_samedomain_test()->
+ myset_env(),
+ Cookie1="RMID=732423sdfs73242; path=/; domain=.example.net",
+ Cookie2="RMID=42; path=/; domain=.example.net",
+ Val1=#cookie{key="RMID",value="732423sdfs73242",domain=".example.net",path="/"},
+ Val2=#cookie{key="RMID",value="42",domain=".example.net",path="/"},
+ Cookies=ts_http_common:add_new_cookie(Cookie1,"foobar.com",[]),
+ %% same domain, second cookie should erase the first one
+ Res=ts_http_common:add_new_cookie(Cookie2,"foobar.com",Cookies),
+ ?assertMatch([Val2],Res).
+
+set_cookie_test()->
+ myset_env(),
+ Cookie="RMID=732423sdfs73242; path=/; domain=.foobar.com",
+ Val="Cookie: RMID=732423sdfs73242\r\n",
+ Cookies=ts_http_common:add_new_cookie(Cookie,"www.foobar.com",[]),
+ ?assertEqual(Val,lists:flatten(ts_http_common:set_cookie_header({Cookies,"www.foobar.com","/toto.html"}))).
+
+add_cookie_test()->
+ myset_env(),
+ Cookie1="RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net",
+ Cookie2="ID=42; path=/; domain=.example.net",
+ Val1=#cookie{key="RMID",value="732423sdfs73242",domain=".example.net",path="/",expires="Fri, 31-Dec-2010 23:59:59 GMT"},
+ Val2=#cookie{key="ID",value="42",domain=".example.net",path="/"},
+ Cookies=ts_http_common:add_new_cookie(Cookie1,"foobar.com",[]),
+ ?assertEqual([Val2,Val1],ts_http_common:add_new_cookie(Cookie2,"foobar.com",Cookies)).
+
+add_cookie_samekey_nodomain_test()->
+ myset_env(),
+ Cookie1="RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net",
+ Cookie2="RMID=42; path=/; domain=.foobar.net",
+ Val1=#cookie{key="RMID",value="732423sdfs73242",domain=".example.net",path="/",expires="Fri, 31-Dec-2010 23:59:59 GMT"},
+ Val2=#cookie{key="RMID",value="42",domain=".foobar.net",path="/"},
+ Cookies=ts_http_common:add_new_cookie(Cookie1,"foobar.com",[]),
+ %% two different domains, two cookies
+ ?assertEqual([Val2,Val1],ts_http_common:add_new_cookie(Cookie2,"foobar.com",Cookies)).
+
+add_cookie_samekey_nodomain_req_test()->
+ myset_env(),
+ URL="/bidule/truc",
+ Cookie1="RMID=732423sdfs73242; expires=Fri, 31-Dec-2010 23:59:59 GMT; path=/; domain=.example.net",
+ Cookie2="RMID=42; path=/; domain=.foobar.net",
+ Cookies1=ts_http_common:add_new_cookie(Cookie1,"",[]),
+ Cookies = ts_http_common:add_new_cookie(Cookie2,"",Cookies1),
+ Proto=#http_dyndata{cookies=Cookies,user_agent="Firefox"},
+ DynVars=ts_dynvars:new(),
+ Req=ts_http:add_dynparams(false,#dyndata{proto=Proto,dynvars=DynVars},
+ #http_request{url=URL},
+ {"www.foobar.net",80}),
+ Str="GET /bidule/truc HTTP/1.1\r\nHost: www.foobar.net:80\r\nUser-Agent: Firefox\r\nCookie: RMID=42\r\n\r\n",
+ ?assertEqual(Str, binary_to_list(ts_http:get_message(Req))).
+
myset_env()->
application:set_env(stdlib,debug_level,0).
@@ -393,15 +393,18 @@ concat_cookies([], DynData) -> DynData;
concat_cookies(New, []) -> New;
concat_cookies([New=#cookie{}|Rest], OldCookies)->
case lists:keysearch(New#cookie.key, #cookie.key, OldCookies) of
- {value, _OldVal} ->
- ?DebugF("Reset key ~p with new value ~p~n",[New#cookie.key,
- New#cookie.value]),
- NewList = lists:keyreplace(New#cookie.key, #cookie.key, OldCookies, New),
- concat_cookies(Rest, NewList);
+ {value, #cookie{domain=Dom}} when Dom == New#cookie.domain -> %same domain
+ ?DebugF("Reset key ~p with new value ~p~n",[New#cookie.key,
+ New#cookie.value]),
+ NewList = lists:keyreplace(New#cookie.key, #cookie.key, OldCookies, New),
+ concat_cookies(Rest, NewList);
+ {value, _Val} -> % same key, but different domains
+ concat_cookies(Rest, [New | OldCookies]);
false ->
- concat_cookies(Rest, [New | OldCookies])
+ concat_cookies(Rest, [New | OldCookies])
end.
+
%%----------------------------------------------------------------------
%% Func: parse_set_cookie/2
%% cf. RFC 2965

0 comments on commit 81b35d8

Please sign in to comment.