sessionm-resthome 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,546 @@
1
+ require 'helper'
2
+
3
+ describe RESTHome do
4
+
5
+ before(:each) do
6
+ @service_class = Class.new RESTHome
7
+ FakeWeb.allow_net_connect = false
8
+ end
9
+
10
+ class DummyProduct
11
+ attr_accessor :data
12
+ def initialize(resource)
13
+ raise 'Missing id' unless resource['id']
14
+ @data = resource
15
+ end
16
+ end
17
+
18
+ def fakeweb_response(method, url, code, value)
19
+ FakeWeb.clean_registry
20
+ status = [code.to_s, 'Ok']
21
+ FakeWeb.register_uri(method, url, :status => status, :body => value.to_json, :content_type => 'application/jsonrequest')
22
+ end
23
+
24
+ it "should build the url and options" do
25
+ @service = @service_class.new
26
+ @service.base_uri = 'http://test.dev'
27
+ @service.basic_auth = {:username => 'user', :password => 'x'}
28
+
29
+ @service.build_url('/items.json').should == 'http://test.dev/items.json'
30
+ options = {}
31
+ @service.build_options!(options)
32
+ options.should == {:basic_auth => {:username => 'user', :password => 'x'}}
33
+
34
+ options = {}
35
+ fakeweb_response(:get, 'http://user:x@test.dev/items.json', 200, {})
36
+ @service.request :get, '/items.json', options
37
+ @service.request_method.should == :get
38
+ @service.request_url.should == 'http://test.dev/items.json'
39
+ @service.request_options.should == {:basic_auth => {:username => 'user', :password => 'x'}}
40
+ end
41
+
42
+ it "should be able to create a route using GET" do
43
+ @service_class.route :products, '/products.json'
44
+ @service_class.method_defined?(:products).should be_true
45
+
46
+ @service = @service_class.new
47
+ @service.base_uri = 'http://test.dev'
48
+
49
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
50
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
51
+ @service.products
52
+ @service.request_method.should == :get
53
+ @service.request_url.should == 'http://test.dev/products.json'
54
+ @service.request_options.should == {}
55
+ end
56
+
57
+ it "should be able to create a route using POST" do
58
+ @service_class.route :create_product, '/products.json', :resource => 'product'
59
+ @service_class.route :add_products, '/products.json'
60
+ @service_class.method_defined?(:create_product).should be_true
61
+ @service_class.method_defined?(:add_products).should be_true
62
+
63
+ @service = @service_class.new
64
+ @service.base_uri = 'http://test.dev'
65
+
66
+ # Create
67
+ fakeweb_response(:post, 'http://test.dev/products.json', 201, {'product' => {'id' => 1, 'name' => 'Item1'}})
68
+ @service.create_product :name => 'Item1'
69
+ @service.request_method.should == :post
70
+ @service.request_url.should == 'http://test.dev/products.json'
71
+ @service.request_options.should == {:body => {'product' => {:name => 'Item1'}}}
72
+
73
+ # Add
74
+ fakeweb_response(:post, 'http://test.dev/products.json', 201, {'product' => {'id' => 2, 'name' => 'Item2'}})
75
+ @service.add_products 'product' => {:name => 'Item2'}
76
+ @service.request_method.should == :post
77
+ @service.request_url.should == 'http://test.dev/products.json'
78
+ @service.request_options.should == {:body => {'product' => {:name => 'Item2'}}}
79
+ end
80
+
81
+ it "should be able to create a route using PUT" do
82
+ @service_class.route :edit_product, '/products/:product_id.json', :resource => 'product'
83
+ @service_class.route :update_products, '/products/:product_id.json'
84
+ @service_class.method_defined?(:edit_product).should be_true
85
+ @service_class.method_defined?(:update_products).should be_true
86
+
87
+ @service = @service_class.new
88
+ @service.base_uri = 'http://test.dev'
89
+
90
+ # Edit
91
+ fakeweb_response(:put, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
92
+ @service.edit_product 1, :name => 'Item1'
93
+ @service.request_method.should == :put
94
+ @service.request_url.should == 'http://test.dev/products/1.json'
95
+ @service.request_options.should == {:body => {'product' => {:name => 'Item1'}}}
96
+
97
+ # Update
98
+ fakeweb_response(:put, 'http://test.dev/products/2.json', 200, {'product' => {'id' => 2, 'name' => 'Item2'}})
99
+ @service.update_products 2, 'product' => {:name => 'Item2'}
100
+ @service.request_method.should == :put
101
+ @service.request_url.should == 'http://test.dev/products/2.json'
102
+ @service.request_options.should == {:body => {'product' => {:name => 'Item2'}}}
103
+ end
104
+
105
+ it "should be able to create a route using DELETE" do
106
+ @service_class.route :delete_product, '/products/:product_id.json'
107
+ @service_class.method_defined?(:delete_product).should be_true
108
+
109
+ @service = @service_class.new
110
+ @service.base_uri = 'http://test.dev'
111
+
112
+ # Delete
113
+ fakeweb_response(:delete, 'http://test.dev/products/1.json', 200, {})
114
+ @service.delete_product 1
115
+ @service.request_method.should == :delete
116
+ @service.request_url.should == 'http://test.dev/products/1.json'
117
+ @service.request_options.should == {}
118
+ end
119
+
120
+ it "should be able to setup RESTful routes" do
121
+ @service_class.rest :product, :products, '/products.json'
122
+ @service_class.method_defined?(:products).should be_true
123
+ @service_class.method_defined?(:product).should be_true
124
+ @service_class.method_defined?(:create_product).should be_true
125
+ @service_class.method_defined?(:edit_product).should be_true
126
+ @service_class.method_defined?(:delete_product).should be_true
127
+
128
+ @service = @service_class.new
129
+ @service.base_uri = 'http://test.dev'
130
+
131
+ # Get products
132
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
133
+ [{'product' => {'id' => 1, 'name' => 'Item1'}},{'product' => {'id' => 2, 'name' => 'Item2'}}])
134
+ @service.products
135
+ @service.request_method.should == :get
136
+ @service.request_url.should == 'http://test.dev/products.json'
137
+ @service.request_options.should == {}
138
+
139
+ # Get a product
140
+ fakeweb_response(:get, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
141
+ @service.product 1
142
+ @service.request_method.should == :get
143
+ @service.request_url.should == 'http://test.dev/products/1.json'
144
+ @service.request_options.should == {}
145
+
146
+ # Create
147
+ fakeweb_response(:post, 'http://test.dev/products.json', 201, {'product' => {'id' => 1, 'name' => 'Item1'}})
148
+ @service.create_product :name => 'Item1'
149
+ @service.request_method.should == :post
150
+ @service.request_url.should == 'http://test.dev/products.json'
151
+ @service.request_options.should == {:body => {'product' => {:name => 'Item1'}}}
152
+
153
+ # Edit
154
+ fakeweb_response(:put, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
155
+ @service.edit_product 1, :name => 'Item1'
156
+ @service.request_method.should == :put
157
+ @service.request_url.should == 'http://test.dev/products/1.json'
158
+ @service.request_options.should == {:body => {'product' => {:name => 'Item1'}}}
159
+
160
+ # Delete
161
+ fakeweb_response(:delete, 'http://test.dev/products/1.json', 200, {})
162
+ @service.delete_product 1
163
+ @service.request_method.should == :delete
164
+ @service.request_url.should == 'http://test.dev/products/1.json'
165
+ @service.request_options.should == {}
166
+ end
167
+
168
+ it "should be able to save resources" do
169
+ @service_class.rest :product, :products, '/products.json'
170
+ @service_class.method_defined?(:create_product).should be_true
171
+ @service_class.method_defined?(:edit_product).should be_true
172
+
173
+ @service = @service_class.new
174
+ @service.base_uri = 'http://test.dev'
175
+
176
+ # Create
177
+ fakeweb_response(:post, 'http://test.dev/products.json', 201, {'product' => {'id' => 1, 'name' => 'Item1'}})
178
+ @service.save :product, :name => 'Item1'
179
+ @service.request_method.should == :post
180
+ @service.request_url.should == 'http://test.dev/products.json'
181
+ @service.request_options.should == {:body => {'product' => {:name => 'Item1'}}}
182
+
183
+ # Edit
184
+ fakeweb_response(:put, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
185
+ @service.save :product, :id => 1, :name => 'Item1'
186
+ @service.request_method.should == :put
187
+ @service.request_url.should == 'http://test.dev/products/1.json'
188
+ @service.request_options.should == {:body => {'product' => {:id => 1, :name => 'Item1'}}}
189
+ end
190
+
191
+ it "should be able to change the return value using a class" do
192
+ @service_class.rest :product, :products, '/products.json', :return => DummyProduct
193
+ @service_class.method_defined?(:products).should be_true
194
+ @service_class.method_defined?(:product).should be_true
195
+
196
+ @service = @service_class.new
197
+ @service.base_uri = 'http://test.dev'
198
+
199
+ # Get products
200
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
201
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
202
+ products = @service.products
203
+ @service.request_method.should == :get
204
+ @service.request_url.should == 'http://test.dev/products.json'
205
+ @service.request_options.should == {}
206
+ products.is_a?(Array).should be_true
207
+ products[1].data['id'].should == 2
208
+
209
+ # Get a product
210
+ fakeweb_response(:get, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
211
+ product = @service.product 1
212
+ @service.request_method.should == :get
213
+ @service.request_url.should == 'http://test.dev/products/1.json'
214
+ @service.request_options.should == {}
215
+ product.is_a?(DummyProduct).should be_true
216
+ product.data['id'].should == 1
217
+ end
218
+
219
+ it "should be able to change the return value using a function" do
220
+ @service_class.send(:define_method, :handle_product) { |resource| resource['name'] }
221
+ @service_class.rest :product, :products, '/products.json', :return => :handle_product
222
+ @service_class.method_defined?(:products).should be_true
223
+ @service_class.method_defined?(:product).should be_true
224
+
225
+ @service = @service_class.new
226
+ @service.base_uri = 'http://test.dev'
227
+
228
+ # Get products
229
+ fakeweb_response(:get, 'http://test.dev/products.json', 200, [{'product' => {'id' => 1, 'name' => 'Item1'}},
230
+ {'product' => {'id' => 2, 'name' => 'Item2'}}])
231
+ products = @service.products
232
+ @service.request_method.should == :get
233
+ @service.request_url.should == 'http://test.dev/products.json'
234
+ @service.request_options.should == {}
235
+ products.is_a?(Array).should be_true
236
+ products.should == ['Item1', 'Item2']
237
+
238
+ # Get a product
239
+ fakeweb_response(:get, 'http://test.dev/products/1.json', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
240
+ product = @service.product 1
241
+ @service.request_method.should == :get
242
+ @service.request_url.should == 'http://test.dev/products/1.json'
243
+ @service.request_options.should == {}
244
+ product.is_a?(String).should be_true
245
+ product.should == 'Item1'
246
+ end
247
+
248
+ it "should be able to use a find_<resource> method to adjust the query option" do
249
+ @service_class.route :find_product, '/products/lookup.json', :resource => 'product'
250
+ @service_class.route :find_component, '/products/:product_id/components/lookup.json', :resource => 'component'
251
+ @service_class.method_defined?(:find_product).should be_true
252
+ @service_class.method_defined?(:find_component).should be_true
253
+
254
+ @service = @service_class.new
255
+ @service.base_uri = 'http://test.dev'
256
+
257
+ # Get a product
258
+ fakeweb_response(:get, 'http://test.dev/products/lookup.json?name=Item1', 200, {'product' => {'id' => 1, 'name' => 'Item1'}})
259
+ @service.find_product_by_name 'Item1'
260
+ @service.request_method.should == :get
261
+ @service.request_url.should == 'http://test.dev/products/lookup.json'
262
+ @service.request_options.should == {:query => {'name' => 'Item1'}}
263
+
264
+ # Get a component
265
+ fakeweb_response(:get, 'http://test.dev/products/1/components/lookup.json?name=C2', 200, {'component' => {'id' => 100, 'name' => 'C2'}})
266
+ @service.find_component_by_name 1, 'C2'
267
+ @service.request_method.should == :get
268
+ @service.request_url.should == 'http://test.dev/products/1/components/lookup.json'
269
+ @service.request_options.should == {:query => {'name' => 'C2'}}
270
+ end
271
+
272
+ it "should raise invalid response" do
273
+ @service_class.rest :product, :products, '/products.json'
274
+ @service_class.method_defined?(:product).should be_true
275
+
276
+ @service = @service_class.new
277
+ @service.base_uri = 'http://test.dev'
278
+
279
+ # Get a product
280
+ fakeweb_response(:get, 'http://test.dev/products/1.json', 400, {})
281
+ lambda{ @service.product 1 }.should raise_error(RESTHome::InvalidResponse)
282
+ @service.request_method.should == :get
283
+ @service.request_url.should == 'http://test.dev/products/1.json'
284
+ @service.request_options.should == {}
285
+ end
286
+
287
+ it "should raise error if find function is not found" do
288
+ @service_class.route :find_product, '/products/lookup.json', :resource => 'product'
289
+ @service_class.method_defined?(:find_product).should be_true
290
+
291
+ @service = @service_class.new
292
+ @service.base_uri = 'http://test.dev'
293
+
294
+ lambda{ @service.find_component_by_name 'Item1' }.should raise_error(RESTHome::MethodMissing)
295
+ lambda{ @service.save :product, {:name => 'Item1'} }.should raise_error(RESTHome::MethodMissing)
296
+ lambda{ @service.save :product, {:id => 1, :name => 'Item1'} }.should raise_error(RESTHome::MethodMissing)
297
+ end
298
+
299
+ it "should be able to use blocks on the response" do
300
+ @service_class.route :products, '/products.json', :resource => 'product'
301
+ @service_class.method_defined?(:products).should be_true
302
+
303
+ @service = @service_class.new
304
+ @service.base_uri = 'http://test.dev'
305
+
306
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
307
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
308
+ @names = @service.products do |product|
309
+ product['name']
310
+ end
311
+
312
+ @names.should == ['Item1', 'Item2']
313
+ @service.request_method.should == :get
314
+ @service.request_url.should == 'http://test.dev/products.json'
315
+ @service.request_options.should == {}
316
+ end
317
+
318
+ it "should be able to use blocks on the response" do
319
+ @service_class.route :products, '/products.json', :resource => 'product', :return => Proc.new { |res| res['name'] }
320
+ @service_class.method_defined?(:products).should be_true
321
+
322
+ @service = @service_class.new
323
+ @service.base_uri = 'http://test.dev'
324
+
325
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
326
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
327
+ @names = @service.products
328
+
329
+ @names.should == ['Item1', 'Item2']
330
+ @service.request_method.should == :get
331
+ @service.request_url.should == 'http://test.dev/products.json'
332
+ @service.request_options.should == {}
333
+ end
334
+
335
+ it "should be able to use blocks on the response" do
336
+ @service_class.route :products, '/products.json', :resource => 'product', :return => lambda { |res| res['name'] }
337
+ @service_class.method_defined?(:products).should be_true
338
+
339
+ @service = @service_class.new
340
+ @service.base_uri = 'http://test.dev'
341
+
342
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
343
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
344
+ @names = @service.products
345
+
346
+ @names.should == ['Item1', 'Item2']
347
+ @service.request_method.should == :get
348
+ @service.request_url.should == 'http://test.dev/products.json'
349
+ @service.request_options.should == {}
350
+ end
351
+
352
+ it "should be able to use blocks on the response" do
353
+ @service_class.route :products, '/products.json', :resource => 'product' do |res|
354
+ res['name']
355
+ end
356
+ @service_class.method_defined?(:products).should be_true
357
+
358
+ @service = @service_class.new
359
+ @service.base_uri = 'http://test.dev'
360
+
361
+ fakeweb_response(:get, 'http://test.dev/products.json', 200,
362
+ [{'product' => {'id' => 1, 'name' => 'Item1'}}, {'product' => {'id' => 2, 'name' => 'Item2'}}])
363
+ @names = @service.products
364
+
365
+ @names.should == ['Item1', 'Item2']
366
+ @service.request_method.should == :get
367
+ @service.request_url.should == 'http://test.dev/products.json'
368
+ @service.request_options.should == {}
369
+ end
370
+
371
+ it "should be able to parse cookies" do
372
+ @service = RESTHome.new
373
+ @service.save_cookies [nil, '', "test=1111; path=/; expires=Sat, 30-Nov-2002 00:00:00 GMT", "dev=1", "missing="]
374
+ @service.cookies['test'].should == '1111'
375
+ @service.cookies['dev'].should == '1'
376
+ @service.cookies['missing'].should be_nil
377
+ end
378
+
379
+ it "should be able to setup default query parameters" do
380
+ @service_class.route :find_songs, '/songs.json', :query => {'search' => 'all'}
381
+ @service_class.method_defined?(:find_songs).should be_true
382
+
383
+ @service = @service_class.new
384
+ @service.base_uri = 'http://test.dev'
385
+
386
+ fakeweb_response(:get, 'http://test.dev/songs.json?search=all', 200, [])
387
+ @names = @service.find_songs
388
+
389
+ @service.request_method.should == :get
390
+ @service.request_url.should == 'http://test.dev/songs.json'
391
+ @service.request_options.should == {:query => {'search' => 'all'}}
392
+
393
+ fakeweb_response(:get, 'http://test.dev/songs.json?search=test', 200, [])
394
+ @names = @service.find_songs_by_search 'test'
395
+
396
+ @service.request_method.should == :get
397
+ @service.request_url.should == 'http://test.dev/songs.json'
398
+ @service.request_options.should == {:query => {'search' => 'test'}}
399
+
400
+ fakeweb_response(:get, 'http://test.dev/songs.json?search=test2', 200, [])
401
+ @names = @service.find_songs :query => {'search' => 'test2'}
402
+
403
+ @service.request_method.should == :get
404
+ @service.request_url.should == 'http://test.dev/songs.json'
405
+ @service.request_options.should == {:query => {'search' => 'test2'}}
406
+ end
407
+
408
+ it "namespaces for convenience" do
409
+ @service_class.class_eval do
410
+ base_uri 'http://test.dev'
411
+
412
+ namespace '/users/1' do
413
+ route :find_songs, '/songs.json'
414
+ route :find_albums, '/albums.json'
415
+ namespace '/videos' do
416
+ route :find_videos, '/search.json'
417
+ end
418
+ route :find_tracks, '/tracks.json'
419
+ end
420
+ route :tracks, '/tracks.json'
421
+ end
422
+
423
+ @service_class.method_defined?(:find_songs).should be_true
424
+ @service_class.method_defined?(:find_albums).should be_true
425
+ @service_class.method_defined?(:tracks).should be_true
426
+
427
+ @service = @service_class.new
428
+
429
+ fakeweb_response(:get, 'http://test.dev/users/1/songs.json', 200, [])
430
+ @service.find_songs
431
+
432
+ @service.request_method.should == :get
433
+ @service.request_url.should == 'http://test.dev/users/1/songs.json'
434
+
435
+ fakeweb_response(:get, 'http://test.dev/users/1/albums.json', 200, [])
436
+ @service.find_albums
437
+
438
+ @service.request_method.should == :get
439
+ @service.request_url.should == 'http://test.dev/users/1/albums.json'
440
+
441
+ fakeweb_response(:get, 'http://test.dev/users/1/tracks.json', 200, [])
442
+ @service.find_tracks
443
+
444
+ @service.request_method.should == :get
445
+ @service.request_url.should == 'http://test.dev/users/1/tracks.json'
446
+
447
+ fakeweb_response(:get, 'http://test.dev/users/1/videos/search.json', 200, [])
448
+ @service.find_videos
449
+
450
+ @service.request_method.should == :get
451
+ @service.request_url.should == 'http://test.dev/users/1/videos/search.json'
452
+
453
+ fakeweb_response(:get, 'http://test.dev/tracks.json', 200, [])
454
+ @service.tracks
455
+
456
+ @service.request_method.should == :get
457
+ @service.request_url.should == 'http://test.dev/tracks.json'
458
+ end
459
+
460
+ it "should support query arguments" do
461
+ @service_class.class_eval do
462
+ base_uri 'http://test.dev'
463
+ route :tracks, '/:version/', :query => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}
464
+ end
465
+ @service = @service_class.new
466
+
467
+ fakeweb_response(:get, %r|http://test.dev/2.0/|, 200, [])
468
+ @service.tracks '2.0', 'cher', 'believe'
469
+
470
+ @service.request_method.should == :get
471
+ @service.request_url.should == 'http://test.dev/2.0/'
472
+ @service.request_options[:query]['method'].should == 'track.getinfo'
473
+ @service.request_options[:query]['artist'].should == 'cher'
474
+ @service.request_options[:query]['track'].should == 'believe'
475
+ end
476
+
477
+ it "should support body arguments" do
478
+ @service_class.class_eval do
479
+ base_uri 'http://test.dev'
480
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :method => :post, :no_body => true
481
+ end
482
+ @service = @service_class.new
483
+
484
+ fakeweb_response(:post, %r|http://test.dev/2.0/|, 200, [])
485
+ @service.tracks '2.0', 'cher', 'believe'
486
+
487
+ @service.request_method.should == :post
488
+ @service.request_url.should == 'http://test.dev/2.0/'
489
+ @service.request_options[:body]['method'].should == 'track.getinfo'
490
+ @service.request_options[:body]['artist'].should == 'cher'
491
+ @service.request_options[:body]['track'].should == 'believe'
492
+ end
493
+
494
+ it "should support body arguments" do
495
+ @service_class.class_eval do
496
+ base_uri 'http://test.dev'
497
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :method => :post
498
+ end
499
+ @service = @service_class.new
500
+
501
+ fakeweb_response(:post, %r|http://test.dev/2.0/|, 200, [])
502
+ @service.tracks '2.0', 'cher', 'believe', 'method' => 'track.getinfo2'
503
+
504
+ @service.request_method.should == :post
505
+ @service.request_url.should == 'http://test.dev/2.0/'
506
+ @service.request_options[:body]['method'].should == 'track.getinfo2'
507
+ @service.request_options[:body]['artist'].should == 'cher'
508
+ @service.request_options[:body]['track'].should == 'believe'
509
+ end
510
+
511
+ it "should support body and query arguments" do
512
+ @service_class.class_eval do
513
+ base_uri 'http://test.dev'
514
+ route :tracks, '/:version/', :body => {'method' => 'track.getinfo', 'artist' => :arg1, 'track' => :arg2}, :query => {'auth' => 'xx', 'Sign' => :arg1}, :method => :post
515
+ end
516
+ @service = @service_class.new
517
+
518
+ fakeweb_response(:post, 'http://test.dev/2.0/?auth=xx&Sign=secret', 200, [])
519
+ @service.tracks '2.0', 'cher', 'believe', 'secret', 'method' => 'track.getinfo2'
520
+
521
+ @service.request_method.should == :post
522
+ @service.request_url.should == 'http://test.dev/2.0/'
523
+ @service.request_options[:body]['method'].should == 'track.getinfo2'
524
+ @service.request_options[:body]['artist'].should == 'cher'
525
+ @service.request_options[:body]['track'].should == 'believe'
526
+ end
527
+
528
+ it "should set the expected status based on the method" do
529
+ @service_class.class_eval do
530
+ base_uri 'http://test.dev'
531
+ route :test, '/test', :method => :post, :no_body => true
532
+ end
533
+ @service = @service_class.new
534
+
535
+ fakeweb_response(:post, 'http://test.dev/test', 200, [])
536
+ @service.test
537
+
538
+ fakeweb_response(:post, 'http://test.dev/test', 201, [])
539
+ @service.test
540
+
541
+ fakeweb_response(:post, 'http://test.dev/test', 202, [])
542
+ expect {
543
+ @service.test
544
+ }.to raise_error { RESTHome::InvalidResponse }
545
+ end
546
+ end
data/tasks/spec.rake ADDED
@@ -0,0 +1,9 @@
1
+ begin
2
+ require 'spec/rake/spectask'
3
+ Spec::Rake::SpecTask.new(:spec) do |spec|
4
+ spec.libs << 'lib' << 'spec'
5
+ spec.spec_files = FileList['spec/**/*_spec.rb']
6
+ end
7
+ rescue LoadError => e
8
+ puts "unable to run specs from rake. gem install rspec"
9
+ end