Skip to content

Commit

Permalink
Add send_iq_and_wait_for_result/2,3 helpers (#175)
Browse files Browse the repository at this point in the history
These functions send an IQ stanza and wait for response. They assert
that the response is the IQ of type "result", and that its ID matches the
ID of the sent request.
  • Loading branch information
arkgil authored and arcusfelis committed Apr 24, 2018
1 parent 3da55e4 commit 115cffb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/escalus.erl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
wait_for_stanza/2,
wait_for_stanzas/2,
wait_for_stanzas/3,
send_iq_and_wait_for_result/2,
send_iq_and_wait_for_result/3,
peek_stanzas/1]).

-export_type([client/0,
Expand Down Expand Up @@ -165,6 +167,12 @@ wait_for_stanzas(Client, Count, Timeout) ->
peek_stanzas(Client) ->
escalus_client:peek_stanzas(Client).

send_iq_and_wait_for_result(Client, Iq) ->
escalus_client:send_iq_and_wait_for_result(Client, Iq).

send_iq_and_wait_for_result(Client, Iq, Timeout) ->
escalus_client:send_iq_and_wait_for_result(Client, Iq, Timeout).

%% Other functions

override(Config, OverrideName, NewValue) ->
Expand Down
33 changes: 33 additions & 0 deletions src/escalus_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
peek_stanzas/1, has_stanzas/1,
wait_for_stanzas/2, wait_for_stanzas/3,
wait_for_stanza/1, wait_for_stanza/2,
send_iq_and_wait_for_result/2, send_iq_and_wait_for_result/3,
is_client/1,
full_jid/1,
short_jid/1,
Expand Down Expand Up @@ -150,6 +151,38 @@ send_and_wait(Client, Packet) ->
ok = send(Client, Packet),
wait_for_stanza(Client).

-spec send_iq_and_wait_for_result(client(), exml:element()) -> exml:element() | no_return().
send_iq_and_wait_for_result(Client, Iq) ->
send_iq_and_wait_for_result(Client, Iq, ?WAIT_FOR_STANZA_TIMEOUT).

-spec send_iq_and_wait_for_result(client(), exml:element(), non_neg_integer()) ->
exml:element() | no_return().
send_iq_and_wait_for_result(Client, #xmlel{name = <<"iq">>} = Req, Timeout) ->
ok = send(Client, Req),
Resp = #xmlel{name = RespName} = wait_for_stanza(Client, Timeout),
RespType = exml_query:attr(Resp, <<"type">>, undefined),
RespId = exml_query:attr(Resp, <<"id">>),
ReqId = exml_query:attr(Req, <<"id">>),
case {RespName, RespType, RespId == ReqId} of
{<<"iq">>, <<"result">>, true} ->
Resp;
{<<"iq">>, <<"result">>, false} ->
raise_invalid_iq_resp_error(received_invalid_iq_result_id, ReqId, RespId, Req, Resp);
{<<"iq">>, _, _} ->
raise_invalid_iq_resp_error(received_invalid_iq_stanza_type, <<"result">>, RespType,
Req, Resp);
{_, _, _} ->
raise_invalid_iq_resp_error(received_invalid_stanza, <<"iq">>, RespName, Req, Resp)
end.

-spec raise_invalid_iq_resp_error(atom(), term(), term(), exml:element(), exml:element()) ->
no_return().
raise_invalid_iq_resp_error(Reason, Expected, Received, Req, Resp) ->
error({Reason, [{expected, Expected},
{received, Received},
{request, Req},
{response, Resp}]}).

-spec is_client(term()) -> boolean().
is_client(#client{}) ->
true;
Expand Down

0 comments on commit 115cffb

Please sign in to comment.