Skip to content

Commit

Permalink
made the SOAP envelope namespace configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiii committed Nov 27, 2010
1 parent 06e536e commit 51fa0e6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.8.0.beta.5 (UPCOMING)

* Added Savon::SOAP::XML#env_namespace to configure the SOAP envelope namespace. It defaults to :env
but can also be set to an empty String for SOAP envelope tags without a namespace.

## 0.8.0.beta.4 (2010-11-20)

* Fix for issue #107 (Soap Fault regex not working for API I connect).
Expand Down
28 changes: 23 additions & 5 deletions lib/savon/soap/xml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,23 @@ def header
@header ||= {}
end

# Sets the SOAP envelope namespace.
attr_writer :env_namespace

# Returns the SOAP envelope namespace. Defaults to :env.
def env_namespace
@env_namespace ||= :env
end

# Sets the +namespaces+ Hash.
attr_writer :namespaces

# Returns the +namespaces+. Defaults to a Hash containing the <tt>xmlns:env</tt> namespace.
# Returns the +namespaces+. Defaults to a Hash containing the SOAP envelope namespace.
def namespaces
@namespaces ||= { "xmlns:env" => SOAP::Namespace[version] }
@namespaces ||= begin
key = env_namespace.blank? ? "xmlns" : "xmlns:#{env_namespace}"
{ key => SOAP::Namespace[version] }
end
end

# Sets the default namespace identifier.
Expand Down Expand Up @@ -90,9 +101,9 @@ def xml

# Returns the XML for a SOAP request.
def to_xml
@xml ||= builder.env :Envelope, complete_namespaces do |xml|
xml.env(:Header) { xml << header_for_xml } unless header_for_xml.empty?
xml.env(:Body) { xml.tag!(*input) { xml << body_to_xml } }
@xml ||= tag(builder, :Envelope, complete_namespaces) do |xml|
tag(xml, :Header) { xml << header_for_xml } unless header_for_xml.empty?
tag(xml, :Body) { xml.tag!(*input) { xml << body_to_xml } }
end
end

Expand All @@ -105,6 +116,13 @@ def builder
builder
end

# Expects a builder +xml+ instance, a tag +name+ and accepts optional +namespaces+
# and a block to create an XML tag.
def tag(xml, name, namespaces = {}, &block)
return xml.tag! name, namespaces, &block if env_namespace.blank?
xml.tag! env_namespace, name, namespaces, &block
end

# Returns the complete Hash of namespaces.
def complete_namespaces
defaults = SchemaTypes.dup
Expand Down
22 changes: 22 additions & 0 deletions spec/savon/soap/xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,17 @@
end
end

describe "#env_namespace" do
it "should default to :env" do
xml.env_namespace.should == :env
end

it "should set the SOAP envelope namespace" do
xml.env_namespace = :soapenv
xml.env_namespace.should == :soapenv
end
end

describe "#namespaces" do
it "should default to a Hash containing the namespace for SOAP 1.1" do
xml.namespaces.should == { "xmlns:env" => "http://schemas.xmlsoap.org/soap/envelope/" }
Expand Down Expand Up @@ -143,6 +154,10 @@
xml.to_xml.should match(/^<\?xml version="1.0" encoding="UTF-8"\?>/)
end

it "should use default SOAP envelope namespace" do
xml.to_xml.should include("<env:Envelope", "<env:Body")
end

it "should add the xsd namespace" do
uri = "http://www.w3.org/2001/XMLSchema"
xml.to_xml.should match(/<env:Envelope (.*)xmlns:xsd="#{uri}"(.*)>/)
Expand Down Expand Up @@ -204,6 +219,13 @@
end
end

context "with the SOAP envelope namespace set to an empty String" do
it "should not add a namespace to SOAP envelope tags" do
xml.env_namespace = ""
xml.to_xml.should include("<Envelope", "<Body")
end
end

context "using the #namespace and #namespace_identifier" do
it "should contain the specified namespace" do
xml.namespace_identifier = :wsdl
Expand Down

0 comments on commit 51fa0e6

Please sign in to comment.