Skip to content

Commit

Permalink
Merge pull request #8 from willemdj/master
Browse files Browse the repository at this point in the history
Fix bug in unicode handling. Thank you Willem.
  • Loading branch information
cmullaparthi authored Jun 13, 2016
2 parents 16e3279 + a4b4cb5 commit 9e1ae52
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/soap_client_util.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ call_body(Body, Encoded_headers, Soap_action,
["<s:Envelope xmlns:s=\"", Namespace, "\">",
Encoded_headers,
"<s:Body>",
Encoded_body,
unicode:characters_to_binary(Encoded_body),
"</s:Body></s:Envelope>"],
call_message(Http_body, Soap_action, Interface, Attachments)
catch
Expand Down Expand Up @@ -212,7 +212,8 @@ encode_headers([Header|T], Model, Acc)
encode_headers([Header|T], Model, Acc) when is_tuple(Header) ->
case erlsom:write(Header, Model) of
{ok, Encoded_header} ->
encode_headers(T, Model, [Encoded_header | Acc]);
encode_headers(T, Model,
[unicode:characters_to_binary(Encoded_header) | Acc]);
Error ->
{error, {client, {encoding_headers, {Error}}}}
end.
Expand Down
21 changes: 12 additions & 9 deletions src/soap_fault.erl
Original file line number Diff line number Diff line change
Expand Up @@ -242,31 +242,36 @@ fault_code(#faultcode{code = Code,
subcode_text(Subcode),
<<"</SOAP-ENV:Code>">>].

fault_code_text(Code, '1.1')
fault_code_text(Code, Version) when is_atom(Code) ->
fault_code_text_2(Code, Version);
fault_code_text(Code, Version) when not is_atom(Code) ->
fault_code_text_2(unicode:characters_to_list(Code), Version).

fault_code_text_2(Code, '1.1')
when Code == server;
Code == "Server" ->
<<"SOAP-ENV:Server">>;
fault_code_text(Code, '1.2')
fault_code_text_2(Code, '1.2')
when Code == server;
Code == "Receiver" ->
<<"SOAP-ENV:Receiver">>;
fault_code_text(Code, '1.1')
fault_code_text_2(Code, '1.1')
when Code == client;
Code == "Client" ->
<<"SOAP-ENV:Client">>;
fault_code_text(Code, '1.2')
fault_code_text_2(Code, '1.2')
when Code == client;
Code == "Sender" ->
<<"SOAP-ENV:Sender">>;
fault_code_text(Code, _) %% version 1.2
fault_code_text_2(Code, _) %% version 1.2
when Code == version_mismatch;
Code == "VersionMismatch" ->
<<"SOAP-ENV:VersionMismatch">>;
fault_code_text(Code, _) %% version 1.2
fault_code_text_2(Code, _) %% version 1.2
when Code == must_understand;
Code == "MustUnderstand" ->
<<"SOAP-ENV:MustUnderstand">>;
fault_code_text(Code, _) %% version 1.2
fault_code_text_2(Code, _) %% version 1.2
when Code == data_encoding_unknown;
Code == "DataEncodingUnknown" ->
<<"SOAP-ENV:DataEncodingUnknown">>.
Expand Down Expand Up @@ -469,13 +474,11 @@ parse_fault_1_2({endElement, ?SOAP12_NS, "Reason", _},
parse_fault_1_2({startElement, ?SOAP12_NS, "Text", _, Attributes},
_Namespaces,
#pf_state{state = reasons} = S) ->
io:format("attributes: ~p~n", [Attributes]),
Language =
case lists:keyfind("lang", #attribute.localName, Attributes) of
#attribute{prefix = "xml", value = L} ->
L;
_Other ->
io:format("found: ~p~n", [_Other]),
undefined
end,
S#pf_state{language = Language, state = text};
Expand Down
2 changes: 1 addition & 1 deletion src/soap_req.erl
Original file line number Diff line number Diff line change
Expand Up @@ -321,4 +321,4 @@ add_if_not_present(List, {Key, _} = Tuple) ->

%% TODO: look at this _and_ erlsom_lib:xmlString - this is quite inefficient.
xml_string(IO_list) ->
erlsom_lib:xmlString(iolist_to_binary(IO_list)).
erlsom_lib:xmlString(unicode:characters_to_binary(IO_list)).
20 changes: 20 additions & 0 deletions test/soap_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ groups() ->
]}
,{client, [],
[client_no_error
,client_unicode
,client_unicode_binary
,client_fault_exception
,client_http_error
,client_wrong_request_header
Expand All @@ -150,6 +152,7 @@ groups() ->
,raw_client
,raw_client_error
,{inets_client, [], [inets_client_no_error
,inets_client_unicode
,inets_client_timeout]}
,{faults_1_1, [], [client_client_fault
,client_server_fault
Expand Down Expand Up @@ -452,10 +455,27 @@ client_no_error(_Config) ->
test_service_client:do_test(#request_body{expected_response="ok"}, [], []),
"text/xml" = proplists:get_value("Content-Type", Http_headers).

client_unicode(_Config) ->
{ok,200, _, [],#response_body{response = "بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ"}, [], _} =
test_service_client:do_test(#request_body{expected_response=
"بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ"}, [], []).

client_unicode_binary(_Config) ->
{ok,200, _, [],#response_body{response = "بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ"}, [], _} =
test_service_client:do_test(#request_body{
expected_response=
unicode:characters_to_binary("بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ")},
[], []).

inets_client_no_error(_Config) ->
{ok,200, _, [],#response_body{}, [], _} =
test_inets_client:do_test(#request_body{expected_response="sleep:0"}, [], []).

inets_client_unicode(_Config) ->
{ok,200, _, [],#response_body{response = "بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ"}, [], _} =
test_inets_client:do_test(#request_body{expected_response=
"بِسْمِ ٱلرَّحْمـَبنِ ٱلرَّحِيمِ"}, [], []).

inets_client_timeout(_Config) ->
{error,{client,{http_request,timeout}}, _} =
test_inets_client:do_test(#request_body{expected_response="sleep:2"}, [],
Expand Down
11 changes: 10 additions & 1 deletion test/test_service_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ do_test(#request_body{expected_response="raw"}, Soap_req, State) ->
{_Start, 3} = binary:match(Raw_body, <<"raw">>),
Raw_resp =
<<"<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\"><env:Body><x:response_body xmlns:x=\"test\"><response>raw</response></x:response_body></env:Body></env:Envelope>">>,
{raw, 200, Raw_resp, Soap_req, State}.
{raw, 200, Raw_resp, Soap_req, State};
do_test(#request_body{expected_response=Response}, Soap_req, State) when Response=="not_ok" ->
%% trigger an exception
case Response of
"ok" ->
{ok, #response_body{response="ok"}, Soap_req, State}
end;
do_test(#request_body{expected_response=Something_else}, Soap_req, State) ->
%% simply echo the message (used to test encodings)
{ok, #response_body{response=Something_else}, Soap_req, State}.

%% replace the default exception handler to include a stacktrace.
-spec exception(Class::atom(), Reason::any(), Stacktrace::any(),
Expand Down

0 comments on commit 9e1ae52

Please sign in to comment.