/
client_spec.rb
492 lines (393 loc) · 16.9 KB
/
client_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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
require "spec_helper"
describe Savon::Client do
let(:client) { Savon::Client.new { wsdl.document = Endpoint.wsdl } }
describe ".new" do
context "with a String" do
it "should set the WSDL document" do
wsdl = "http://example.com/UserService?wsdl"
client = Savon::Client.new(wsdl)
client.wsdl.instance_variable_get("@document").should == wsdl
end
end
context "with a block expecting one argument" do
it "should yield the WSDL object" do
Savon::Client.new { |wsdl| wsdl.should be_a(Savon::Wasabi::Document) }
end
end
context "with a block expecting two arguments" do
it "should yield the WSDL and HTTP objects" do
Savon::Client.new do |wsdl, http|
wsdl.should be_an(Savon::Wasabi::Document)
http.should be_an(HTTPI::Request)
end
end
end
context "with a block expecting three arguments" do
it "should yield the WSDL, HTTP and WSSE objects" do
Savon::Client.new do |wsdl, http, wsse|
wsdl.should be_an(Savon::Wasabi::Document)
http.should be_an(HTTPI::Request)
wsse.should be_an(Akami::WSSE)
end
end
end
context "with a block expecting no arguments" do
it "should let you access the WSDL object" do
Savon::Client.new { wsdl.should be_a(Savon::Wasabi::Document) }
end
it "should let you access the HTTP object" do
Savon::Client.new { http.should be_an(HTTPI::Request) }
end
it "should let you access the WSSE object" do
Savon::Client.new { wsse.should be_a(Akami::WSSE) }
end
end
end
describe "#wsdl" do
it "should return the Savon::Wasabi::Document" do
client.wsdl.should be_a(Savon::Wasabi::Document)
end
end
describe "#http" do
it "should return the HTTPI::Request" do
client.http.should be_an(HTTPI::Request)
end
end
describe "#wsse" do
it "should return the Akami::WSSE object" do
client.wsse.should be_a(Akami::WSSE)
end
end
describe "#request" do
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:authentication)))
HTTPI.stubs(:post).returns(new_response)
end
context "without any arguments" do
it "should raise an ArgumentError" do
lambda { client.request }.should raise_error(ArgumentError)
end
end
context "with a single argument (Symbol)" do
it "should set the input tag to result in <getUser>" do
client.request(:get_user) { soap.input.should == [nil, :getUser, {}] }
end
it "should set the target namespace with the default identifier" do
namespace = 'xmlns:wsdl="http://v1_0.ws.auth.order.example.com/"'
HTTPI::Request.any_instance.expects(:body=).with { |value| value.include? namespace }
client.request :get_user
end
it "should not set the target namespace if soap.namespace was set to nil" do
namespace = 'wsdl="http://v1_0.ws.auth.order.example.com/"'
HTTPI::Request.any_instance.expects(:body=).with { |value| !value.include?(namespace) }
client.request(:get_user) { soap.namespace = nil }
end
context "when the wsdl's operation namespace identifier matches a document identifier" do
before do
client.wsdl.operations[:authenticate][:namespace_identifier] = "tns"
end
it "sets the soap's namespace identifier to the matching operation's namespace identifier" do
client.request(:authenticate) { soap.namespace_identifier.should == :tns }
end
it "sets the soap's namespace to the namspace matching the identifier" do
client.request(:authenticate) { soap.namespace.should == "http://v1_0.ws.auth.order.example.com/" }
end
it "sets the input tag to result in <tns:authenticate>" do
client.request(:authenticate) { soap.input.should == [:tns, :authenticate, {}] }
end
end
end
context "with a single argument (String)" do
it "should set the input tag to result in <get_user>" do
client.request("get_user") { soap.input.should == [nil, :get_user, {}] }
end
end
context "with a Symbol and a Hash" do
it "should set the input tag to result in <getUser active='true'>" do
client.request(:get_user, :active => true) { soap.input.should == [nil, :getUser, { :active => true }] }
end
it "should use the :soap_action key to set the SOAPAction header" do
client.request(:get_user, :soap_action => :test_action) { http.headers["SOAPAction"].should == %{"testAction"} }
end
end
context "with two Symbols" do
it "should set the input tag to result in <wsdl:getUser>" do
client.request(:v1, :get_user) { soap.input.should == [:v1, :getUser, {}] }
end
it "should set the target namespace with the given identifier" do
namespace = 'xmlns:v1="http://v1_0.ws.auth.order.example.com/"'
HTTPI::Request.any_instance.expects(:body=).with { |value| value.include? namespace }
client.request :v1, :get_user
end
it "should not set the target namespace if soap.namespace was set to nil" do
namespace = 'xmlns:v1="http://v1_0.ws.auth.order.example.com/"'
HTTPI::Request.any_instance.expects(:body=).with { |value| !value.include?(namespace) }
client.request(:v1, :get_user) { soap.namespace = nil }
end
end
context "with two Symbols and a Hash" do
it "should set the input tag to result in <wsdl:getUser active='true'>" do
client.request(:wsdl, :get_user, :active => true) { soap.input.should == [:wsdl, :getUser, { :active => true }] }
end
it "should use the :soap_action key to set the SOAPAction header" do
client.request(:wsdl, :get_user, :soap_action => :test_action) { http.headers["SOAPAction"].should == %{"testAction"} }
end
end
context "with a block expecting one argument" do
it "should yield the SOAP object" do
client.request(:authenticate) { |soap| soap.should be_a(Savon::SOAP::XML) }
end
end
context "with a block expecting two arguments" do
it "should yield the SOAP and WSDL objects" do
client.request(:authenticate) do |soap, wsdl|
soap.should be_a(Savon::SOAP::XML)
wsdl.should be_an(Savon::Wasabi::Document)
end
end
end
context "with a block expecting three arguments" do
it "should yield the SOAP, WSDL and HTTP objects" do
client.request(:authenticate) do |soap, wsdl, http|
soap.should be_a(Savon::SOAP::XML)
wsdl.should be_an(Savon::Wasabi::Document)
http.should be_an(HTTPI::Request)
end
end
end
context "with a block expecting four arguments" do
it "should yield the SOAP, WSDL, HTTP and WSSE objects" do
client.request(:authenticate) do |soap, wsdl, http, wsse|
soap.should be_a(Savon::SOAP::XML)
wsdl.should be_a(Savon::Wasabi::Document)
http.should be_an(HTTPI::Request)
wsse.should be_a(Akami::WSSE)
end
end
end
context "with a block expecting no arguments" do
it "should let you access the SOAP object" do
client.request(:authenticate) { soap.should be_a(Savon::SOAP::XML) }
end
it "should let you access the HTTP object" do
client.request(:authenticate) { http.should be_an(HTTPI::Request) }
end
it "should let you access the WSSE object" do
client.request(:authenticate) { wsse.should be_a(Akami::WSSE) }
end
it "should let you access the WSDL object" do
client.request(:authenticate) { wsdl.should be_a(Savon::Wasabi::Document) }
end
end
it "should not set the Cookie header for the next request" do
client.http.headers.expects(:[]=).with("Cookie", anything).never
client.http.headers.stubs(:[]=).with("SOAPAction", '"authenticate"')
client.http.headers.stubs(:[]=).with("Content-Type", "text/xml;charset=UTF-8")
client.http.headers.stubs(:[]=).with("Content-Length", "383")
client.request :authenticate
end
end
context "#request with a Set-Cookie response header" do
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:authentication)))
HTTPI.stubs(:post).returns(new_response(:headers => { "Set-Cookie" => "some-cookie=choc-chip; Path=/; HttpOnly" }))
end
it "should set the Cookie header for the next request" do
client.request :authenticate
client.http.headers["Cookie"].should == "some-cookie=choc-chip"
end
it "should set additional cookies when new cookies are found in subsequent requests" do
client.request :authenticate
HTTPI.stubs(:post).returns(new_response(:headers => { "Set-Cookie" => "second-cookie=oatmeal; Path=/; HttpOnly" }))
client.request :nibble
client.http.headers["Cookie"].split(";").should include("some-cookie=choc-chip", "second-cookie=oatmeal")
end
end
context "with a remote WSDL document" do
let(:client) { Savon::Client.new { wsdl.document = Endpoint.wsdl } }
before { HTTPI.expects(:get).returns(new_response(:body => Fixture.wsdl(:authentication))) }
it "adds a SOAPAction header containing the SOAP action name" do
HTTPI.stubs(:post).returns(new_response)
client.request :authenticate do
http.headers["SOAPAction"].should == %{"authenticate"}
end
end
it "should execute SOAP requests and return the response" do
HTTPI.expects(:post).returns(new_response)
response = client.request(:authenticate)
response.should be_a(Savon::SOAP::Response)
response.to_xml.should == Fixture.response(:authentication)
end
end
context "with a local WSDL document" do
let(:client) { Savon::Client.new { wsdl.document = "spec/fixtures/wsdl/authentication.xml" } }
before { HTTPI.expects(:get).never }
it "adds a SOAPAction header containing the SOAP action name" do
HTTPI.stubs(:post).returns(new_response)
client.request :authenticate do
http.headers["SOAPAction"].should == %{"authenticate"}
end
end
it "should execute SOAP requests and return the response" do
HTTPI.expects(:post).returns(new_response)
response = client.request(:authenticate)
response.should be_a(Savon::SOAP::Response)
response.to_xml.should == Fixture.response(:authentication)
end
end
context "when the WSDL specifies multiple namespaces" do
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:multiple_namespaces)))
HTTPI.stubs(:post).returns(new_response)
end
it "qualifies each element with the appropriate namespace" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
title = xml.at_xpath(
".//actions:Save/actions:article/article:Title/text()",
"article" => "http://example.com/article",
"actions" => "http://example.com/actions").to_s
author = xml.at_xpath(
".//actions:Save/actions:article/article:Author/text()",
"article" => "http://example.com/article",
"actions" => "http://example.com/actions").to_s
title == "Hamlet" && author == "Shakespeare"
end
client.request :save do
soap.body = { :article => { "Title" => "Hamlet", "Author" => "Shakespeare" } }
end
end
it "still sends nil as xsi:nil as in the non-namespaced case" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
attribute = xml.at_xpath(".//article:Title/@xsi:nil",
"xsi" => "http://www.w3.org/2001/XMLSchema-instance",
"article" => "http://example.com/article").to_s
attribute == "true"
end
client.request(:save) { soap.body = { :article => { "Title" => nil } } }
end
it "translates between symbol :save and string 'Save'" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
!!xml.at_xpath(".//actions:Save", "actions" => "http://example.com/actions")
end
client.request :save do
soap.body = { :article => { :title => "Hamlet", :author => "Shakespeare" } }
end
end
it "qualifies Save with the appropriate namespace" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
!!xml.at_xpath(".//actions:Save", "actions" => "http://example.com/actions")
end
client.request "Save" do
soap.body = { :article => { :title => "Hamlet", :author => "Shakespeare" } }
end
end
end
context "when the WSDL has a lowerCamel name" do
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:lower_camel)))
HTTPI.stubs(:post).returns(new_response)
end
it "appends namespace when name is specified explicitly" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
!!xml.at_xpath(".//actions:Save/actions:lowerCamel", "actions" => "http://example.com/actions")
end
client.request("Save") { soap.body = { 'lowerCamel' => 'theValue' } }
end
it "still appends namespace when converting from symbol" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
xml = Nokogiri::XML(value)
!!xml.at_xpath(".//actions:Save/actions:lowerCamel", "actions" => "http://example.com/actions")
end
client.request("Save") { soap.body = { :lower_camel => 'theValue' } }
end
end
context "with multiple types" do
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:multiple_types)))
HTTPI.stubs(:post).returns(new_response)
end
it "does not blow up" do
HTTPI::Request.any_instance.expects(:body=).with { |value| value.include?("Save") }
client.request(:save) { soap.body = {} }
end
end
context "with an Array of namespaced items" do
let(:client) { Savon::Client.new { wsdl.document = "spec/fixtures/wsdl/taxcloud.xml" } }
before do
HTTPI.stubs(:get).returns(new_response(:body => Fixture.wsdl(:taxcloud)))
HTTPI.stubs(:post).returns(new_response)
end
it "should namespaces each Array item as expected" do
HTTPI::Request.any_instance.expects(:body=).with do |value|
value.include?("<ins0:cartItems><ins0:CartItem>") && value.include?("<tns:ItemID>SKU-TEST</tns:ItemID>")
end
address = { "Address1" => "888 6th Ave", "Address2" => nil, "City" => "New York", "State" => "NY", "Zip5" => "10001", "Zip4" => nil }
cart_item = { "Index" => 0, "ItemID" => "SKU-TEST", "TIC" => "00000", "Price" => 50.0, "Qty" => 1 }
client.request :lookup, :body => {
"customerID" => 123,
"cartID" => 456,
"cartItems" => { "CartItem" => [cart_item] },
"origin" => address,
"destination" => address
}
end
end
context "without a WSDL document" do
let(:client) do
Savon::Client.new do
wsdl.endpoint = Endpoint.soap
wsdl.namespace = "http://v1_0.ws.auth.order.example.com/"
end
end
before { HTTPI.expects(:get).never }
it "raise an ArgumentError when trying to access the WSDL" do
lambda { client.wsdl.soap_actions }.should raise_error(ArgumentError, /Wasabi/)
end
it "adds a SOAPAction header containing the SOAP action name" do
HTTPI.stubs(:post).returns(new_response)
client.request :authenticate do
http.headers["SOAPAction"].should == %{"authenticate"}
end
end
it "should execute SOAP requests and return the response" do
HTTPI.expects(:post).returns(new_response)
response = client.request(:authenticate)
response.should be_a(Savon::SOAP::Response)
response.to_xml.should == Fixture.response(:authentication)
end
end
context "when encountering a SOAP fault" do
let(:client) do
Savon::Client.new do
wsdl.endpoint = Endpoint.soap
wsdl.namespace = "http://v1_0.ws.auth.order.example.com/"
end
end
before { HTTPI::expects(:post).returns(new_response(:code => 500, :body => Fixture.response(:soap_fault))) }
it "should raise a Savon::SOAP::Fault" do
lambda { client.request :authenticate }.should raise_error(Savon::SOAP::Fault)
end
end
context "when encountering an HTTP error" do
let(:client) do
Savon::Client.new do
wsdl.endpoint = Endpoint.soap
wsdl.namespace = "http://v1_0.ws.auth.order.example.com/"
end
end
before { HTTPI::expects(:post).returns(new_response(:code => 500)) }
it "should raise a Savon::HTTP::Error" do
lambda { client.request :authenticate }.should raise_error(Savon::HTTP::Error)
end
end
def new_response(options = {})
defaults = { :code => 200, :headers => {}, :body => Fixture.response(:authentication) }
response = defaults.merge options
HTTPI::Response.new response[:code], response[:headers], response[:body]
end
end