Permalink
Browse files

Fixed the problem reported in issue6. Added a test for the issue as well

  • Loading branch information...
jimmiw committed Oct 22, 2009
1 parent a0477b3 commit bdb035366106ff8f378b853275a9393d3925a1bc
Showing with 66 additions and 3 deletions.
  1. +27 −3 lib/handsoap/service.rb
  2. +39 −0 tests/fault_test.rb
View
@@ -25,30 +25,54 @@ def self.xml_query_driver=(driver)
@xml_query_driver = Handsoap::XmlQueryFront.load_driver!(driver)
end
+ # Wraps SOAP errors in a standard class.
class Fault < StandardError
attr_reader :code, :reason, :details
+
def initialize(code, reason, details)
@code = code
@reason = reason
@details = details
end
+
def to_s
"Handsoap::Fault { :code => '#{@code}', :reason => '#{@reason}' }"
end
+
def self.from_xml(node, options = { :namespace => nil })
if not options[:namespace]
raise "Missing option :namespace"
end
+
ns = { 'env' => options[:namespace] }
- fault_code = node.xpath('./env:Code/env:Value', ns).to_s
+
+ # tries to find SOAP1.2 fault code
+ fault_code = node.xpath("./env:Code/env:Value", ns).to_s
+
+ # if no SOAP1.2 fault code was found, try the SOAP1.1 way
unless fault_code
fault_code = node.xpath('./faultcode', ns).to_s
+
+ # if fault_code is blank, add the namespace and try again
+ unless fault_code
+ fault_code = node.xpath("//env:faultcode", ns).to_s
+ end
end
- reason = node.xpath('./env:Reason/env:Text[1]', ns).to_s
+
+ # tries to find SOAP1.2 reason
+ reason = node.xpath("./env:Reason/env:Text[1]", ns).to_s
+
+ # if no SOAP1.2 faultstring was found, try the SOAP1.1 way
unless reason
reason = node.xpath('./faultstring', ns).to_s
+
+ # if reason is blank, add the namespace and try again
+ unless reason
+ reason = node.xpath("//env:faultstring", ns).to_s
+ end
end
- details = node.xpath('./detail/*', ns)
+
+ details = node.xpath('./detail/*', ns)
self.new(fault_code, reason, details)
end
end
View
@@ -21,6 +21,7 @@ def get_fault_11
</soap:Envelope>'
Handsoap::XmlQueryFront.parse_string(xml_doc, Handsoap.xml_query_driver)
end
+
def get_fault_12
xml_doc = '<?xml version="1.0" ?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope"
@@ -49,20 +50,58 @@ def get_fault_12
</env:Envelope>'
Handsoap::XmlQueryFront.parse_string(xml_doc, Handsoap.xml_query_driver)
end
+
+ # Added to test issue 6, found here: http://github.com/unwire/handsoap/issues#issue/6
+ # this is for SOAP 1.1 btw
+ def get_fault_with_namespace
+ xml_doc = '<?xml version="1.0" encoding="UTF-8"?>
+<a:Envelope xmlns:a="http://schemas.xmlsoap.org/soap/envelope/">
+ <a:Body>
+ <a:Fault>
+ <a:faultcode>a:Client</a:faultcode>
+ <a:faultstring>Failed to parse the request</a:faultstring>
+ <a:detail>Invalid parameter</a:detail>
+ </a:Fault>
+ </a:Body>
+</a:Envelope>'
+
+ Handsoap::XmlQueryFront.parse_string(xml_doc, Handsoap.xml_query_driver)
+ end
+
+ # Tests that SOAP1.1 faults can be parsed
def test_can_parse_soap_fault_11
envelope_namespace = "http://schemas.xmlsoap.org/soap/envelope/"
+
node = get_fault_11.xpath('/env:Envelope/env:Body/descendant-or-self::env:Fault', { 'env' => envelope_namespace })
fault = Handsoap::Fault.from_xml(node, :namespace => envelope_namespace)
+
assert_kind_of Handsoap::Fault, fault
assert_equal 'soap:Server', fault.code
assert_equal 'Error while blackList account: the application does not exist', fault.reason
end
+
+ # Tests that SOAP1.2 faults can be parsed
def test_can_parse_soap_fault_12
envelope_namespace = "http://www.w3.org/2003/05/soap-envelope"
+
node = get_fault_12.xpath('/env:Envelope/env:Body/descendant-or-self::env:Fault', { 'env' => envelope_namespace })
fault = Handsoap::Fault.from_xml(node, :namespace => envelope_namespace)
+
assert_kind_of Handsoap::Fault, fault
assert_equal 'env:Sender', fault.code
assert_equal 'Processing error', fault.reason
end
+
+ # Added to test issue 6, found here: http://github.com/unwire/handsoap/issues#issue/6
+ # This is for SOAP 1.1 btw
+ def test_can_parse_soap_fault_with_namespace
+ envelope_namespace = "http://schemas.xmlsoap.org/soap/envelope/"
+
+ node = get_fault_with_namespace.xpath('/env:Envelope/env:Body/descendant-or-self::env:Fault', { 'env' => envelope_namespace })
+ fault = Handsoap::Fault.from_xml(node, :namespace => envelope_namespace)
+
+ assert_kind_of Handsoap::Fault, fault
+ assert_equal 'a:Client', fault.code
+ assert_equal 'Failed to parse the request', fault.reason
+ end
end

0 comments on commit bdb0353

Please sign in to comment.