wadl 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_wadl.rb ADDED
@@ -0,0 +1,303 @@
1
+ # Unit tests for the Ruby WADL library.
2
+
3
+ require 'test/unit'
4
+ require 'wadl'
5
+
6
+ class WADLTest < Test::Unit::TestCase
7
+
8
+ def wadl(wadl)
9
+ WADL::Application.from_wadl(<<-EOT)
10
+ <?xml version="1.0"?>
11
+ <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
12
+ xsi:schemaLocation="http://research.sun.com/wadl wadl.xsd"
13
+ xmlns:xsd="http://www.w3.org/2001/XMLSchema"
14
+ xmlns="http://research.sun.com/wadl">
15
+ #{wadl}
16
+ </application>
17
+ EOT
18
+ end
19
+
20
+ # Null test to shut the compiler up.
21
+ def test_null
22
+ end
23
+
24
+ end
25
+
26
+ class FindingWhatYouNeed < WADLTest
27
+
28
+ def setup
29
+ @wadl = wadl(<<-EOT)
30
+ <resources base="http://www.example.com/">
31
+ <resource id="green_things" path="green">
32
+ <resource href="#frogs" />
33
+ <resource id="pistachios" path="pistachio" />
34
+ <method href="#fetch" />
35
+ </resource>
36
+
37
+ <resource id="hopping_things" path="hop">
38
+ <resource href="#frogs" />
39
+ </resource>
40
+
41
+ <resource id="frogs" path="frog">
42
+ <method name="POST" id="fetch_frog" />
43
+ </resource>
44
+ </resources>
45
+
46
+ <method name="GET" id="fetch" />
47
+ EOT
48
+ end
49
+
50
+ # Test the ability to find a resource by ID, to find a sub-resource
51
+ # of a resource, and to dereference a resource.
52
+ def test_find_resource_by_id
53
+ green_things = @wadl.find_resource(:green_things)
54
+ frogs = @wadl.find_resource(:frogs)
55
+
56
+ assert_equal(green_things.id, 'green_things')
57
+ assert_equal(frogs.path, 'frog')
58
+ assert_equal(green_things.find_resource(:frogs), frogs)
59
+ assert_equal(green_things.find_resource(:pistachios).path, "pistachio")
60
+ end
61
+
62
+ # Test the ability to find a resource by path.
63
+ def test_find_resource_by_path
64
+ green_things = @wadl.green
65
+ assert_equal(green_things.id, 'green_things')
66
+
67
+ green_things = @wadl.find_resource_by_path('green')
68
+ assert_equal(green_things.id, 'green_things')
69
+
70
+ green_things = @wadl.resource('green')
71
+ assert_equal(green_things.id, 'green_things')
72
+
73
+ frogs = green_things.find_resource_by_path('frog')
74
+ assert_equal(frogs.id, 'frogs')
75
+ end
76
+
77
+ # Dereference a resource two different ways and construct two different
78
+ # URIs from the same resource.
79
+ def test_dereference_resource
80
+ green_frogs = @wadl.green_things.frogs
81
+ assert_equal(green_frogs.uri, 'http://www.example.com/green/frog')
82
+
83
+ hopping_frogs = @wadl.hopping_things.frogs
84
+ assert_equal(hopping_frogs.uri, 'http://www.example.com/hop/frog')
85
+ end
86
+
87
+ # Find a resource's method by id or HTTP action.
88
+ def test_find_method
89
+ frogs = @wadl.find_resource(:frogs)
90
+
91
+ assert_equal(frogs.find_method_by_id(:fetch_frog).name, 'POST')
92
+ assert_equal(frogs.find_method_by_http_method('POST').id, 'fetch_frog')
93
+ end
94
+
95
+ # Dereference a resource's method.
96
+ def test_find_dereferenced_method
97
+ green_things = @wadl.find_resource(:green_things)
98
+ assert_equal(green_things.find_method_by_id(:fetch).name, 'GET')
99
+ end
100
+
101
+ end
102
+
103
+ class PathParameters < WADLTest
104
+
105
+ def setup
106
+ @wadl = wadl(<<-EOT)
107
+ <resources base="http://www.example.com/">
108
+ <resource id="mad" path="im/mad/because">
109
+ <resource href="#insult" />
110
+ </resource>
111
+
112
+ <resource id="insult" path="the/{person}/is/{a}">
113
+ <param name="a" default="dork" style="matrix" />
114
+ <param name="and" style="matrix" />
115
+ <resource id="so-let's" path="so-let's/{do something}" />
116
+ </resource>
117
+ </resources>
118
+ EOT
119
+ end
120
+
121
+ def test_path_parameter_substitution
122
+ insult_resource = @wadl.find_resource_by_path('the/{person}/is/{a}')
123
+
124
+ # Test simple substitution.
125
+ assert_equal(insult_resource.uri(:path => { 'person' => 'king', 'a' => 'fink' }),
126
+ 'http://www.example.com/the/king/is/;a=fink')
127
+ # Test default values.
128
+ assert_equal(insult_resource.uri(:path => { 'person' => 'king' }),
129
+ 'http://www.example.com/the/king/is/;a=dork')
130
+
131
+ # Test use of optional paramaters.
132
+ assert_equal(insult_resource.uri(:path => { 'person' => 'king', 'a' => 'fink',
133
+ 'and' => 'he can bite me' }),
134
+ 'http://www.example.com/the/king/is/;a=fink/;and=he%20can%20bite%20me')
135
+
136
+ # Don't provide required argument.
137
+ assert_raises(ArgumentError) { insult_resource.uri }
138
+
139
+ # Provide multiple values for single-valued argument.
140
+ assert_raises(ArgumentError) {
141
+ insult_resource.uri(:path => { :person => 'king', :a => ['fink', 'dolt'] })
142
+ }
143
+ end
144
+
145
+ # Test enumerated options for parameters
146
+ def test_options
147
+ resource = wadl(<<-EOT).find_resource('fate')
148
+ <resources base="http://www.example.com/">
149
+ <resource id="fate" path="fates/{fate}">
150
+ <param name="fate">
151
+ <option value="Clotho" />
152
+ <option value="Lachesis" />
153
+ <option value="Atropos" />
154
+ </param>
155
+ </resource>
156
+ </resources>
157
+ EOT
158
+
159
+ assert_equal(resource.uri(:path => { :fate => 'Clotho' }),
160
+ 'http://www.example.com/fates/Clotho')
161
+
162
+ assert_raises(ArgumentError) { resource.uri(:path => { :fate => 'Groucho' }) }
163
+ end
164
+
165
+ # This one's complicated. We bind a resource's path parameters to
166
+ # specific values, then get a sub-resource of the bound resource and
167
+ # bind _its_ path parameters. This tests the BoundResource delegate
168
+ # class.
169
+ def test_bound_resource_traversal
170
+ im_mad_because = @wadl.find_resource('mad')
171
+ assert_equal(im_mad_because.uri, 'http://www.example.com/im/mad/because')
172
+
173
+ insult = im_mad_because.find_resource('insult')
174
+ assert_equal(insult.uri(:path => { 'person' => 'king', 'a' => 'fink' }),
175
+ 'http://www.example.com/im/mad/because/the/king/is/;a=fink')
176
+
177
+ im_mad_because_hes_a_fink = insult.bind!(:path => { 'person' => 'king', 'a' => 'fink' })
178
+ assert_equal(im_mad_because_hes_a_fink.uri,
179
+ 'http://www.example.com/im/mad/because/the/king/is/;a=fink')
180
+
181
+ im_mad_because_hes_a_fink_lets = im_mad_because_hes_a_fink.find_resource("so-let's")
182
+ assert_equal(im_mad_because_hes_a_fink_lets.uri(:path => { 'do something' => 'revolt' }),
183
+ "http://www.example.com/im/mad/because/the/king/is/;a=fink/so-let's/revolt")
184
+
185
+ im_mad_because_hes_a_fink_lets_revolt = im_mad_because_hes_a_fink_lets.
186
+ bind(:path => { 'person' => 'fink', 'do something' => 'revolt' })
187
+
188
+ assert_equal(im_mad_because_hes_a_fink_lets_revolt.uri,
189
+ "http://www.example.com/im/mad/because/the/king/is/;a=fink/so-let's/revolt")
190
+ end
191
+
192
+ def test_repeating_arguments
193
+ text = <<-EOT
194
+ <resources base="http://www.example.com/">
195
+ <resource id="list" path="i/want/{a}">
196
+ <param name="a" repeating="true" style="%s" />
197
+ </resource>
198
+ </resources>
199
+ EOT
200
+
201
+ # NOTE: Repeating plain arguments get separated by commas
202
+ # (an arbitrary decision on my part).
203
+ { 'plain' => 'http://www.example.com/i/want/pony,water%20slide,BB%20gun',
204
+ 'matrix' => 'http://www.example.com/i/want/;a=pony;a=water%20slide;a=BB%20gun' }.each { |style, uri|
205
+ list = wadl(text % style).find_resource('list')
206
+ assert_equal(list.uri(:path => { :a => ['pony', 'water slide', 'BB gun'] }), uri)
207
+ }
208
+ end
209
+
210
+ def test_fixed_values
211
+ poll = wadl(<<-EOT).find_resource('poll')
212
+ <resources base="http://www.example.com/">
213
+ <resource id="poll" path="big-brother-is/{opinion}">
214
+ <param name="opinion" fixed="doubleplusgood" />
215
+ </resource>
216
+ </resources>
217
+ EOT
218
+
219
+ assert_equal(poll.uri(:opinion => 'ungood'),
220
+ 'http://www.example.com/big-brother-is/doubleplusgood')
221
+ end
222
+
223
+ def test_matrix_values
224
+ lights = wadl(<<-EOT).find_resource('blinkenlights')
225
+ <resources base="http://www.example.com/">
226
+ <resource id="blinkenlights" path="light-panel/{light1}{light2}{light3}">
227
+ <param name="light1" type="xsd:boolean" style="matrix" fixed="true" />
228
+ <param name="light2" type="xsd:boolean" style="matrix" fixed="false" />
229
+ <param name="light3" type="xsd:boolean" style="matrix" />
230
+ </resource>
231
+ </resources>
232
+ EOT
233
+
234
+ on_uri = 'http://www.example.com/light-panel/;light1;light3'
235
+ off_uri = 'http://www.example.com/light-panel/;light1'
236
+
237
+ assert_equal(lights.uri(:path => { :light3 => 'true' }), on_uri)
238
+ assert_equal(lights.uri(:path => { :light3 => '1' }), on_uri)
239
+
240
+ assert_equal(lights.uri, off_uri)
241
+ assert_equal(lights.uri(:path => { :light3 => 'false' }), off_uri)
242
+ assert_equal(lights.uri(:path => { :light3 => false }), off_uri)
243
+ assert_equal(lights.uri(:path => { :light3 => 'True' }), off_uri)
244
+ assert_equal(lights.uri(:path => { :light3 => true }), off_uri)
245
+ end
246
+
247
+ class RequestFormatTests < WADLTest
248
+
249
+ def setup
250
+ @wadl = wadl(<<-EOT)
251
+ <resources base="http://www.example.com/">
252
+ <resource id="top" path="palette">
253
+ <param style="form" name="api_key" />
254
+ <resource id="color" path="colors/{color}">
255
+ <method href="#get_graphic" />
256
+ <method href="#set_graphic" />
257
+ </resource>
258
+ </resource>
259
+ </resources>
260
+
261
+ <method name="GET" id="get_graphic">
262
+ <request>
263
+ <param name="shade" type="xsd:string" required="true" />
264
+ </request>
265
+ </method>
266
+
267
+ <method name="POST" id="set_graphic">
268
+ <request>
269
+ <representation mediaType="application/x-www-form-encoded">
270
+ <param name="new_graphic" type="xsd:string" required="true" />
271
+ <param name="filename" type="xsd:string" required="true" />
272
+ </representation>
273
+ </request>
274
+ </method>
275
+ EOT
276
+
277
+ @color = @wadl.find_resource('top').bind(:query => { :api_key => 'foobar' }).find_resource('color')
278
+ end
279
+
280
+ def test_query_vars
281
+ graphic = @color.find_method('get_graphic')
282
+ path = { :color => 'blue' }
283
+ query = { :shade => 'light' }
284
+
285
+ assert_equal(graphic.request.uri(@color, :path => path, :query => query),
286
+ 'http://www.example.com/palette/colors/blue?shade=light')
287
+
288
+ assert_raises(ArgumentError) { graphic.request.uri(@color, path) }
289
+ end
290
+
291
+ def test_representation
292
+ graphic = @color.find_method('set_graphic')
293
+ representation = graphic.request.find_form
294
+
295
+ assert_equal(representation % { :new_graphic => 'foobar', 'filename' => 'blue.jpg' },
296
+ 'new_graphic=foobar&filename=blue.jpg')
297
+
298
+ assert_raises(ArgumentError) { representation % { :new_graphic => 'foobar' } }
299
+ end
300
+
301
+ end
302
+
303
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wadl
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Leonard Richardson
13
+ - Jens Wille
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-07 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rest-open-uri
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: mime-types
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ description: Ruby client for the Web Application Description Language.
46
+ email:
47
+ - leonardr@segfault.org
48
+ - jens.wille@uni-koeln.de
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - COPYING
55
+ - ChangeLog
56
+ - README
57
+ files:
58
+ - lib/wadl.rb
59
+ - lib/wadl/version.rb
60
+ - README
61
+ - ChangeLog
62
+ - Rakefile
63
+ - COPYING
64
+ - examples/yahoo.wadl
65
+ - examples/YahooSearch.rb
66
+ - examples/crummy.rb
67
+ - examples/yahoo.rb
68
+ - examples/crummy.wadl
69
+ - examples/delicious.wadl
70
+ - examples/YahooSearch.wadl
71
+ - examples/delicious.rb
72
+ - test/test_wadl.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/blackwinter/wadl
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --charset
80
+ - UTF-8
81
+ - --title
82
+ - wadl Application documentation
83
+ - --main
84
+ - README
85
+ - --line-numbers
86
+ - --inline-source
87
+ - --all
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ segments:
95
+ - 0
96
+ version: "0"
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ requirements: []
105
+
106
+ rubyforge_project:
107
+ rubygems_version: 1.3.6
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Ruby client for the Web Application Description Language.
111
+ test_files: []
112
+