From 51fa0e6fc9413201ab7024ff89563bf8e9b4447b Mon Sep 17 00:00:00 2001 From: rubiii Date: Sat, 27 Nov 2010 18:27:57 +0100 Subject: [PATCH] made the SOAP envelope namespace configurable --- CHANGELOG.md | 5 +++++ lib/savon/soap/xml.rb | 28 +++++++++++++++++++++++----- spec/savon/soap/xml_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 131c2080..e5af3fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/lib/savon/soap/xml.rb b/lib/savon/soap/xml.rb index 918eee78..81247a7b 100644 --- a/lib/savon/soap/xml.rb +++ b/lib/savon/soap/xml.rb @@ -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 xmlns:env 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. @@ -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 @@ -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 diff --git a/spec/savon/soap/xml_spec.rb b/spec/savon/soap/xml_spec.rb index 2ad59dcd..0131856b 100644 --- a/spec/savon/soap/xml_spec.rb +++ b/spec/savon/soap/xml_spec.rb @@ -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/" } @@ -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("/) @@ -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("