Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom converter and for using global env_namespace #172

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions lib/savon/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,22 @@ def preconfigure(options)
# Expects an +input+ and sets the +SOAPAction+ HTTP headers.
def set_soap_action(input)
soap_action = wsdl.soap_action input.to_sym
soap_action ||= input.kind_of?(String) ? input : input.to_s.lower_camelcase
if input.kind_of?(String)
soap_action ||= input.to_sym
else
soap_action ||= Savon.converter.nil? ? input.to_s.lower_camelcase.to_sym : Savon.converter.call(input).to_sym
end
http.headers["SOAPAction"] = %{"#{soap_action}"}
end

# Expects a +namespace+, +input+ and +attributes+ and sets the SOAP input.
def set_soap_input(namespace, input, attributes)
new_input = wsdl.soap_input input.to_sym
new_input ||= input.kind_of?(String) ? input.to_sym : input.to_s.lower_camelcase.to_sym
if input.kind_of?(String)
new_input ||= input.to_sym
else
new_input ||= Savon.converter.nil? ? input.to_s.lower_camelcase.to_sym : Savon.converter.call(input).to_sym
end
soap.input = [namespace, new_input, attributes].compact
end

Expand Down Expand Up @@ -155,3 +163,4 @@ def method_missing(method, *args, &block)

end
end

19 changes: 19 additions & 0 deletions lib/savon/global.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ def strip_namespaces?
# Sets whether to strip namespaces in a SOAP response Hash.
attr_writer :strip_namespaces


# Returns the current action_converter
attr_reader :converter

# Sets both action_converter and input_converter, converter should be a proc
def converter=(converter)
@converter = converter
end

# Returns the global env_namespace
attr_reader :env_namespace

# Sets the global env_namespace
def env_namespace=(namespace)
@env_namespace = namespace
end

# Reset to default configuration.
def reset_config!
self.log = true
Expand All @@ -69,7 +86,9 @@ def reset_config!
self.raise_errors = true
self.soap_version = SOAP::DefaultVersion
self.strip_namespaces = true
self.converter = nil
end

end
end

5 changes: 3 additions & 2 deletions lib/savon/soap/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ def header
# Sets the SOAP envelope namespace.
attr_writer :env_namespace

# Returns the SOAP envelope namespace. Defaults to :env.
# Returns the SOAP envelope namespace. Uses the global namespace if set Defaults to :env.
def env_namespace
@env_namespace ||= :env
@env_namespace ||= Savon.env_namespace.nil? ? :env : Savon.env_namespace
end

# Sets the +namespaces+ Hash.
Expand Down Expand Up @@ -181,3 +181,4 @@ def body_to_xml
end
end
end

9 changes: 8 additions & 1 deletion spec/savon/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@
client.request(:get_user) { soap.input.should == [:getUser, {}] }
end

it "should set the input tag to result in <GetUser> when configured" do
# Create a stub that returns the string in camel case
Savon.stubs(:converter).returns(proc { |input| input.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } })
client.request(:get_user) { soap.input.should == [:GetUser, {}] }
end

it "should set the target namespace with the default identifier" do
namespace = 'xmlns:wsdl="http://v1_0.ws.auth.order.example.com/"'
HTTPI::Request.any_instance.expects(:body=).with { |value| value.include? namespace }
Expand Down Expand Up @@ -236,7 +242,7 @@
end

context "with a local WSDL document" do
let(:client) { Savon::Client.new { wsdl.document = "spec/fixtures/wsdl/authentication.xml" } }
let(:client) { Savon::Client.new { wsdl.document = "spec/fixtures/wsdl/authentication.xml" } }

before { HTTPI.expects(:get).never }

Expand Down Expand Up @@ -344,3 +350,4 @@ def new_response(options = {})
end

end

8 changes: 7 additions & 1 deletion spec/savon/soap/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
xml.env_namespace = :soapenv
xml.env_namespace.should == :soapenv
end

it "should use the global env_namespace if set as the SOAP envelope namespace" do
Savon.stubs(:env_namespace).returns(:soapenv)
xml.env_namespace.should == :soapenv
end
end

describe "#namespaces" do
Expand Down Expand Up @@ -223,7 +228,7 @@
:token => "secret",
:attributes! => { :token => { :xmlns => "http://example.com" } }
}

xml.to_xml.should include('<env:Header><token xmlns="http://example.com">secret</token></env:Header>')
end
end
Expand Down Expand Up @@ -333,3 +338,4 @@ def reset_soap_version
end

end