Permalink
Browse files

handle dyn_variables as array in test conditions (TSUN-205)

  • Loading branch information...
1 parent f6990bd commit d102df0a49f5bc5934f114deb1c4416df6604ced @nniclausse nniclausse committed Dec 2, 2011
View
@@ -97,7 +97,7 @@
<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1><TR><TD ALIGN=left NOWRAP bgcolor="#F2F2F2"> Version:</TD><TD ALIGN=left NOWRAP>1.4.2</TD></TR>
<TR><TD ALIGN=left NOWRAP bgcolor="#F2F2F2"> Date :</TD><TD ALIGN=left NOWRAP>December
- 1, 2011</TD></TR>
+ 2, 2011</TD></TR>
</TABLE>
</DIV><!--TOC section Contents-->
<H2 CLASS="section"><!--SEC ANCHOR -->Contents</H2><!--SEC END --><UL CLASS="toc"><LI CLASS="li-toc">
@@ -3102,7 +3102,7 @@ <H5 CLASS="paragraph"><!--SEC ANCHOR -->&lt;if&gt;</H5><!--SEC END --><TABLE BOR
</TABLE></TD></TR>
<TR><TD BGCOLOR=black COLSPAN="1"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
</TD></TR>
-</TABLE></TD><TD><TABLE BORDER=0 CELLPADDING="1" CELLSPACING=0><TR><TD><PRE CLASS="verbatim">&lt;if var="tsung_userid" eq="3"&gt;
+</TABLE></TD><TD><TABLE BORDER=0 CELLPADDING="1" CELLSPACING=0><TR><TD><PRE CLASS="verbatim">&lt;if var="tsung_userid" eq="3"&gt;
&lt;request&gt; &lt;http url="/foo"/&gt; &lt;/request&gt;
&lt;request&gt; &lt;http url="/bar"/&gt; &lt;/request&gt;
&lt;/if&gt;
@@ -3114,7 +3114,24 @@ <H5 CLASS="paragraph"><!--SEC ANCHOR -->&lt;if&gt;</H5><!--SEC END --><TABLE BOR
</TD></TR>
</TABLE></TD></TR>
</TABLE></TD></TR>
-</TABLE><P>You can use <TT>eq</TT> or <TT>neq</TT> to check the variable.</P><!--TOC paragraph &lt;foreach&gt;-->
+</TABLE><P>You can use <TT>eq</TT> or <TT>neq</TT> to check the variable.</P><P>If the dynamic variable is a list (output from xpath for example), you
+can access to the nth element of a list like this:
+</P><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD><TABLE BORDER=0 CELLPADDING=0
+CELLSPACING=0><TR><TD BGCOLOR=black COLSPAN="3"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
+</TD></TR>
+</TABLE></TD></TR>
+<TR><TD BGCOLOR=black COLSPAN="1"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
+</TD></TR>
+</TABLE></TD><TD><TABLE BORDER=0 CELLPADDING="1" CELLSPACING=0><TR><TD><PRE CLASS="verbatim">&lt;if var="myvar[1]" eq="3"&gt;
+</PRE></TD></TR>
+</TABLE></TD><TD BGCOLOR=black COLSPAN="1"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
+</TD></TR>
+</TABLE></TD></TR>
+<TR><TD BGCOLOR=black COLSPAN="3"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
+</TD></TR>
+</TABLE></TD></TR>
+</TABLE></TD></TR>
+</TABLE><P>(here we compare the first element of the list to 3)</P><!--TOC paragraph &lt;foreach&gt;-->
<H5 CLASS="paragraph"><!--SEC ANCHOR -->&lt;foreach&gt;</H5><!--SEC END --><P>Repeat the enclosing actions for all the elements contained in the list specified. The basic syntax is as follows:</P><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0><TR><TD><TABLE BORDER=0 CELLPADDING=0
CELLSPACING=0><TR><TD BGCOLOR=black COLSPAN="3"><TABLE CELLSPACING="1" CELLPADDING=0 BORDER=0><TR><TD>
</TD></TR>
View
@@ -2411,14 +2411,21 @@ \subsubsection{Loops, If, Foreach}
\paragraph{<if>}
\begin{Verbatim}
-<if var="tsung_userid" eq="3">
+<if var="tsung_userid" eq="3">
<request> <http url="/foo"/> </request>
<request> <http url="/bar"/> </request>
</if>
\end{Verbatim}
You can use \varname{eq} or \varname{neq} to check the variable.
+If the dynamic variable is a list (output from xpath for example), you
+can access to the nth element of a list like this:
+\begin{Verbatim}
+<if var="myvar[1]" eq="3">
+\end{Verbatim}
+(here we compare the first element of the list to 3)
+
\paragraph{<foreach>}
Repeat the enclosing actions for all the elements contained in the list specified. The basic syntax is as follows:
@@ -24,6 +24,13 @@ dynvars_new_ok_test() ->
?assertEqual([{one,1},{two,2},{three,"three"},{four,4}],
ts_dynvars:new(Keys, Values)).
+dynvars_array_test() ->
+ Keys = [one,two,three,four],
+ Values = [[10,11,12],2,"three",4],
+ DynVars= ts_dynvars:new(Keys, Values),
+ ?assertEqual({ok,[10,11,12]}, ts_dynvars:lookup(one, DynVars)),
+ ?assertEqual({ok,11}, ts_dynvars:lookup({one,2}, DynVars)).
+
dynvars_new_more_test() ->
Keys = [one,two,three],
Values = [1,2,"three",[]],
@@ -114,17 +114,17 @@ parse_dyn_var_xpath4_test() ->
parse_dyn_var_many_re_test() ->
myset_env(),
- {Data, Res}= setdata(?MANY),
+ {Data, Res}= setdata(?MANY,binary),
RegexpFun = fun(A) -> {re,list_to_atom(A), ?DEF_RE_DYNVAR_BEGIN++ A ++?DEF_RE_DYNVAR_END} end,%'
B=lists:map(fun(A)->"random"++integer_to_list(A) end, lists:seq(1,?MANY)),
C=lists:map(RegexpFun, B),
{Time, Out}=timer:tc( ts_search,parse_dynvar,[C,list_to_binary(Data)]),
erlang:display([?MANY," re:", Time]),
- ?assertMatch(Res, Out).
+ ?assertEqual(Res, Out).
parse_dyn_var_many_xpath_test() ->
myset_env(),
- {Data, Res}= setdata(?MANY,binary),
+ {Data, Res}= setdata(?MANY,binarylist),
B=lists:map(fun(A)->{xpath, list_to_atom("random"++integer_to_list(A)),
"//input[@type='hidden'][@name='random"++integer_to_list(A)++"']/@value"} end, lists:seq(1,?MANY)),
{Time, Out}=timer:tc( ts_search,parse_dynvar,[B,list_to_binary(Data)]),
@@ -133,7 +133,7 @@ parse_dyn_var_many_xpath_test() ->
parse_dyn_var_many_xpath_explicit_test() ->
myset_env(),
- {Data, Res}= setdata(?MANY,binary),
+ {Data, Res}= setdata(?MANY,binarylist),
B=lists:map(fun(A)->{xpath, list_to_atom("random"++integer_to_list(A)),
"/html/body/form/input[@type='hidden'][@name='random"++integer_to_list(A)++"']/@value"} end, lists:seq(1,?MANY)),
{Time, Out}=timer:tc( ts_search,parse_dynvar,[B,list_to_binary(Data)]),
@@ -143,7 +143,7 @@ parse_dyn_var_many_xpath_explicit_test() ->
parse_dyn_var_many_big_re_test() ->
myset_env(),
- {Data, Res}= setdata_big(?MANY),
+ {Data, Res}= setdata_big(?MANY,binary),
RegexpFun = fun(A) -> {re,list_to_atom(A), ?DEF_RE_DYNVAR_BEGIN++ A ++?DEF_RE_DYNVAR_END} end,%'
B=lists:map(fun(A)->"random"++integer_to_list(A) end, lists:seq(1,?MANY)),
C=lists:map(RegexpFun, B),
@@ -153,7 +153,7 @@ parse_dyn_var_many_big_re_test() ->
parse_dyn_var_many_big_xpath_test() ->
myset_env(),
- {Data, Res}= setdata_big(?MANY,binary),
+ {Data, Res}= setdata_big(?MANY,binarylist),
B=lists:map(fun(A)->{xpath, list_to_atom("random"++integer_to_list(A)),
"//input[@type='hidden'][@name='random"++integer_to_list(A)++"']/@value"} end, lists:seq(1,?MANY)),
{Time, Out}=timer:tc( ts_search,parse_dynvar,[B,list_to_binary(Data)]),
@@ -162,7 +162,7 @@ parse_dyn_var_many_big_xpath_test() ->
parse_dyn_var_many_big_xpath_explicit_test() ->
myset_env(),
- {Data, Res}= setdata_big(?MANY,binary),
+ {Data, Res}= setdata_big(?MANY,binarylist),
B=lists:map(fun(A)->{xpath, list_to_atom("random"++integer_to_list(A)),
"/html/body/form/input[@type='hidden'][@name='random"++integer_to_list(A)++"']/@value"} end, lists:seq(1,?MANY)),
{Time, Out}=timer:tc( ts_search,parse_dynvar,[B,list_to_binary(Data)]),
@@ -196,8 +196,10 @@ setdata_big(N, Type) ->
{HTML,lists:reverse(lists:map(fun(A)->{list_to_atom("random"++integer_to_list(A)) , format_result("value"++integer_to_list(A),Type)} end, lists:seq(1,N)))}.
-format_result(Data,binary) ->
+format_result(Data,binarylist) ->
[list_to_binary(Data)];
+format_result(Data,binary) ->
+ list_to_binary(Data);
format_result(Data,_) ->
Data.
@@ -251,14 +253,14 @@ parse_subst_badregexp_sid_test() ->
Data="HTTP/1.1 200 OK\r\nServer: nginx/0.7.65\r\nDate: Fri, 05 Feb 2010 08:13:29 GMT\r\nContent-Type: text/xml; charset=utf-8\r\nConnection: keep-alive\r\nContent-Length: 373\r\n\r\n<body polling=\"10\" ver=\"1.6\" secure=\"true\" wait=\"20\" requests=\"2\" hold=\"1\" sid=\"5bfd2b59-3144-4e62-993b-d05d2ae3bee9\" xmpp:version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" authid=\"b65b29eb-99c0-4afd-8f97-d6d20f4ddba2\" maxpause=\"10\" from=\"tigase-test\" inactivity=\"10\" ack=\"2995502128855\" xmlns:xmpp=\"urn:xmpp:xbosh\" xmlns=\"http://jabber.org/protocol/httpbind\"/>",
Regexp = "sid=\".*?\"",
[{Name,Value}] = ts_search:parse_dynvar([{re, sid, Regexp }],list_to_binary(Data)),
- ?assertEqual({sid,""},{Name,Value}).
+ ?assertEqual({sid,<<"">>},{Name,Value}).
parse_subst_regexp_sid_test() ->
myset_env(),
Data="HTTP/1.1 200 OK\r\nServer: nginx/0.7.65\r\nDate: Fri, 05 Feb 2010 08:13:29 GMT\r\nContent-Type: text/xml; charset=utf-8\r\nConnection: keep-alive\r\nContent-Length: 373\r\n\r\n<body polling=\"10\" ver=\"1.6\" secure=\"true\" wait=\"20\" requests=\"2\" hold=\"1\" sid=\"5bfd2b59-3144-4e62-993b-d05d2ae3bee9\" xmpp:version=\"1.0\" xmlns:stream=\"http://etherx.jabber.org/streams\" authid=\"b65b29eb-99c0-4afd-8f97-d6d20f4ddba2\" maxpause=\"10\" from=\"tigase-test\" inactivity=\"10\" ack=\"2995502128855\" xmlns:xmpp=\"urn:xmpp:xbosh\" xmlns=\"http://jabber.org/protocol/httpbind\"/>",
Regexp = "sid=\"([^\"]*)\"",
[{Name,Value}] = ts_search:parse_dynvar([{re, sid, Regexp }],list_to_binary(Data)),
- ?assertEqual({sid,"5bfd2b59-3144-4e62-993b-d05d2ae3bee9"},{Name,Value}).
+ ?assertEqual({sid,<<"5bfd2b59-3144-4e62-993b-d05d2ae3bee9">>},{Name,Value}).
dynvars_urandom_test() ->
@@ -342,7 +344,7 @@ badarg_re_test() ->
Data = << "Below this line, is 1000 repeated lines">>,
Regexp = "is (\\d+) repeated lines",
{ok,Regexp2}=re:compile(Regexp),
- ?assertEqual([{lines, "1000"}], ts_search:parse_dynvar([{re, 'lines', Regexp2 }],Data)).
+ ?assertEqual([{lines, <<"1000">>}], ts_search:parse_dynvar([{re, 'lines', Regexp2 }],Data)).
myset_env()->
myset_env(0).
View
@@ -617,7 +617,11 @@ ctrl_struct_impl({foreach_end,ForEachName,VarName,Filter,Target}, DynData=#dynda
-rel('eq',A,B) -> A == B;
+rel(R,A,B) when is_integer(B) ->
+%% jsonpath can output numbers instead of binaries
+ rel(R,A,list_to_binary(integer_to_list(B)));
+rel('eq',A,B) ->
+ A == B;
rel('neq',A,B) -> A /= B.
need_jump('while',F) -> F;
View
@@ -56,14 +56,20 @@ new(VarNames, Values) when is_list(VarNames),is_list(Values)->
end.
%% @spec lookup(Key::atom(), Dynvar::dynvars()) -> {ok,Value::term()} | false
-lookup(Key, []) when is_atom(Key)->
+lookup(Key, []) ->
false;
lookup(Key, DynVars) when ?IS_DYNVARS(DynVars), is_atom(Key)->
case lists:keysearch(Key,1,DynVars) of
{value,{Key,Value}} -> {ok,Value};
false -> false
+ end;
+lookup({Key, Index}, DynVars) when ?IS_DYNVARS(DynVars), is_atom(Key), is_integer(Index)->
+ case lists:keysearch(Key,1,DynVars) of
+ {value,{Key,Value}} -> {ok,lists:nth(Index,Value)};
+ false -> false
end.
+
%% @doc same as lookup/2, only that if the key isn't present, the default
%% value is returned instead of returning false.
lookup(Key, DynVars, Default) when ?IS_DYNVARS(DynVars), is_atom(Key)->
View
@@ -265,7 +265,7 @@ parse_dynvar([],_Binary , _String,_Tree, DynVars) -> DynVars;
parse_dynvar(D=[{re,_, _}| _],Binary,undefined,Tree,DynVars) ->
parse_dynvar(D,Binary,Binary,Tree,DynVars);
parse_dynvar([{re,VarName, RegExp}| DynVarsSpecs],Binary,Data,Tree,DynVars) ->
- case re:run(Data, RegExp,[{capture,[1],list}]) of
+ case re:run(Data, RegExp,[{capture,[1],binary}]) of
{match,[Value]} ->
?LOGF("DynVar (RE): Match (~p=~p) ~n",[VarName, Value], ?INFO),
parse_dynvar(DynVarsSpecs, Binary,Data,Tree,
@@ -403,7 +403,7 @@ parse(Element = #xmlElement{name=transaction, attributes=Attrs},
%%%% Parsing the 'if' element
parse(_Element = #xmlElement{name='if', attributes=Attrs,content=Content},
Conf = #config{session_tab = Tab, sessions=[CurS|_], curid=Id}) ->
- VarName = getAttr(atom,Attrs,var),
+ VarName=get_dynvar_name(getAttr(string,Attrs,var)),
{Rel,Value} = case getAttr(string,Attrs,eq,none) of
none -> {neq,getAttr(string,Attrs,neq)};
X -> {eq,X}
@@ -413,7 +413,7 @@ parse(_Element = #xmlElement{name='if', attributes=Attrs,content=Content},
NewConf = lists:foldl(fun parse/2, Conf#config{curid=Id+1}, Content),
NewId = NewConf#config.curid,
?LOGF("endif in session ~p as id ~p",[CurS#session.id,NewId+1],?INFO),
- InitialAction = {ctrl_struct, {if_start, Rel, VarName, Value , NewId+1}},
+ InitialAction = {ctrl_struct, {if_start, Rel, VarName, list_to_binary(Value) , NewId+1}},
%%NewId+1 -> id of the first action after the if
ets:insert(Tab,{{CurS#session.id,Id+1},InitialAction}),
NewConf;
@@ -477,7 +477,8 @@ parse(_Element = #xmlElement{name=foreach, attributes=Attrs,content=Content},
parse(_Element = #xmlElement{name=repeat,attributes=Attrs,content=Content},
Conf = #config{session_tab = Tab, sessions=[CurS|_], curid=Id}) ->
MaxRepeat = getAttr(integer,Attrs,max_repeat,20),
- RepeatName = getAttr(atom,Attrs,name),
+ RepeatName = get_dynvar_name(getAttr(string,Attrs,name)),
+
[LastElement|_] = lists:reverse([E || E=#xmlElement{} <- Content]),
case LastElement of
#xmlElement{name=While,attributes=WhileAttrs}
@@ -490,7 +491,7 @@ parse(_Element = #xmlElement{name=repeat,attributes=Attrs,content=Content},
Var = getAttr(atom,WhileAttrs,var),
NewConf = lists:foldl(fun parse/2, Conf#config{curid=Id}, Content),
NewId = NewConf#config.curid,
- EndAction = {ctrl_struct,{repeat,RepeatName, While,Rel,Var,Value,Id+1, MaxRepeat}},
+ EndAction = {ctrl_struct,{repeat,RepeatName, While,Rel,Var,list_to_binary(Value),Id+1, MaxRepeat}},
%Id+1 -> id of the first action inside the loop
?LOGF("Add repeat action in session ~p as id ~p, Jump to: ~p",
[CurS#session.id,NewId+1,Id+1],?INFO),
@@ -999,3 +1000,10 @@ set_net_type("udp") -> gen_udp;
set_net_type("udp6") -> gen_udp6;
set_net_type("ssl") -> ssl;
set_net_type("ssl6") -> ssl6.
+
+get_dynvar_name(VarNameStr) ->
+ %% check if the var name is for an array (myvar[N])
+ case re:run(VarNameStr,"(.+)\[(\d+)\]",[{capture,all_but_first,list},dotall]) of
+ {match,[Name,Index]} -> {list_to_atom(Name),Index};
+ _ -> list_to_atom(VarNameStr)
+ end.

0 comments on commit d102df0

Please sign in to comment.