forked from savonrb/savon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
response_spec.rb
239 lines (192 loc) · 7.31 KB
/
response_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require "spec_helper"
describe Savon::SOAP::Response do
describe ".new" do
it "should raise a Savon::SOAP::Fault in case of a SOAP fault" do
lambda { soap_fault_response }.should raise_error(Savon::SOAP::Fault)
end
it "should not raise a Savon::SOAP::Fault in case the default is turned off" do
Savon.raise_errors = false
lambda { soap_fault_response }.should_not raise_error(Savon::SOAP::Fault)
Savon.raise_errors = true
end
it "should raise a Savon::HTTP::Error in case of an HTTP error" do
lambda { soap_response :code => 500 }.should raise_error(Savon::HTTP::Error)
end
it "should not raise a Savon::HTTP::Error in case the default is turned off" do
Savon.raise_errors = false
soap_response :code => 500
Savon.raise_errors = true
end
end
describe "#success?" do
around do |example|
Savon.raise_errors = false
example.run
Savon.raise_errors = true
end
it "should return true if the request was successful" do
soap_response.should be_a_success
end
it "should return false if there was a SOAP fault" do
soap_fault_response.should_not be_a_success
end
it "should return false if there was an HTTP error" do
http_error_response.should_not be_a_success
end
end
describe "#soap_fault?" do
around do |example|
Savon.raise_errors = false
example.run
Savon.raise_errors = true
end
it "should not return true in case the response seems to be ok" do
soap_response.soap_fault?.should be_false
end
it "should return true in case of a SOAP fault" do
soap_fault_response.soap_fault?.should be_true
end
end
describe "#soap_fault" do
around do |example|
Savon.raise_errors = false
example.run
Savon.raise_errors = true
end
it "should return a Savon::SOAP::Fault" do
soap_fault_response.soap_fault.should be_a(Savon::SOAP::Fault)
end
it "should return a Savon::SOAP::Fault containing the HTTPI::Response" do
soap_fault_response.soap_fault.http.should be_an(HTTPI::Response)
end
it "should return a Savon::SOAP::Fault even if the SOAP response seems to be ok" do
soap_response.soap_fault.should be_a(Savon::SOAP::Fault)
end
end
describe "#http_error?" do
around do |example|
Savon.raise_errors = false
example.run
Savon.raise_errors = true
end
it "should not return true in case the response seems to be ok" do
soap_response.http_error?.should_not be_true
end
it "should return true in case of an HTTP error" do
soap_response(:code => 500).http_error?.should be_true
end
end
describe "#http_error" do
around do |example|
Savon.raise_errors = false
example.run
Savon.raise_errors = true
end
it "should return a Savon::HTTP::Error" do
http_error_response.http_error.should be_a(Savon::HTTP::Error)
end
it "should return a Savon::HTTP::Error containing the HTTPI::Response" do
http_error_response.http_error.http.should be_an(HTTPI::Response)
end
it "should return a Savon::HTTP::Error even if the HTTP response seems to be ok" do
soap_response.http_error.should be_a(Savon::HTTP::Error)
end
end
describe "#[]" do
it "should return the SOAP response body as a Hash" do
soap_response[:authenticate_response][:return].should ==
Fixture.response_hash(:authentication)[:authenticate_response][:return]
end
it "should throw an exception when the response body isn't parsable" do
lambda { invalid_soap_response.body }.should raise_error Savon::SOAP::InvalidResponseError
end
end
describe "#header" do
it "should return the SOAP response header as a Hash" do
response = soap_response :body => Fixture.response(:header)
response.header.should include(:session_number => "ABCD1234")
end
it "should throw an exception when the response header isn't parsable" do
lambda { invalid_soap_response.header }.should raise_error Savon::SOAP::InvalidResponseError
end
end
%w(body to_hash).each do |method|
describe "##{method}" do
it "should return the SOAP response body as a Hash" do
soap_response.send(method)[:authenticate_response][:return].should ==
Fixture.response_hash(:authentication)[:authenticate_response][:return]
end
it "should return a Hash for a SOAP multiRef response" do
hash = soap_response(:body => Fixture.response(:multi_ref)).send(method)
hash[:list_response].should be_a(Hash)
hash[:multi_ref].should be_an(Array)
end
it "should add existing namespaced elements as an array" do
hash = soap_response(:body => Fixture.response(:list)).send(method)
hash[:multi_namespaced_entry_response][:history].should be_a(Hash)
hash[:multi_namespaced_entry_response][:history][:case].should be_an(Array)
end
end
end
describe "#to_array" do
context "when the given path exists" do
it "should return an Array containing the path value" do
soap_response.to_array(:authenticate_response, :return).should ==
[Fixture.response_hash(:authentication)[:authenticate_response][:return]]
end
end
context "when the given path returns nil" do
it "should return an empty Array" do
soap_response.to_array(:authenticate_response, :undefined).should == []
end
end
context "when the given path does not exist at all" do
it "should return an empty Array" do
soap_response.to_array(:authenticate_response, :some, :undefined, :path).should == []
end
end
end
describe "#hash" do
it "should return the complete SOAP response XML as a Hash" do
response = soap_response :body => Fixture.response(:header)
response.hash[:envelope][:header][:session_number].should == "ABCD1234"
end
end
describe "#to_xml" do
it "should return the raw SOAP response body" do
soap_response.to_xml.should == Fixture.response(:authentication)
end
end
describe "#doc" do
it "returns a Nokogiri::XML::Document for the SOAP response XML" do
soap_response.doc.should be_a(Nokogiri::XML::Document)
end
end
describe "#xpath" do
it "permits XPath access to elements in the request" do
soap_response.xpath("//client").first.inner_text.should == "radclient"
soap_response.xpath("//ns2:authenticateResponse/return/success").first.inner_text.should == "true"
end
end
describe "#http" do
it "should return the HTTPI::Response" do
soap_response.http.should be_an(HTTPI::Response)
end
end
def soap_response(options = {})
defaults = { :code => 200, :headers => {}, :body => Fixture.response(:authentication) }
response = defaults.merge options
Savon::SOAP::Response.new HTTPI::Response.new(response[:code], response[:headers], response[:body])
end
def soap_fault_response
soap_response :code => 500, :body => Fixture.response(:soap_fault)
end
def http_error_response
soap_response :code => 404, :body => "Not found"
end
def invalid_soap_response(options={})
defaults = { :code => 200, :headers => {}, :body => "I'm not SOAP" }
response = defaults.merge options
Savon::SOAP::Response.new HTTPI::Response.new(response[:code], response[:headers], response[:body])
end
end