Permalink
Browse files

use the builder instead of a block's return value

currently, when passing a block to both soap#body and soap#xml to use builder to
create the xml, savon uses the block's return value instead of the builder's state.

this fixes #322.
  • Loading branch information...
1 parent c36be54 commit 0179e5a93e7ac6802a2732dc96906f69a6fd4717 @rubiii rubiii committed Sep 18, 2012
Showing with 56 additions and 18 deletions.
  1. +5 −0 CHANGELOG.md
  2. +11 −2 lib/savon/soap/xml.rb
  3. +40 −16 spec/savon/soap/xml_spec.rb
View
@@ -1,3 +1,8 @@
+## UPCOMING
+
+* Fix: [#322](https://github.com/savonrb/savon/issues/322) use the builder's state instead of the
+ block's return value to set the soap body/xml values.
+
## 1.2.0 (2012-09-15)
* Fix: [#312](https://github.com/savonrb/savon/pull/312) recursively determines the proper namespaces
View
@@ -136,7 +136,12 @@ def encoding
# Accepts a +block+ and yields a <tt>Builder::XmlMarkup</tt> object to let you create
# custom body XML.
def body
- @body = yield builder(nil) if block_given?
+ if block_given?
+ xml = builder(nil)
+ yield xml
+ @body = xml.target!
+ end
+
@body
end
@@ -147,7 +152,11 @@ def body
# Accepts a +block+ and yields a <tt>Builder::XmlMarkup</tt> object to let you create
# a completely custom XML.
def xml(directive_tag = :xml, attrs = {})
- @xml = yield builder(directive_tag, attrs) if block_given?
+ return unless block_given?
+
+ xml = builder(directive_tag, attrs)
+ yield xml
+ @xml = xml.target!
end
# Accepts an XML String and lets you specify a completely custom request body.
@@ -114,12 +114,24 @@ def xml(endpoint = nil, input = nil, body = nil)
xml.to_xml.should include("<id>1</id>")
end
- it "should accept a block" do
- xml.body do |body|
- body.user { body.id 1 }
+ context "with a block" do
+ it "should yield a Builder::XmlMarkup object" do
+ xml.body do |body|
+ body.user { body.id 1 }
+ end
+
+ xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
end
- xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
+ # https://github.com/savonrb/savon/issues/322
+ it "should use the builder (and not the block's return value) to generate the body" do
+ xml.body do |body|
+ body.user { body.id 1 }
+ body.whatever if false
+ end
+
+ xml.to_xml.should include("<authenticate><user><id>1</id></user></authenticate>")
+ end
end
end
@@ -135,22 +147,34 @@ def xml(endpoint = nil, input = nil, body = nil)
xml.to_xml.should == "<custom>xml</custom>"
end
- it "yields a Builder::XmlMarkup object to a given block" do
- xml.xml { |xml| xml.using("Builder") }
- xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
- end
+ context "with a block" do
+ it "yields a Builder::XmlMarkup object" do
+ xml.xml { |xml| xml.using("Builder") }
+ xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
+ end
- it "accepts options to pass to the Builder::XmlMarkup instruct!" do
- xml.xml :xml, :aaa => :bbb do |xml|
- xml.using("Builder")
+ it "accepts options to pass to the Builder::XmlMarkup instruct!" do
+ xml.xml :xml, :aaa => :bbb do |xml|
+ xml.using("Builder")
+ end
+
+ xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8" aaa="bbb"?><using>Builder</using>'
end
- xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8" aaa="bbb"?><using>Builder</using>'
- end
+ it "allows to change the encoding" do
+ xml.xml(:xml, :encoding => "US-ASCII") { |xml| xml.using("Builder") }
+ xml.to_xml.should == '<?xml version="1.0" encoding="US-ASCII"?><using>Builder</using>'
+ end
- it "allows to change the encoding" do
- xml.xml(:xml, :encoding => "US-ASCII") { |xml| xml.using("Builder") }
- xml.to_xml.should == '<?xml version="1.0" encoding="US-ASCII"?><using>Builder</using>'
+ # https://github.com/savonrb/savon/issues/322
+ it "uses the builder (and not the block's return value) to generate the xml" do
+ xml.xml do |xml|
+ xml.using("Builder")
+ xml.whatever if false
+ end
+
+ xml.to_xml.should == '<?xml version="1.0" encoding="UTF-8"?><using>Builder</using>'
+ end
end
end

0 comments on commit 0179e5a

Please sign in to comment.