wash_out 0.5.6 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,550 +3,530 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe WashOut do
6
- before(:each) do
6
+ before :each do
7
7
  WashOut::Engine.snakecase_input = true
8
8
  WashOut::Engine.camelize_wsdl = true
9
9
  WashOut::Engine.namespace = false
10
10
  end
11
11
 
12
- it "should be valid" do
13
- WashOut.should be_a(Module)
12
+ let :nori do
13
+ Nori.new(
14
+ :strip_namespaces => true,
15
+ :advanced_typecasting => true,
16
+ :convert_tags_to => lambda {|x| x.snakecase.to_sym}
17
+ )
14
18
  end
15
19
 
16
- it "should allow to include SOAP module" do
17
- lambda {
18
- mock_controller do
19
- # nothing
20
- end
21
- }.should_not raise_exception
22
- end
20
+ def savon(method, message={}, &block)
21
+ message = {:value => message} unless message.is_a?(Hash)
23
22
 
24
- it "should generate WSDL" do
25
- mock_controller do
26
- soap_action :result, :args => nil, :return => :int
27
- def answer
28
- render :soap => "42"
29
- end
23
+ savon = Savon::Client.new(:log => false, :wsdl => 'http://app/api/wsdl', &block)
24
+ savon.call(method, :message => message).to_hash
25
+ end
30
26
 
31
- soap_action "getArea", :args => { :circle => { :center => { :x => [:integer],
32
- :y => :integer },
33
- :radius => :double } },
34
- :return => { :area => :double,
35
- :distance_from_o => :double },
36
- :to => :get_area
37
- def get_area
38
- circle = params[:circle]
39
- render :soap => { :area => Math::PI * circle[:radius] ** 2,
40
- :distance_from_o => Math.sqrt(circle[:center][:x] ** 2 + circle[:center][:y] ** 2) }
41
- end
27
+ describe "Module" do
28
+ it "includes" do
29
+ lambda {
30
+ mock_controller do
31
+ # nothing
32
+ end
33
+ }.should_not raise_exception
34
+ end
42
35
 
43
- soap_action "rocky", :args => { :circle1 => { :x => :integer } },
44
- :return => { :circle2 => { :y => :integer } }
45
- def rocky; end
36
+ it "allows definition of a simple action" do
37
+ lambda {
38
+ mock_controller do
39
+ soap_action "answer", :args => nil, :return => :integer
40
+ end
41
+ }.should_not raise_exception
46
42
  end
43
+ end
47
44
 
48
- xml = Nori.parse client.wsdl.xml
45
+ describe "WSDL" do
46
+ let :wsdl do
47
+ mock_controller do
48
+ soap_action :result, :args => nil, :return => :int
49
49
 
50
- # Savon underscores method names so we
51
- # get back just what we have at controller
52
- client.wsdl.soap_actions.map{|x| x.to_s}.sort.should == [:result, :get_area, :rocky].map{|x| x.to_s}.sort
50
+ soap_action "getArea", :args => { :circle => { :center => { :x => [:integer],
51
+ :y => :integer },
52
+ :radius => :double } },
53
+ :return => { :area => :double }
53
54
 
54
- x = xml[:definitions][:types][:schema][:complex_type].find{|x| x[:'@name'] == 'Center'}[:sequence][:element].find{|x| x[:'@name'] == 'X'}
55
- x[:'@min_occurs'].should == "0"
56
- x[:'@max_occurs'].should == "unbounded"
55
+ soap_action "rocky", :args => { :circle1 => { :x => :integer } },
56
+ :return => { :circle2 => { :y => :integer } }
57
+ end
57
58
 
58
- xml[:definitions][:binding][:operation].map{|e| e[:'@name']}.sort.should == ['Result', 'getArea', 'rocky'].sort
59
+ HTTPI.get("http://app/api/wsdl").body
60
+ end
59
61
 
60
- client.wsdl.xml.include?('<xsd:complexType name="Circle1">').should == true
61
- end
62
+ let :xml do
63
+ nori.parse wsdl
64
+ end
62
65
 
63
- it "should allow definition of a simple action" do
64
- lambda {
65
- mock_controller do
66
- soap_action "answer", :args => nil, :return => :integer
67
- end
68
- }.should_not raise_exception
69
- end
66
+ it "lists operations" do
67
+ operations = xml[:definitions][:binding][:operation]
68
+ operations.should be_a_kind_of(Array)
70
69
 
71
- it "should answer to request without parameters" do
72
- mock_controller do
73
- soap_action "answer", :args => nil, :return => :int
74
- def answer
75
- render :soap => "42"
76
- end
70
+ operations.map{|e| e[:'@name']}.sort.should == ['Result', 'getArea', 'rocky'].sort
77
71
  end
78
72
 
79
- client.request(:answer).to_hash[:answer_response][:value].should == "42"
80
- end
81
-
82
- it "should respond to request with insufficient parameters" do
83
- mock_controller do
84
- soap_action "answer", :args => {:a => :integer}, :return => :integer
85
- def answer
86
- render :soap => "42"
87
- end
73
+ it "defines complex types" do
74
+ wsdl.include?('<xsd:complexType name="Circle1">').should == true
88
75
  end
89
76
 
90
- client.request(:answer).to_hash[:answer_response][:value].should == "42"
91
- end
77
+ it "defines arrays" do
78
+ x = xml[:definitions][:types][:schema][:complex_type].
79
+ find{|x| x[:'@name'] == 'Center'}[:sequence][:element].
80
+ find{|x| x[:'@name'] == 'X'}
92
81
 
93
- it "should answer to request with empty parameter" do
94
- mock_controller do
95
- soap_action "answer", :args => {:a => :string}, :return => {:a => :string}
96
- def answer
97
- render :soap => {:a => params[:a]}
98
- end
82
+ x[:'@min_occurs'].should == "0"
83
+ x[:'@max_occurs'].should == "unbounded"
99
84
  end
100
-
101
- client.request(:answer) do
102
- soap.body = { :a => '' }
103
- end.to_hash[:answer_response][:a].should == {:"@xsi:type"=>"xsd:string"}
104
85
  end
105
86
 
106
- it "should answer to request with one parameter" do
107
- mock_controller do
108
- soap_action "checkAnswer", :args => :integer, :return => :boolean, :to => 'check_answer'
109
- def check_answer
110
- render :soap => (params[:value] == 42)
111
- end
112
- end
87
+ describe "Dispatcher" do
113
88
 
114
- client.request(:check_answer) do
115
- soap.body = { :value => 42 }
116
- end.to_hash[:check_answer_response][:value].should == true
117
- client.request(:check_answer) do
118
- soap.body = { :value => 13 }
119
- end.to_hash[:check_answer_response][:value].should == false
120
- end
89
+ context "simple actions" do
90
+ it "accept no parameters" do
91
+ mock_controller do
92
+ soap_action "answer", :args => nil, :return => :int
93
+ def answer
94
+ render :soap => "42"
95
+ end
96
+ end
121
97
 
122
- it "handles incorrect requests" do
123
- mock_controller do
124
- soap_action "duty",
125
- :args => {:bad => {:a => :string, :b => :string}, :good => {:a => :string, :b => :string}},
126
- :return => nil
127
- def duty
128
- render :soap => nil
98
+ savon(:answer)[:answer_response][:value].
99
+ should == "42"
129
100
  end
130
- end
131
101
 
132
- lambda {
133
- client.request(:duty) do
134
- soap.body = { :bad => 42, :good => nil }
135
- end
136
- }.should raise_exception(Savon::SOAP::Fault)
137
- end
102
+ it "accept insufficient parameters" do
103
+ mock_controller do
104
+ soap_action "answer", :args => {:a => :integer}, :return => :integer
105
+ def answer
106
+ render :soap => "42"
107
+ end
108
+ end
138
109
 
139
- it "respects :response_tag option" do
140
- mock_controller do
141
- soap_action "specific", :response_tag => "test", :return => :string
142
- def specific
143
- render :soap => "test"
110
+ savon(:answer)[:answer_response][:value].
111
+ should == "42"
144
112
  end
145
- end
146
113
 
147
- client.request(:specific).to_hash.should == {:test => {:value=>"test"}}
148
- end
114
+ it "accept empty parameter" do
115
+ mock_controller do
116
+ soap_action "answer", :args => {:a => :string}, :return => {:a => :string}
117
+ def answer
118
+ render :soap => {:a => params[:a]}
119
+ end
120
+ end
121
+ savon(:answer, :a => '')[:answer_response][:a].
122
+ should == {:"@xsi:type"=>"xsd:string"}
123
+ end
149
124
 
150
- it "should handle snakecase option properly" do
151
- WashOut::Engine.snakecase_input = false
152
- WashOut::Engine.camelize_wsdl = false
125
+ it "accept one parameter" do
126
+ mock_controller do
127
+ soap_action "checkAnswer", :args => :integer, :return => :boolean, :to => 'check_answer'
128
+ def check_answer
129
+ render :soap => (params[:value] == 42)
130
+ end
131
+ end
153
132
 
154
- mock_controller do
155
- soap_action "rocknroll", :args => {:ZOMG => :string}, :return => nil
156
- def rocknroll
157
- params["ZOMG"].should == "yam!"
158
- render :soap => nil
133
+ savon(:check_answer, 42)[:check_answer_response][:value].should == true
134
+ savon(:check_answer, 13)[:check_answer_response][:value].should == false
159
135
  end
160
- end
161
-
162
- client.request(:rocknroll) do
163
- soap.body = { "ZOMG" => 'yam!' }
164
- end
165
- end
166
136
 
167
- context "optional arrays" do
168
- it "should answer for simple structure" do
169
- mock_controller do
170
- soap_action "rocknroll",
171
- :args => nil, :return => { :my_value => [:integer] }
172
- def rocknroll
173
- render :soap => {}
137
+ it "accept two parameters" do
138
+ mock_controller do
139
+ soap_action "funky", :args => { :a => :integer, :b => :string }, :return => :string
140
+ def funky
141
+ render :soap => ((params[:a] * 10).to_s + params[:b])
142
+ end
174
143
  end
175
- end
176
144
 
177
- client.request(:rocknroll).to_hash[:rocknroll_response].should be_nil
145
+ savon(:funky, :a => 42, :b => 'k')[:funky_response][:value].should == '420k'
146
+ end
178
147
  end
179
148
 
180
- it "should answer for complex structure" do
181
- mock_controller do
182
- soap_action "rocknroll",
183
- :args => nil, :return => { :my_value => [{ :value => :integer}] }
184
- def rocknroll
185
- render :soap => {}
149
+ context "complex actions" do
150
+ it "accept nested structures" do
151
+ mock_controller do
152
+ soap_action "getArea", :args => { :circle => { :center => { :x => :integer,
153
+ :y => :integer },
154
+ :radius => :double } },
155
+ :return => { :area => :double,
156
+ :distance_from_o => :double },
157
+ :to => :get_area
158
+ def get_area
159
+ circle = params[:circle]
160
+ render :soap => { :area => Math::PI * circle[:radius] ** 2,
161
+ :distance_from_o => Math.sqrt(circle[:center][:x] ** 2 + circle[:center][:y] ** 2) }
162
+ end
186
163
  end
187
- end
188
164
 
189
- client.request(:rocknroll).to_hash[:rocknroll_response].should be_nil
190
- end
165
+ message = { :circle => { :center => { :x => 3, :y => 4 },
166
+ :radius => 5 } }
191
167
 
192
- it "should answer for nested complex structure" do
193
- mock_controller do
194
- soap_action "rocknroll",
195
- :args => nil, :return => { :my_value => { :my_array => [{ :value => :integer}] } }
196
- def rocknroll
197
- render :soap => {}
168
+ savon(:get_area, message)[:get_area_response].
169
+ should == ({ :area => (Math::PI * 25).to_s, :distance_from_o => (5.0).to_s })
170
+ end
171
+
172
+ it "accept arrays" do
173
+ mock_controller do
174
+ soap_action "rumba",
175
+ :args => {
176
+ :rumbas => [:integer]
177
+ },
178
+ :return => nil
179
+ def rumba
180
+ params.should == {"rumbas" => [1, 2, 3]}
181
+ render :soap => nil
182
+ end
198
183
  end
199
- end
200
184
 
201
- client.request(:rocknroll).to_hash[:rocknroll_response][:my_value].should == { :"@xsi:type" => "tns:MyValue" }
202
- end
203
- end
185
+ savon(:rumba, :rumbas => [1, 2, 3])
186
+ end
187
+
188
+ it "accept nested structures inside arrays" do
189
+ mock_controller do
190
+ soap_action "rumba",
191
+ :args => {
192
+ :rumbas => [ {
193
+ :zombies => :string,
194
+ :puppies => :string
195
+ } ]
196
+ },
197
+ :return => nil
198
+ def rumba
199
+ params.should == {
200
+ "rumbas" => [
201
+ {"zombies" => 'suck', "puppies" => 'rock'},
202
+ {"zombies" => 'slow', "puppies" => 'fast'}
203
+ ]
204
+ }
205
+ render :soap => nil
206
+ end
207
+ end
204
208
 
205
- it "should answer to request with two parameter" do
206
- mock_controller do
207
- soap_action "funky", :args => { :a => :integer, :b => :string }, :return => :string
208
- def funky
209
- render :soap => ((params[:a] * 10).to_s + params[:b])
209
+ savon :rumba, :rumbas => [
210
+ {:zombies => 'suck', :puppies => 'rock'},
211
+ {:zombies => 'slow', :puppies => 'fast'}
212
+ ]
210
213
  end
211
- end
212
214
 
213
- client.request(:funky) do
214
- soap.body = { :a => 42, :b => 'k' }
215
- end.to_hash[:funky_response][:value].should == '420k'
216
- end
215
+ it "respond with nested structures" do
216
+ mock_controller do
217
+ soap_action "gogogo",
218
+ :args => nil,
219
+ :return => {
220
+ :zoo => :string,
221
+ :boo => { :moo => :string, :doo => :string }
222
+ }
223
+ def gogogo
224
+ render :soap => {
225
+ :zoo => 'zoo',
226
+ :boo => { :moo => 'moo', :doo => 'doo' }
227
+ }
228
+ end
229
+ end
217
230
 
218
- it "should understand nested parameter specifications" do
219
- mock_controller do
220
- soap_action "getArea", :args => { :circle => { :center => { :x => :integer,
221
- :y => :integer },
222
- :radius => :double } },
223
- :return => { :area => :double,
224
- :distance_from_o => :double },
225
- :to => :get_area
226
- def get_area
227
- circle = params[:circle]
228
- render :soap => { :area => Math::PI * circle[:radius] ** 2,
229
- :distance_from_o => Math.sqrt(circle[:center][:x] ** 2 + circle[:center][:y] ** 2) }
231
+ savon(:gogogo)[:gogogo_response].
232
+ should == {:zoo=>"zoo", :boo=>{:moo=>"moo", :doo=>"doo", :"@xsi:type"=>"tns:Boo"}}
230
233
  end
231
- end
232
234
 
233
- client.request(:get_area) do
234
- soap.body = { :circle => { :center => { :x => 3, :y => 4 },
235
- :radius => 5 } }
236
- end.to_hash[:get_area_response].should == ({ :area => (Math::PI * 25).to_s, :distance_from_o => (5.0).to_s })
237
- end
235
+ it "respond with arrays" do
236
+ mock_controller do
237
+ soap_action "rumba",
238
+ :args => nil,
239
+ :return => [:integer]
240
+ def rumba
241
+ render :soap => [1, 2, 3]
242
+ end
243
+ end
244
+
245
+ savon(:rumba)[:rumba_response].should == {:value => ["1", "2", "3"]}
246
+ end
238
247
 
239
- it "should allow arbitrary action names" do
240
- name = 'AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything'
248
+ it "respond with complex structures inside arrays" do
249
+ mock_controller do
250
+ soap_action "rumba",
251
+ :args => nil,
252
+ :return => {
253
+ :rumbas => [{:zombies => :string, :puppies => :string}]
254
+ }
255
+ def rumba
256
+ render :soap =>
257
+ {:rumbas => [
258
+ {:zombies => "suck1", :puppies => "rock1" },
259
+ {:zombies => "suck2", :puppies => "rock2" }
260
+ ]
261
+ }
262
+ end
263
+ end
241
264
 
242
- mock_controller do
243
- soap_action name,
244
- :args => nil, :return => :integer, :to => :answer
245
- def answer
246
- render :soap => "forty two"
265
+ savon(:rumba)[:rumba_response].should == {
266
+ :rumbas => [
267
+ {:zombies => "suck1",:puppies => "rock1", :"@xsi:type"=>"tns:Rumbas"},
268
+ {:zombies => "suck2", :puppies => "rock2", :"@xsi:type"=>"tns:Rumbas" }
269
+ ]
270
+ }
247
271
  end
248
- end
249
272
 
250
- client.request(name).to_hash["#{name.underscore}_response".to_sym][:value].should == "forty two"
251
- end
273
+ it "respond with structs in structs in arrays" do
274
+ mock_controller do
275
+ soap_action "rumba",
276
+ :args => nil,
277
+ :return => [{:rumbas => {:zombies => :integer}}]
252
278
 
253
- it "should correctly report SOAP errors" do
254
- mock_controller do
255
- soap_action "error", :args => { :need_error => :boolean }, :return => nil
256
- def error
257
- raise self.class.const_get(:SOAPError), "you wanted one" if params[:need_error]
279
+ def rumba
280
+ render :soap => [{:rumbas => {:zombies => 100000}}, {:rumbas => {:zombies => 2}}]
281
+ end
282
+ end
258
283
 
259
- render :soap => nil
284
+ savon(:rumba)[:rumba_response].should == {
285
+ :value => [
286
+ {
287
+ :rumbas => {
288
+ :zombies => "100000",
289
+ :"@xsi:type" => "tns:Rumbas"
290
+ },
291
+ :"@xsi:type" => "tns:Value"
292
+ },
293
+ {
294
+ :rumbas => {
295
+ :zombies => "2",
296
+ :"@xsi:type" => "tns:Rumbas"
297
+ },
298
+ :"@xsi:type"=>"tns:Value"
299
+ }
300
+ ]
301
+ }
260
302
  end
261
- end
262
303
 
263
- lambda {
264
- client.request(:error) do
265
- soap.body = { :need_error => false }
266
- end
267
- }.should_not raise_exception
268
- lambda {
269
- client.request(:error) do
270
- soap.body = { :need_error => true }
271
- end
272
- }.should raise_exception(Savon::SOAP::Fault)
273
- end
304
+ context "with arrays missing" do
305
+ it "respond with simple definition" do
306
+ mock_controller do
307
+ soap_action "rocknroll",
308
+ :args => nil, :return => { :my_value => [:integer] }
309
+ def rocknroll
310
+ render :soap => {}
311
+ end
312
+ end
274
313
 
275
- it "should report a SOAP error if method does not exists" do
276
- mock_controller
314
+ savon(:rocknroll)[:rocknroll_response].should be_nil
315
+ end
277
316
 
278
- lambda {
279
- client.request(:nonexistent)
280
- }.should raise_exception(Savon::SOAP::Fault)
281
- end
317
+ it "respond with complext definition" do
318
+ mock_controller do
319
+ soap_action "rocknroll",
320
+ :args => nil, :return => { :my_value => [{ :value => :integer}] }
321
+ def rocknroll
322
+ render :soap => {}
323
+ end
324
+ end
282
325
 
283
- it "should be possible to explicitly render a SOAP error" do
284
- mock_controller do
285
- soap_action "error", :args => nil, :return => nil
286
- def error
287
- render_soap_error "a message"
326
+ savon(:rocknroll)[:rocknroll_response].should be_nil
327
+ end
328
+
329
+ it "respond with nested simple definition" do
330
+ mock_controller do
331
+ soap_action "rocknroll",
332
+ :args => nil, :return => { :my_value => { :my_array => [{ :value => :integer}] } }
333
+ def rocknroll
334
+ render :soap => {}
335
+ end
336
+ end
337
+
338
+ savon(:rocknroll)[:rocknroll_response][:my_value].
339
+ should == { :"@xsi:type" => "tns:MyValue" }
340
+ end
288
341
  end
289
342
  end
290
343
 
291
- lambda {
292
- client.request(:error)
293
- }.should raise_exception(Savon::SOAP::Fault)
294
- end
344
+ context "types" do
345
+ it "recognize boolean" do
346
+ mock_controller do
347
+ soap_action "true", :args => :boolean, :return => :nil
348
+ def true
349
+ params[:value].should == true
350
+ render :soap => nil
351
+ end
295
352
 
296
- it "should handle nested returns" do
297
- mock_controller do
298
- soap_action "gogogo",
299
- :args => nil,
300
- :return => {
301
- :zoo => :string,
302
- :boo => {
303
- :moo => :string,
304
- :doo => :string
305
- }
306
- }
307
- def gogogo
308
- render :soap => {
309
- :zoo => 'zoo',
310
- :boo => {
311
- :moo => 'moo',
312
- :doo => 'doo'
313
- }
314
- }
353
+ soap_action "false", :args => :boolean, :return => :nil
354
+ def false
355
+ params[:value].should == false
356
+ render :soap => nil
357
+ end
358
+ end
359
+
360
+ savon(:true, :value => true)
361
+ savon(:false, :value => false)
315
362
  end
316
- end
317
363
 
318
- client.request(:gogogo)[:gogogo_response].should == {:zoo=>"zoo", :boo=>{:moo=>"moo", :doo=>"doo", :"@xsi:type"=>"tns:Boo"}}
319
- end
364
+ it "recognize dates" do
365
+ mock_controller do
366
+ soap_action "date", :args => :date, :return => :nil
367
+ def date
368
+ params[:value].should == Date.parse('2000-12-30') unless params[:value].blank?
369
+ render :soap => nil
370
+ end
371
+ end
320
372
 
321
- it "should handle arrays" do
322
- mock_controller do
323
- soap_action "rumba",
324
- :args => {
325
- :rumbas => [:integer]
326
- },
327
- :return => nil
328
- def rumba
329
- params.should == {"rumbas" => [1, 2, 3]}
330
- render :soap => nil
373
+ savon(:date, :value => '2000-12-30')
374
+ lambda { savon(:date) }.should_not raise_exception
331
375
  end
332
376
  end
333
377
 
334
- client.request(:rumba) do
335
- soap.body = {
336
- :rumbas => [1, 2, 3]
337
- }
338
- end
339
- end
378
+ context "errors" do
379
+ it "raise for incorrect requests" do
380
+ mock_controller do
381
+ soap_action "duty",
382
+ :args => {:bad => {:a => :string, :b => :string}, :good => {:a => :string, :b => :string}},
383
+ :return => nil
384
+ def duty
385
+ render :soap => nil
386
+ end
387
+ end
340
388
 
341
- it "should handle complex structures inside arrays" do
342
- mock_controller do
343
- soap_action "rumba",
344
- :args => {
345
- :rumbas => [ {
346
- :zombies => :string,
347
- :puppies => :string
348
- } ]
349
- },
350
- :return => nil
351
- def rumba
352
- params.should == {
353
- "rumbas" => [
354
- {"zombies" => 'suck', "puppies" => 'rock'},
355
- {"zombies" => 'slow', "puppies" => 'fast'}
356
- ]
357
- }
358
- render :soap => nil
389
+ lambda {
390
+ savon(:duty, :bad => 42, :good => nil)
391
+ }.should raise_exception(Savon::SOAPFault)
359
392
  end
360
- end
361
393
 
362
- client.request(:rumba) do
363
- soap.body = {
364
- :rumbas => [
365
- {:zombies => 'suck', :puppies => 'rock'},
366
- {:zombies => 'slow', :puppies => 'fast'}
367
- ]
368
- }
369
- end
370
- end
394
+ it "raise to report SOAP errors" do
395
+ mock_controller do
396
+ soap_action "error", :args => { :need_error => :boolean }, :return => nil
397
+ def error
398
+ raise self.class.const_get(:SOAPError), "you wanted one" if params[:need_error]
399
+ render :soap => nil
400
+ end
401
+ end
371
402
 
372
- it "should be able to return arrays" do
373
- mock_controller do
374
- soap_action "rumba",
375
- :args => nil,
376
- :return => [:integer]
377
- def rumba
378
- render :soap => [1, 2, 3]
403
+ lambda { savon(:error, :need_error => false) }.should_not raise_exception
404
+ lambda { savon(:error, :need_error => true) }.should raise_exception(Savon::SOAPFault)
379
405
  end
380
- end
381
406
 
382
- client.request(:rumba).to_hash[:rumba_response].should == {:value => ["1", "2", "3"]}
383
- end
407
+ # TODO: New Savon doesn't allow you to call methods that are not available among WSDL
408
+ xit "raise for nonexistent method" do
409
+ mock_controller
384
410
 
385
- it "should deprecate old syntax" do
386
- # save rspec context check
387
- raise_runtime_exception = raise_exception(RuntimeError)
411
+ lambda { savon(:nonexistent) }.should raise_exception(Savon::SOAPFault)
412
+ end
388
413
 
389
- mock_controller do
390
- lambda {
391
- soap_action "rumba",
392
- :args => :integer,
393
- :return => []
394
- }.should raise_runtime_exception
395
- def rumba
396
- render :soap => nil
414
+ it "raise for manual throws" do
415
+ mock_controller do
416
+ soap_action "error", :args => nil, :return => nil
417
+ def error
418
+ render_soap_error "a message"
419
+ end
420
+ end
421
+
422
+ lambda { savon(:error) }.should raise_exception(Savon::SOAPFault)
397
423
  end
398
- end
399
- end
400
424
 
401
- it "should handle return of complex structures inside arrays" do
402
- mock_controller do
403
- soap_action "rumba",
404
- :args => nil,
405
- :return => {
406
- :rumbas => [{:zombies => :string, :puppies => :string}]
407
- }
408
- def rumba
409
- render :soap =>
410
- {:rumbas => [
411
- {:zombies => "suck1", :puppies => "rock1" },
412
- {:zombies => "suck2", :puppies => "rock2" }
413
- ]
425
+ it "raise when response structure mismatches" do
426
+ mock_controller do
427
+ soap_action 'bad', :args => :integer, :return => {
428
+ :basic => :string,
429
+ :stallions => {
430
+ :stallion => [
431
+ :name => :string,
432
+ :wyldness => :integer,
433
+ ]
434
+ },
414
435
  }
415
- end
416
- end
436
+ def bad
437
+ render :soap => {
438
+ :basic => 'hi',
439
+ :stallions => [{:name => 'ted', :wyldness => 11}]
440
+ }
441
+ end
417
442
 
418
- client.request(:rumba)[:rumba_response].should == {
419
- :rumbas => [
420
- {:zombies => "suck1",:puppies => "rock1", :"@xsi:type"=>"tns:Rumbas"},
421
- {:zombies => "suck2", :puppies => "rock2", :"@xsi:type"=>"tns:Rumbas" }
422
- ]
423
- }
424
- end
443
+ soap_action 'bad2', :args => :integer, :return => {
444
+ :basic => :string,
445
+ :telephone_booths => [:string]
446
+ }
447
+ def bad2
448
+ render :soap => {
449
+ :basic => 'hihi',
450
+ :telephone_booths => 'oops'
451
+ }
452
+ end
453
+ end
425
454
 
426
- it "should handle return of structs in structs in arrays" do
427
- mock_controller do
428
- soap_action "rumba",
429
- :args => nil,
430
- :return => [{:rumbas => {:zombies => :integer}}]
455
+ lambda { savon(:bad) }.should raise_exception(
456
+ WashOut::Dispatcher::ProgrammerError,
457
+ /SOAP response .*wyldness.*Array.*Hash.*stallion/
458
+ )
431
459
 
432
- def rumba
433
- render :soap => [{:rumbas => {:zombies => 100000}}, {:rumbas => {:zombies => 2}}]
460
+ lambda { savon(:bad2) }.should raise_exception(
461
+ WashOut::Dispatcher::ProgrammerError,
462
+ /SOAP response .*oops.*String.*telephone_booths.*Array/
463
+ )
434
464
  end
435
465
  end
436
466
 
437
- client.request(:rumba)[:rumba_response].should == {
438
- :value => [
439
- {
440
- :rumbas => {
441
- :zombies => "100000",
442
- :"@xsi:type" => "tns:Rumbas"
443
- },
444
- :"@xsi:type" => "tns:Value"
445
- },
446
- {
447
- :rumbas => {
448
- :zombies => "2",
449
- :"@xsi:type" => "tns:Rumbas"
450
- },
451
- :"@xsi:type"=>"tns:Value"
452
- }
453
- ]
454
- }
455
- end
456
-
457
- it "should handle complex structs/arrays" do
458
- mock_controller do
459
- soap_action "rumba",
460
- :args => nil,
461
- :return => {
462
- :rumbas => [
463
- {
464
- :zombies => :string,
465
- :puppies => [
466
- {:kittens => :integer}
467
- ]
468
- }
469
- ]
470
- }
467
+ context "deprecates" do
468
+ it "old syntax" do
469
+ # save rspec context check
470
+ raise_runtime_exception = raise_exception(RuntimeError)
471
471
 
472
- def rumba
473
- render :soap => {
474
- :rumbas => [
475
- {
476
- :zombies => "abc",
477
- :puppies => [
478
- {:kittens => 1},
479
- {:kittens => 5},
480
- ]
481
- },
482
- {
483
- :zombies => "def",
484
- :puppies => [
485
- {:kittens => 4}
486
- ]
487
- }
488
- ]
489
- }
472
+ mock_controller do
473
+ lambda {
474
+ soap_action "rumba",
475
+ :args => :integer,
476
+ :return => []
477
+ }.should raise_runtime_exception
478
+ def rumba
479
+ render :soap => nil
480
+ end
481
+ end
490
482
  end
491
483
  end
492
484
 
493
- client.request(:rumba)[:rumba_response].should == {
494
- :rumbas => [
495
- {
496
- :zombies => "abc",
497
- :puppies => [
498
- {
499
- :kittens => "1",
500
- :"@xsi:type" => "tns:Puppies"
501
- },
502
- {
503
- :kittens => "5",
504
- :"@xsi:type" => "tns:Puppies"
505
- }
506
- ],
507
- :"@xsi:type"=>"tns:Rumbas"
508
- },
509
- {
510
- :zombies => "def",
511
- :puppies => {
512
- :kittens => "4",
513
- :"@xsi:type" => "tns:Puppies"
514
- },
515
- :"@xsi:type"=>"tns:Rumbas"
516
- }
517
- ]
518
- }
519
- end
485
+ it "allows arbitrary action names" do
486
+ name = 'AnswerToTheUltimateQuestionOfLifeTheUniverseAndEverything'
520
487
 
521
- it "handles dates" do
522
- mock_controller do
523
- soap_action "date",
524
- :args => :date,
525
- :return => :nil
526
- def date
527
- params[:value].should == Date.parse('2000-12-30') unless params[:value].blank?
528
- render :soap => nil
488
+ mock_controller do
489
+ soap_action name, :args => nil, :return => :integer, :to => :answer
490
+ def answer
491
+ render :soap => "forty two"
492
+ end
529
493
  end
494
+
495
+ savon(name.underscore.to_sym)["#{name.underscore}_response".to_sym][:value].
496
+ should == "forty two"
530
497
  end
531
498
 
532
- client.request(:date) do
533
- soap.body = {
534
- :value => '2000-12-30'
535
- }
499
+ it "respects :response_tag option" do
500
+ mock_controller do
501
+ soap_action "specific", :response_tag => "test", :return => :string
502
+ def specific
503
+ render :soap => "test"
504
+ end
505
+ end
506
+
507
+ savon(:specific).should == {:test => {:value=>"test"}}
536
508
  end
537
509
 
538
- lambda {
539
- client.request(:date) do
540
- soap.body = {
541
- :value => nil
542
- }
510
+ it "handles snakecase option properly" do
511
+ WashOut::Engine.snakecase_input = false
512
+ WashOut::Engine.camelize_wsdl = false
513
+
514
+ mock_controller do
515
+ soap_action "rocknroll", :args => {:ZOMG => :string}, :return => nil
516
+ def rocknroll
517
+ params["ZOMG"].should == "yam!"
518
+ render :soap => nil
519
+ end
543
520
  end
544
- }.should_not raise_exception
521
+
522
+ savon(:rocknroll, "ZOMG" => 'yam!')
523
+ end
524
+
545
525
  end
546
526
 
547
- describe "ws-security" do
527
+ describe "WS Security" do
548
528
 
549
- it "should append username_token to params, if present" do
529
+ it "appends username_token to params" do
550
530
  WashOut::Engine.wsse_username = nil
551
531
  WashOut::Engine.wsse_password = nil
552
532
 
@@ -559,14 +539,12 @@ describe WashOut do
559
539
  end
560
540
  end
561
541
 
562
- client.request(:check_token) do
563
- wsse.username = "gorilla"
564
- wsse.password = "secret"
565
- soap.body = { :value => 42 }
542
+ savon(:check_token, 42) do
543
+ wsse_auth "gorilla", "secret"
566
544
  end
567
545
  end
568
546
 
569
- it "should handle PasswordText auth" do
547
+ it "handles PasswordText auth" do
570
548
  WashOut::Engine.wsse_username = "gorilla"
571
549
  WashOut::Engine.wsse_password = "secret"
572
550
 
@@ -578,38 +556,23 @@ describe WashOut do
578
556
  end
579
557
 
580
558
  # correct auth
581
- lambda {
582
- client.request(:check_auth) do
583
- wsse.username = "gorilla"
584
- wsse.password = "secret"
585
- soap.body = { :value => 42 }
586
- end
587
- }.should_not raise_exception
559
+ lambda { savon(:check_auth, 42){ wsse_auth "gorilla", "secret" } }.
560
+ should_not raise_exception
561
+
588
562
  # wrong user
589
- lambda {
590
- client.request(:check_auth) do
591
- wsse.username = "chimpanzee"
592
- wsse.password = "secret"
593
- soap.body = { :value => 42 }
594
- end
595
- }.should raise_exception(Savon::SOAP::Fault)
563
+ lambda { savon(:check_auth, 42){ wsse_auth "chimpanzee", "secret" } }.
564
+ should raise_exception(Savon::SOAPFault)
565
+
596
566
  # wrong pass
597
- lambda {
598
- client.request(:check_auth) do
599
- wsse.username = "gorilla"
600
- wsse.password = "nicetry"
601
- soap.body = { :value => 42 }
602
- end
603
- }.should raise_exception(Savon::SOAP::Fault)
567
+ lambda { savon(:check_auth, 42){ wsse_auth "gorilla", "nicetry" } }.
568
+ should raise_exception(Savon::SOAPFault)
569
+
604
570
  # no auth
605
- lambda {
606
- client.request(:check_auth) do
607
- soap.body = { :value => 42 }
608
- end
609
- }.should raise_exception(Savon::SOAP::Fault)
571
+ lambda { savon(:check_auth, 42) }.
572
+ should raise_exception(Savon::SOAPFault)
610
573
  end
611
574
 
612
- it "should handle PasswordDigest auth" do
575
+ it "handles PasswordDigest auth" do
613
576
  WashOut::Engine.wsse_username = "gorilla"
614
577
  WashOut::Engine.wsse_password = "secret"
615
578
 
@@ -621,77 +584,22 @@ describe WashOut do
621
584
  end
622
585
 
623
586
  # correct auth
624
- lambda {
625
- client.request(:check_auth) do
626
- wsse.credentials "gorilla", "secret", :digest
627
- soap.body = { :value => 42 }
628
- end
629
- }.should_not raise_exception
630
- # wrong user
631
- lambda {
632
- client.request(:check_auth) do
633
- wsse.credentials "chimpanzee", "secret", :digest
634
- soap.body = { :value => 42 }
635
- end
636
- }.should raise_exception(Savon::SOAP::Fault)
637
- # wrong pass
638
- lambda {
639
- client.request(:check_auth) do
640
- wsse.credentials "gorilla", "nicetry", :digest
641
- soap.body = { :value => 42 }
642
- end
643
- }.should raise_exception(Savon::SOAP::Fault)
644
- end
587
+ lambda { savon(:check_auth, 42){ wsse_auth "gorilla", "secret", :digest } }.
588
+ should_not raise_exception
645
589
 
646
- end
647
-
648
- it 'will not let you pass an Array in the place of a Hash' do
649
- mock_controller do
650
- soap_action 'bad', :args => :integer, :return => {
651
- :basic => :string,
652
- :stallions => {
653
- :stallion => [
654
- :name => :string,
655
- :wyldness => :integer,
656
- ]
657
- },
658
- }
659
- def bad
660
- render :soap => {
661
- :basic => 'hi',
662
- :stallions => [{:name => 'ted', :wyldness => 11}]
663
- }
664
- end
665
- end
590
+ # wrong user
591
+ lambda { savon(:check_auth, 42){ wsse_auth "chimpanzee", "secret", :digest } }.
592
+ should raise_exception(Savon::SOAPFault)
666
593
 
667
- lambda {
668
- client.request(:bad).to_hash(:bad_response)
669
- }.should raise_exception(
670
- WashOut::Dispatcher::ProgrammerError,
671
- /SOAP response .*wyldness.*Array.*Hash.*stallion/
672
- )
673
- end
594
+ # wrong pass
595
+ lambda { savon(:check_auth, 42){ wsse_auth "gorilla", "nicetry", :digest } }.
596
+ should raise_exception(Savon::SOAPFault)
674
597
 
675
- it 'loudly fails if you return a string in place of an Array' do
676
- mock_controller do
677
- soap_action 'bad2', :args => :integer, :return => {
678
- :basic => :string,
679
- :telephone_booths => [:string]
680
- }
681
- def bad2
682
- render :soap => {
683
- :basic => 'hihi',
684
- :telephone_booths => 'oops'
685
- }
686
- end
598
+ # no auth
599
+ lambda { savon(:check_auth, 42) }.
600
+ should raise_exception(Savon::SOAPFault)
687
601
  end
688
602
 
689
- lambda {
690
- client.request(:bad2).to_hash[:bad_response]
691
- }.should raise_exception(
692
- WashOut::Dispatcher::ProgrammerError,
693
- /SOAP response .*oops.*String.*telephone_booths.*Array/
694
- )
695
603
  end
696
604
 
697
605
  end