savon 2.0.3 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,144 +1,464 @@
1
1
  require "spec_helper"
2
2
  require "integration/support/server"
3
3
 
4
- describe Savon::Request do
4
+ describe Savon::WSDLRequest do
5
5
 
6
- subject(:request) { Savon::Request.new(:authenticate, wsdl, globals, locals) }
6
+ let(:globals) { Savon::GlobalOptions.new }
7
+ let(:http_request) { HTTPI::Request.new }
7
8
 
8
- let(:globals) { Savon::GlobalOptions.new(:endpoint => @server.url, :log => false) }
9
- let(:locals) { Savon::LocalOptions.new }
10
- let(:wsdl) { Wasabi::Document.new Fixture.wsdl(:authentication) }
11
- let(:no_wsdl) { Wasabi::Document.new }
12
-
13
- before :all do
14
- @server = IntegrationServer.run
9
+ def new_wsdl_request
10
+ Savon::WSDLRequest.new(globals, http_request)
15
11
  end
16
12
 
17
- after :all do
18
- @server.stop
19
- end
13
+ describe "#build" do
14
+ it "returns an HTTPI::Request" do
15
+ wsdl_request = Savon::WSDLRequest.new(globals)
16
+ expect(wsdl_request.build).to be_an(HTTPI::Request)
17
+ end
18
+
19
+ describe "proxy" do
20
+ it "is set when specified" do
21
+ globals.proxy("http://proxy.example.com")
22
+ http_request.expects(:proxy=).with("http://proxy.example.com")
23
+
24
+ new_wsdl_request.build
25
+ end
26
+
27
+ it "is not set otherwise" do
28
+ http_request.expects(:proxy=).never
29
+ new_wsdl_request.build
30
+ end
31
+ end
32
+
33
+ describe "open timeout" do
34
+ it "is set when specified" do
35
+ globals.open_timeout(22)
36
+ http_request.expects(:open_timeout=).with(22)
37
+
38
+ new_wsdl_request.build
39
+ end
40
+
41
+ it "is not set otherwise" do
42
+ http_request.expects(:open_timeout=).never
43
+ new_wsdl_request.build
44
+ end
45
+ end
46
+
47
+ describe "read timeout" do
48
+ it "is set when specified" do
49
+ globals.read_timeout(33)
50
+ http_request.expects(:read_timeout=).with(33)
51
+
52
+ new_wsdl_request.build
53
+ end
54
+
55
+ it "is not set otherwise" do
56
+ http_request.expects(:read_timeout=).never
57
+ new_wsdl_request.build
58
+ end
59
+ end
60
+
61
+ describe "ssl version" do
62
+ it "is set when specified" do
63
+ globals.ssl_version(:SSLv3)
64
+ http_request.auth.ssl.expects(:ssl_version=).with(:SSLv3)
65
+
66
+ new_wsdl_request.build
67
+ end
68
+
69
+ it "is not set otherwise" do
70
+ http_request.auth.ssl.expects(:ssl_version=).never
71
+ new_wsdl_request.build
72
+ end
73
+ end
74
+
75
+ describe "ssl verify mode" do
76
+ it "is set when specified" do
77
+ globals.ssl_verify_mode(:none)
78
+ http_request.auth.ssl.expects(:verify_mode=).with(:none)
20
79
 
21
- describe "#call" do
22
- it "expects the XML to POST" do
23
- response = request.call("<xml/>")
80
+ new_wsdl_request.build
81
+ end
24
82
 
25
- expect(request.http.url).to eq(URI(globals[:endpoint]))
26
- expect(request.http.body).to eq("<xml/>")
27
- expect(request.http.headers["Content-Length"]).to eq("<xml/>".bytesize.to_s)
83
+ it "is not set otherwise" do
84
+ http_request.auth.ssl.expects(:verify_mode=).never
85
+ new_wsdl_request.build
86
+ end
87
+ end
88
+
89
+ describe "ssl cert key file" do
90
+ it "is set when specified" do
91
+ cert_key = File.expand_path("../../fixtures/ssl/client_key.pem", __FILE__)
92
+ globals.ssl_cert_key_file(cert_key)
93
+ http_request.auth.ssl.expects(:cert_key_file=).with(cert_key)
94
+
95
+ new_wsdl_request.build
96
+ end
97
+
98
+ it "is not set otherwise" do
99
+ http_request.auth.ssl.expects(:cert_key_file=).never
100
+ new_wsdl_request.build
101
+ end
102
+ end
28
103
 
29
- expect(response).to be_a(HTTPI::Response)
104
+ describe "ssl cert key password" do
105
+ it "is set when specified" do
106
+ the_pass = "secure-password!42"
107
+ globals.ssl_cert_key_password(the_pass)
108
+ http_request.auth.ssl.expects(:cert_key_password=).with(the_pass)
109
+
110
+ new_wsdl_request.build
111
+ end
112
+
113
+ it "is not set otherwise" do
114
+ http_request.auth.ssl.expects(:cert_key_password=).never
115
+ new_wsdl_request.build
116
+ end
30
117
  end
31
118
 
32
- it "falls back to use the WSDL's endpoint if the global :endpoint option was not set" do
33
- wsdl.endpoint = @server.url
34
- globals_without_endpoint = Savon::GlobalOptions.new(:log => false)
35
- request = Savon::Request.new(:authenticate, wsdl, globals_without_endpoint, locals)
36
- response = request.call("<xml/>")
119
+ describe "ssl encrypted cert key file" do
120
+ describe "set with an invalid decrypting password" do
121
+ it "fails when attempting to use the SSL private key" do
122
+ pass = "wrong-password"
123
+ key = File.expand_path("../../fixtures/ssl/client_encrypted_key.pem", __FILE__)
124
+ cert = File.expand_path("../../fixtures/ssl/client_encrypted_key_cert.pem", __FILE__)
125
+
126
+ globals.ssl_cert_file(cert)
127
+ globals.ssl_cert_key_password(pass)
128
+ globals.ssl_cert_key_file(key)
129
+
130
+ new_wsdl_request.build
131
+
132
+ expect { http_request.auth.ssl.cert_key }.to raise_error(OpenSSL::PKey::RSAError)
133
+ end
134
+ end
37
135
 
38
- expect(request.http.url).to eq(URI(wsdl.endpoint))
136
+ describe "set with a valid decrypting password" do
137
+ it "handles SSL private keys properly" do
138
+ pass = "secure-password!42"
139
+ key = File.expand_path("../../fixtures/ssl/client_encrypted_key.pem", __FILE__)
140
+ cert = File.expand_path("../../fixtures/ssl/client_encrypted_key_cert.pem", __FILE__)
141
+
142
+ globals.ssl_cert_file(cert)
143
+ globals.ssl_cert_key_password(pass)
144
+ globals.ssl_cert_key_file(key)
145
+
146
+ new_wsdl_request.build
147
+
148
+ http_request.auth.ssl.cert_key.to_s.should =~ /BEGIN RSA PRIVATE KEY/
149
+ end
150
+ end
39
151
  end
40
152
 
41
- it "sets the global :proxy option if it's available" do
42
- globals[:proxy] = "http://proxy.example.com"
43
- expect(request.http.proxy).to eq(URI(globals[:proxy]))
153
+ describe "ssl cert file" do
154
+ it "is set when specified" do
155
+ cert = File.expand_path("../../fixtures/ssl/client_cert.pem", __FILE__)
156
+ globals.ssl_cert_file(cert)
157
+ http_request.auth.ssl.expects(:cert_file=).with(cert)
158
+
159
+ new_wsdl_request.build
160
+ end
161
+
162
+ it "is not set otherwise" do
163
+ http_request.auth.ssl.expects(:cert_file=).never
164
+ new_wsdl_request.build
165
+ end
44
166
  end
45
167
 
46
- it "does not set the global :proxy option when it's not available" do
47
- expect(request.http.proxy).to be_nil
168
+ describe "ssl ca cert file" do
169
+ it "is set when specified" do
170
+ ca_cert = File.expand_path("../../fixtures/ssl/client_cert.pem", __FILE__)
171
+ globals.ssl_ca_cert_file(ca_cert)
172
+ http_request.auth.ssl.expects(:ca_cert_file=).with(ca_cert)
173
+
174
+ new_wsdl_request.build
175
+ end
176
+
177
+ it "is not set otherwise" do
178
+ http_request.auth.ssl.expects(:ca_cert_file=).never
179
+ new_wsdl_request.build
180
+ end
48
181
  end
49
182
 
50
- it "sets the request cookies using the global :last_response option if it's available" do
51
- http = HTTPI::Response.new(200, {}, ["<success/>"])
52
- globals[:last_response] = Savon::Response.new(http, globals, locals)
183
+ describe "basic auth" do
184
+ it "is set when specified" do
185
+ globals.basic_auth("luke", "secret")
186
+ http_request.auth.expects(:basic).with("luke", "secret")
187
+
188
+ new_wsdl_request.build
189
+ end
53
190
 
54
- HTTPI::Request.any_instance.expects(:set_cookies).with(globals[:last_response]).once
55
- request
191
+ it "is not set otherwise" do
192
+ http_request.auth.expects(:basic).never
193
+ new_wsdl_request.build
194
+ end
56
195
  end
57
196
 
58
- it "does not set the cookies using the global :last_response option when it's not available" do
59
- expect(request.http.open_timeout).to be_nil
197
+ describe "digest auth" do
198
+ it "is set when specified" do
199
+ globals.digest_auth("lea", "top-secret")
200
+ http_request.auth.expects(:digest).with("lea", "top-secret")
201
+
202
+ new_wsdl_request.build
203
+ end
204
+
205
+ it "is not set otherwise" do
206
+ http_request.auth.expects(:digest).never
207
+ new_wsdl_request.build
208
+ end
60
209
  end
210
+ end
211
+
212
+ end
213
+
214
+ describe Savon::SOAPRequest do
215
+
216
+ let(:globals) { Savon::GlobalOptions.new }
217
+ let(:http_request) { HTTPI::Request.new }
61
218
 
62
- it "sets the global :open_timeout option if it's available" do
63
- globals[:open_timeout] = 33
64
- expect(request.http.open_timeout).to eq(globals[:open_timeout])
219
+ def new_soap_request
220
+ Savon::SOAPRequest.new(globals, http_request)
221
+ end
222
+
223
+ describe "#build" do
224
+ it "returns an HTTPI::Request" do
225
+ soap_request = Savon::SOAPRequest.new(globals)
226
+ expect(soap_request.build).to be_an(HTTPI::Request)
65
227
  end
66
228
 
67
- it "does not set the global :open_timeout option when it's not available" do
68
- request.call("<xml/>")
69
- expect(request.http.open_timeout).to be_nil
229
+ describe "proxy" do
230
+ it "is set when specified" do
231
+ globals.proxy("http://proxy.example.com")
232
+ http_request.expects(:proxy=).with("http://proxy.example.com")
233
+
234
+ new_soap_request.build
235
+ end
236
+
237
+ it "is not set otherwise" do
238
+ http_request.expects(:proxy=).never
239
+ new_soap_request.build
240
+ end
70
241
  end
71
242
 
72
- it "sets the global :read_timeout option if it's available" do
73
- globals[:read_timeout] = 44
74
- expect(request.http.read_timeout).to eq(globals[:read_timeout])
243
+ describe "cookies" do
244
+ it "sets the given cookies" do
245
+ cookies = [HTTPI::Cookie.new("some-cookie=choc-chip; Path=/; HttpOnly")]
246
+
247
+ http_request.expects(:set_cookies).with(cookies)
248
+ new_soap_request.build(:cookies => cookies)
249
+ end
250
+
251
+ it "does not set the cookies if there are none" do
252
+ http_request.expects(:set_cookies).never
253
+ new_soap_request.build
254
+ end
75
255
  end
76
256
 
77
- it "does not set the global :read_timeout option when it's not available" do
78
- expect(request.http.read_timeout).to be_nil
257
+ describe "open timeout" do
258
+ it "is set when specified" do
259
+ globals.open_timeout(22)
260
+ http_request.expects(:open_timeout=).with(22)
261
+
262
+ new_soap_request.build
263
+ end
264
+
265
+ it "is not set otherwise" do
266
+ http_request.expects(:open_timeout=).never
267
+ new_soap_request.build
268
+ end
79
269
  end
80
270
 
81
- it "sets the global :headers option if it's available" do
82
- globals[:headers] = { "X-Authorize" => "secret" }
83
- expect(request.http.headers["X-Authorize"]).to eq("secret")
271
+ describe "read timeout" do
272
+ it "is set when specified" do
273
+ globals.read_timeout(33)
274
+ http_request.expects(:read_timeout=).with(33)
275
+
276
+ new_soap_request.build
277
+ end
278
+
279
+ it "is not set otherwise" do
280
+ http_request.expects(:read_timeout=).never
281
+ new_soap_request.build
282
+ end
84
283
  end
85
284
 
86
- it "sets the SOAPAction header using the local :soap_action option if it's available" do
87
- locals[:soap_action] = "urn://authenticate"
88
- expect(request.http.headers["SOAPAction"]).to eq(%{"#{locals[:soap_action]}"})
285
+ describe "headers" do
286
+ it "are set when specified" do
287
+ globals.headers("X-Token" => "secret")
288
+ configured_http_request = new_soap_request.build
289
+
290
+ expect(configured_http_request.headers["X-Token"]).to eq("secret")
291
+ end
292
+
293
+ it "are not set otherwise" do
294
+ configured_http_request = new_soap_request.build
295
+ expect(configured_http_request.headers).to_not include("X-Token")
296
+ end
89
297
  end
90
298
 
91
- it "sets the SOAPAction header using the WSDL if it's available" do
92
- expect(request.http.headers["SOAPAction"]).to eq(%{"authenticate"})
299
+ describe "SOAPAction header" do
300
+ it "is set and wrapped in parenthesis" do
301
+ configured_http_request = new_soap_request.build(:soap_action => "findUser")
302
+ soap_action = configured_http_request.headers["SOAPAction"]
303
+
304
+ expect(soap_action).to eq(%("findUser"))
305
+ end
306
+
307
+ it "is not set when it's explicitely set to nil" do
308
+ configured_http_request = new_soap_request.build(:soap_action => nil)
309
+ expect(configured_http_request.headers).to_not include("SOAPAction")
310
+ end
311
+
312
+ it "is not set when there is already a SOAPAction value" do
313
+ globals.headers("SOAPAction" => %("authenticate"))
314
+ configured_http_request = new_soap_request.build(:soap_action => "findUser")
315
+ soap_action = configured_http_request.headers["SOAPAction"]
316
+
317
+ expect(soap_action).to eq(%("authenticate"))
318
+ end
93
319
  end
94
320
 
95
- it "sets the SOAPAction header using Gyoku if both option and WSDL were not set" do
96
- request = Savon::Request.new(:authenticate, no_wsdl, globals, locals)
97
- expect(request.http.headers["SOAPAction"]).to eq(%{"authenticate"})
321
+ describe "Content-Type header" do
322
+ it "defaults to SOAP 1.1 and UTF-8" do
323
+ configured_http_request = new_soap_request.build
324
+ content_type = configured_http_request.headers["Content-Type"]
325
+
326
+ expect(content_type).to eq("text/xml;charset=UTF-8")
327
+ end
328
+
329
+ it "can be changed to SOAP 1.2 and any other encoding" do
330
+ globals.soap_version(2)
331
+ globals.encoding("ISO-8859-1")
332
+
333
+ configured_http_request = new_soap_request.build
334
+ content_type = configured_http_request.headers["Content-Type"]
335
+
336
+ expect(content_type).to eq("application/soap+xml;charset=ISO-8859-1")
337
+ end
338
+
339
+ it "is not set when there is already a Content-Type value" do
340
+ globals.headers("Content-Type" => "application/awesomeness;charset=UTF-3000")
341
+ configured_http_request = new_soap_request.build(:soap_action => "findUser")
342
+ content_type = configured_http_request.headers["Content-Type"]
343
+
344
+ expect(content_type).to eq("application/awesomeness;charset=UTF-3000")
345
+ end
98
346
  end
99
347
 
100
- it "does not set the SOAPAction header if it's already set" do
101
- locals[:soap_action] = "urn://authenticate"
102
- globals[:headers] = { "SOAPAction" => %{"doAuthenticate"} }
348
+ describe "ssl version" do
349
+ it "is set when specified" do
350
+ globals.ssl_version(:SSLv3)
351
+ http_request.auth.ssl.expects(:ssl_version=).with(:SSLv3)
352
+
353
+ new_soap_request.build
354
+ end
103
355
 
104
- expect(request.http.headers["SOAPAction"]).to eq(%{"doAuthenticate"})
356
+ it "is not set otherwise" do
357
+ http_request.auth.ssl.expects(:ssl_version=).never
358
+ new_soap_request.build
359
+ end
105
360
  end
106
361
 
107
- it "does not set the SOAPAction header if the local :soap_action was set to nil" do
108
- locals[:soap_action] = nil
109
- expect(request.http.headers).to_not include("SOAPAction")
362
+ describe "ssl verify mode" do
363
+ it "is set when specified" do
364
+ globals.ssl_verify_mode(:none)
365
+ http_request.auth.ssl.expects(:verify_mode=).with(:none)
366
+
367
+ new_soap_request.build
368
+ end
369
+
370
+ it "is not set otherwise" do
371
+ http_request.auth.ssl.expects(:verify_mode=).never
372
+ new_soap_request.build
373
+ end
110
374
  end
111
375
 
112
- it "sets the SOAP 1.2 Content-Type header using the global :soap_version and :encoding options if available" do
113
- globals[:soap_version] = 2
114
- globals[:encoding] = "UTF-16"
376
+ describe "ssl cert key file" do
377
+ it "is set when specified" do
378
+ cert_key = File.expand_path("../../fixtures/ssl/client_key.pem", __FILE__)
379
+ globals.ssl_cert_key_file(cert_key)
380
+ http_request.auth.ssl.expects(:cert_key_file=).with(cert_key)
115
381
 
116
- expect(request.http.headers["Content-Type"]).to eq("application/soap+xml;charset=UTF-16")
382
+ new_soap_request.build
383
+ end
384
+
385
+ it "is not set otherwise" do
386
+ http_request.auth.ssl.expects(:cert_key_file=).never
387
+ new_soap_request.build
388
+ end
117
389
  end
118
390
 
119
- it "sets the SOAP 1.1 Content-Type header using the global :soap_version and :encoding options if available" do
120
- globals[:soap_version] = 1
121
- globals[:encoding] = "UTF-8"
391
+ describe "ssl cert key password" do
392
+ it "is set when specified" do
393
+ the_pass = "secure-password!42"
394
+ globals.ssl_cert_key_password(the_pass)
395
+ http_request.auth.ssl.expects(:cert_key_password=).with(the_pass)
396
+
397
+ new_soap_request.build
398
+ end
122
399
 
123
- expect(request.http.headers["Content-Type"]).to eq("text/xml;charset=UTF-8")
400
+ it "is not set otherwise" do
401
+ http_request.auth.ssl.expects(:cert_key_password=).never
402
+ new_soap_request.build
403
+ end
124
404
  end
125
405
 
126
- it "sets the global :basic_auth option if it's available" do
127
- globals[:basic_auth] = [:luke, "secret"]
128
- expect(request.http.auth.basic).to eq(globals[:basic_auth])
406
+ describe "ssl cert file" do
407
+ it "is set when specified" do
408
+ cert = File.expand_path("../../fixtures/ssl/client_cert.pem", __FILE__)
409
+ globals.ssl_cert_file(cert)
410
+ http_request.auth.ssl.expects(:cert_file=).with(cert)
411
+
412
+ new_soap_request.build
413
+ end
414
+
415
+ it "is not set otherwise" do
416
+ http_request.auth.ssl.expects(:cert_file=).never
417
+ new_soap_request.build
418
+ end
129
419
  end
130
420
 
131
- it "does not set the global :basic_auth option when it's not available" do
132
- expect(request.http.auth.basic).to be_nil
421
+ describe "ssl ca cert file" do
422
+ it "is set when specified" do
423
+ ca_cert = File.expand_path("../../fixtures/ssl/client_cert.pem", __FILE__)
424
+ globals.ssl_ca_cert_file(ca_cert)
425
+ http_request.auth.ssl.expects(:ca_cert_file=).with(ca_cert)
426
+
427
+ new_soap_request.build
428
+ end
429
+
430
+ it "is not set otherwise" do
431
+ http_request.auth.ssl.expects(:ca_cert_file=).never
432
+ new_soap_request.build
433
+ end
133
434
  end
134
435
 
135
- it "sets the global :digest_auth option if it's available" do
136
- globals[:digest_auth] = [:lea, "top-secret"]
137
- expect(request.http.auth.digest).to eq(globals[:digest_auth])
436
+ describe "basic auth" do
437
+ it "is set when specified" do
438
+ globals.basic_auth("luke", "secret")
439
+ http_request.auth.expects(:basic).with("luke", "secret")
440
+
441
+ new_soap_request.build
442
+ end
443
+
444
+ it "is not set otherwise" do
445
+ http_request.auth.expects(:basic).never
446
+ new_soap_request.build
447
+ end
138
448
  end
139
449
 
140
- it "does not set the global :digest_auth option when it's not available" do
141
- expect(request.http.auth.digest).to be_nil
450
+ describe "digest auth" do
451
+ it "is set when specified" do
452
+ globals.digest_auth("lea", "top-secret")
453
+ http_request.auth.expects(:digest).with("lea", "top-secret")
454
+
455
+ new_soap_request.build
456
+ end
457
+
458
+ it "is not set otherwise" do
459
+ http_request.auth.expects(:digest).never
460
+ new_soap_request.build
461
+ end
142
462
  end
143
463
  end
144
464