Skip to content

Commit

Permalink
fixed response#header and #body /cc #474
Browse files Browse the repository at this point in the history
both methods now respect the global :convert_response_tags_to and
:strip_namespaces options and return the expected result instead of
raising a Savon::InvalidResponseError.
  • Loading branch information
rubiii committed Jun 30, 2013
1 parent cfcd2b7 commit befd779
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
* Fix: [#450](https://github.com/savonrb/savon/pull/450) Added `Savon::Response#soap_fault`
and `Savon::Response#http_error` which were present in version 1.

* Fix: [#474](https://github.com/savonrb/savon/issues/474) Changed `Savon::Response#header` and
`Savon::Response#body` to respect the global `:convert_response_tags_to` and `:strip_namespaces`
options and return the expected result instead of raising a `Savon::InvalidResponseError`.

### 2.2.0 (2013-04-21)

* Feature: [#416](https://github.com/savonrb/savon/pull/416) The global `namespace_identifier`
Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ gemspec
gem "httpclient", "~> 2.3.0"
gem "simplecov", :require => false, :group => :test
gem "coveralls", :require => false

gem "nori", :github => "savonrb/nori"
13 changes: 9 additions & 4 deletions lib/savon/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ def http_error?
end

def header
raise_invalid_response_error! unless hash.key? :envelope
hash[:envelope][:header]
find('Header')
end

def body
raise_invalid_response_error! unless hash.key? :envelope
hash[:envelope][:body]
find('Body')
end

alias_method :to_hash, :body
Expand Down Expand Up @@ -69,6 +67,13 @@ def xpath(path, namespaces = nil)
doc.xpath(path, namespaces || xml_namespaces)
end

def find(*path)
envelope = nori.find(hash, 'Envelope')
raise_invalid_response_error! unless envelope

nori.find(envelope, *path)
end

private

def build_soap_and_http_errors!
Expand Down
10 changes: 6 additions & 4 deletions spec/savon/options_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,13 @@

context "global :strip_namespaces" do
it "can be changed to not strip any namespaces" do
client = new_client(:endpoint => @server.url(:repeat), :convert_response_tags_to => lambda { |tag| tag.snakecase }, :strip_namespaces => false)
response = client.call(:authenticate, :xml => Fixture.response(:authentication))
client = new_client(
:endpoint => @server.url(:repeat),
:convert_response_tags_to => lambda { |tag| tag.snakecase },
:strip_namespaces => false
)

# the header/body convenience methods fails when conventions are not met. [dh, 2012-12-12]
expect { response.body }.to raise_error(Savon::InvalidResponseError)
response = client.call(:authenticate, :xml => Fixture.response(:authentication))

expect(response.hash["soap:envelope"]["soap:body"]).to include("ns2:authenticate_response")
end
Expand Down
50 changes: 50 additions & 0 deletions spec/savon/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,29 @@
response.header.should include(:session_number => "ABCD1234")
end

it 'respects the global :strip_namespaces option' do
globals[:strip_namespaces] = false

response_with_header = soap_response(:body => Fixture.response(:header))
header = response_with_header.header

expect(header).to be_a(Hash)

# notice: :session_number is a snake_case Symbol without namespaces,
# but the Envelope and Header elements are qualified.
expect(header.keys).to include(:session_number)
end

it 'respects the global :convert_response_tags_to option' do
globals[:convert_response_tags_to] = lambda { |key| key.upcase }

response_with_header = soap_response(:body => Fixture.response(:header))
header = response_with_header.header

expect(header).to be_a(Hash)
expect(header.keys).to include('SESSIONNUMBER')
end

it "should throw an exception when the response header isn't parsable" do
lambda { invalid_soap_response.header }.should raise_error Savon::InvalidResponseError
end
Expand All @@ -120,6 +143,24 @@
hash[:multi_namespaced_entry_response][:history].should be_a(Hash)
hash[:multi_namespaced_entry_response][:history][:case].should be_an(Array)
end

it 'respects the global :strip_namespaces option' do
globals[:strip_namespaces] = false

body = soap_response.body

expect(body).to be_a(Hash)
expect(body.keys).to include(:"ns2:authenticate_response")
end

it 'respects the global :convert_response_tags_to option' do
globals[:convert_response_tags_to] = lambda { |key| key.upcase }

body = soap_response.body

expect(body).to be_a(Hash)
expect(body.keys).to include('AUTHENTICATERESPONSE')
end
end
end

Expand Down Expand Up @@ -175,6 +216,15 @@
end
end

describe '#find' do
it 'delegates to Nori#find to find child elements inside the Envelope' do
result = soap_response.find('Body', 'authenticateResponse', 'return')

expect(result).to be_a(Hash)
expect(result.keys).to include(:authentication_value)
end
end

describe "#http" do
it "should return the HTTPI::Response" do
soap_response.http.should be_an(HTTPI::Response)
Expand Down

0 comments on commit befd779

Please sign in to comment.