pauldix-typhoeus 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,399 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Typhoeus do
4
+ before(:each) do
5
+ @klass = Class.new do
6
+ include Typhoeus
7
+ end
8
+ end
9
+
10
+ before(:all) do
11
+ @pid = start_method_server(3001)
12
+ end
13
+
14
+ after(:all) do
15
+ stop_method_server(@pid)
16
+ end
17
+
18
+ describe "get" do
19
+ it "should add a get method" do
20
+ easy = @klass.get("http://localhost:3001/posts.xml")
21
+ easy.code.should == 200
22
+ easy.body.should include("REQUEST_METHOD=GET")
23
+ easy.body.should include("REQUEST_URI=/posts.xml")
24
+ end
25
+
26
+ it "should take passed in params and add them to the query string" do
27
+ easy = @klass.get("http://localhost:3001", {:params => {:foo => :bar}})
28
+ easy.body.should include("QUERY_STRING=foo=bar")
29
+ end
30
+ end # get
31
+
32
+ describe "post" do
33
+ it "should add a post method" do
34
+ easy = @klass.post("http://localhost:3001/posts.xml", {:params => {:post => {:author => "paul", :title => "a title", :body => "a body"}}})
35
+ easy.code.should == 200
36
+ easy.body.should include("post%5Bbody%5D=a+body")
37
+ easy.body.should include("post%5Bauthor%5D=paul")
38
+ easy.body.should include("post%5Btitle%5D=a+title")
39
+ easy.body.should include("REQUEST_METHOD=POST")
40
+ end
41
+
42
+ it "should add a body" do
43
+ easy = @klass.post("http://localhost:3001/posts.xml", {:body => "this is a request body"})
44
+ easy.code.should == 200
45
+ easy.body.should include("this is a request body")
46
+ easy.body.should include("REQUEST_METHOD=POST")
47
+ end
48
+ end # post
49
+
50
+ it "should add a put method" do
51
+ easy = @klass.put("http://localhost:3001/posts/3.xml")
52
+ easy.code.should == 200
53
+ easy.body.should include("REQUEST_METHOD=PUT")
54
+ end
55
+
56
+ it "should add a delete method" do
57
+ easy = @klass.delete("http://localhost:3001/posts/3.xml")
58
+ easy.code.should == 200
59
+ easy.body.should include("REQUEST_METHOD=DELETE")
60
+ end
61
+
62
+ describe "#define_remote_method" do
63
+ before(:each) do
64
+ @klass = Class.new do
65
+ include Typhoeus
66
+ end
67
+ end
68
+
69
+ describe "defined methods" do
70
+ before(:each) do
71
+ @klass.instance_eval do
72
+ define_remote_method :do_stuff
73
+ end
74
+ end
75
+
76
+ it "should take a method name as the first argument and define that as a class method" do
77
+ @klass.should respond_to(:do_stuff)
78
+ end
79
+
80
+ it "should optionally take arguments" do
81
+ @klass.should_receive(:get)
82
+ @klass.do_stuff
83
+ end
84
+
85
+ it "should take arguments" do
86
+ @klass.should_receive(:get).with("", {:params=>{:foo=>"bar"}, :body=>"whatever"})
87
+ @klass.do_stuff(:params => {:foo => "bar"}, :body => "whatever")
88
+ end
89
+ end
90
+
91
+ describe "base_uri" do
92
+ it "should take a :uri as an argument" do
93
+ @klass.instance_eval do
94
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net"
95
+ end
96
+
97
+ @klass.should_receive(:get).with("http://pauldix.net", {})
98
+ @klass.do_stuff
99
+ end
100
+
101
+ it "should use default_base_uri if no base_uri provided" do
102
+ @klass.instance_eval do
103
+ remote_defaults :base_uri => "http://kgb.com"
104
+ define_remote_method :do_stuff
105
+ end
106
+
107
+ @klass.should_receive(:get).with("http://kgb.com", {})
108
+ @klass.do_stuff
109
+ end
110
+
111
+ it "should override default_base_uri if uri argument is provided" do
112
+ @klass.instance_eval do
113
+ remote_defaults :base_uri => "http://kgb.com"
114
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net"
115
+ end
116
+
117
+ @klass.should_receive(:get).with("http://pauldix.net", {})
118
+ @klass.do_stuff
119
+ end
120
+ end
121
+
122
+ describe "path" do
123
+ it "should take :path as an argument" do
124
+ @klass.instance_eval do
125
+ define_remote_method :do_stuff, :base_uri => "http://kgb.com", :path => "/default.html"
126
+ end
127
+
128
+ @klass.should_receive(:get).with("http://kgb.com/default.html", {})
129
+ @klass.do_stuff
130
+ end
131
+
132
+ it "should use deafult_path if no path provided" do
133
+ @klass.instance_eval do
134
+ remote_defaults :path => "/index.html"
135
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net"
136
+ end
137
+
138
+ @klass.should_receive(:get).with("http://pauldix.net/index.html", {})
139
+ @klass.do_stuff
140
+ end
141
+
142
+ it "should orverride default_path if path argument is provided" do
143
+ @klass.instance_eval do
144
+ remote_defaults :path => "/index.html"
145
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net", :path => "/foo.html"
146
+ end
147
+
148
+ @klass.should_receive(:get).with("http://pauldix.net/foo.html", {})
149
+ @klass.do_stuff
150
+ end
151
+
152
+ it "should map symbols in path to arguments for the remote method" do
153
+ @klass.instance_eval do
154
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net", :path => "/posts/:post_id/comments/:comment_id"
155
+ end
156
+
157
+ @klass.should_receive(:get).with("http://pauldix.net/posts/foo/comments/bar", {})
158
+ @klass.do_stuff(:post_id => "foo", :comment_id => "bar")
159
+ end
160
+
161
+ it "should use a path passed into the remote method call" do
162
+ @klass.instance_eval do
163
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net"
164
+ end
165
+
166
+ @klass.should_receive(:get).with("http://pauldix.net/whatev?asdf=foo", {})
167
+ @klass.do_stuff(:path => "/whatev?asdf=foo")
168
+ end
169
+ end
170
+
171
+ describe "method" do
172
+ it "should take :method as an argument" do
173
+ @klass.instance_eval do
174
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net", :method => :put
175
+ end
176
+
177
+ @klass.should_receive(:put).with("http://pauldix.net", {})
178
+ @klass.do_stuff
179
+ end
180
+
181
+ it "should use :get if no method or default_method exists" do
182
+ @klass.instance_eval do
183
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net"
184
+ end
185
+
186
+ @klass.should_receive(:get).with("http://pauldix.net", {})
187
+ @klass.do_stuff
188
+ end
189
+
190
+ it "should use default_method if no method provided" do
191
+ @klass.instance_eval do
192
+ remote_defaults :method => :delete
193
+ define_remote_method :do_stuff, :base_uri => "http://kgb.com"
194
+ end
195
+
196
+ @klass.should_receive(:delete).with("http://kgb.com", {})
197
+ @klass.do_stuff
198
+ end
199
+
200
+ it "should override deafult_method if method argument is provided" do
201
+ @klass.instance_eval do
202
+ remote_defaults :method => :put
203
+ define_remote_method :do_stuff, :base_uri => "http://pauldix.net", :method => :post
204
+ end
205
+
206
+ @klass.should_receive(:post).with("http://pauldix.net", {})
207
+ @klass.do_stuff
208
+ end
209
+ end
210
+
211
+ describe "on_success" do
212
+ it "should take :on_success as an argument" do
213
+ @klass.instance_eval do
214
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :on_success => lambda {|e| e.code.should == 200; :foo}
215
+ end
216
+
217
+ @klass.do_stuff.should == :foo
218
+ end
219
+
220
+ it "should use default_on_success if no on_success provided" do
221
+ @klass.instance_eval do
222
+ remote_defaults :on_success => lambda {|e| e.code.should == 200; :foo}
223
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001"
224
+ end
225
+
226
+ @klass.do_stuff.should == :foo
227
+ end
228
+
229
+ it "should override default_on_success if on_success is provided" do
230
+ @klass.instance_eval do
231
+ remote_defaults :on_success => lambda {|e| :foo}
232
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :on_success => lambda {|e| e.code.should == 200; :bar}
233
+ end
234
+
235
+ @klass.do_stuff.should == :bar
236
+ end
237
+ end
238
+
239
+ describe "on_failure" do
240
+ it "should take :on_failure as an argument" do
241
+ @klass.instance_eval do
242
+ define_remote_method :do_stuff, :base_uri => "http://localhost:9999", :on_failure => lambda {|e| e.code.should == 0; :foo}
243
+ end
244
+
245
+ @klass.do_stuff.should == :foo
246
+ end
247
+
248
+ it "should use default_on_failure if no on_success provided" do
249
+ @klass.instance_eval do
250
+ remote_defaults :on_failure => lambda {|e| e.code.should == 0; :bar}
251
+ define_remote_method :do_stuff, :base_uri => "http://localhost:9999"
252
+ end
253
+
254
+ @klass.do_stuff.should == :bar
255
+ end
256
+
257
+ it "should override default_on_failure if no method is provided" do
258
+ @klass.instance_eval do
259
+ remote_defaults :on_failure => lambda {|e| :foo}
260
+ define_remote_method :do_stuff, :base_uri => "http://localhost:9999", :on_failure => lambda {|e| e.code.should == 0; :bar}
261
+ end
262
+
263
+ @klass.do_stuff.should == :bar
264
+ end
265
+ end
266
+
267
+ describe "params" do
268
+ it "should take :params as an argument" do
269
+ @klass.instance_eval do
270
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :params => {:foo => :bar}
271
+ end
272
+
273
+ @klass.do_stuff.body.should include("QUERY_STRING=foo=bar")
274
+ end
275
+
276
+ it "should add :params from remote method definition with params passed in when called" do
277
+ @klass.instance_eval do
278
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :params => {:foo => :bar}
279
+ end
280
+
281
+ @klass.do_stuff(:params => {:asdf => :jkl}).body.should include("QUERY_STRING=foo=bar&asdf=jkl")
282
+ end
283
+ end
284
+
285
+ describe "memoize_responses" do
286
+ it "should only make one call to the http method and the on_success handler if :memoize_responses => true" do
287
+ success_mock = mock("success")
288
+ success_mock.should_receive(:call).exactly(2).times
289
+
290
+ @klass.instance_eval do
291
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :path => "/:file", :on_success => lambda {|e| success_mock.call; :foo}
292
+ end
293
+
294
+ first_return_val = @klass.do_stuff(:file => "user.html")
295
+ second_return_val = @klass.do_stuff(:file => "post.html")
296
+ third_return_val = @klass.do_stuff(:file => "user.html")
297
+
298
+ first_return_val.should == :foo
299
+ second_return_val.should == :foo
300
+ third_return_val.should == :foo
301
+ end
302
+
303
+ it "should clear memoized responses after a full run" do
304
+ success_mock = mock("success")
305
+ success_mock.should_receive(:call).exactly(2).times
306
+
307
+ @klass.instance_eval do
308
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :path => "/:file", :on_success => lambda {|e| success_mock.call; :foo}
309
+ end
310
+
311
+ @klass.do_stuff(:file => "user.html").should == :foo
312
+ @klass.do_stuff(:file => "user.html").should == :foo
313
+ end
314
+ end
315
+
316
+ describe "cache_response" do
317
+ before(:each) do
318
+ @cache = Class.new do
319
+ def self.get(key)
320
+ @cache ||= {}
321
+ @cache[key]
322
+ end
323
+
324
+ def self.set(key, value, timeout = 0)
325
+ @cache ||= {}
326
+ @cache[key] = value
327
+ end
328
+ end
329
+
330
+ @klass.instance_eval do
331
+ define_remote_method :do_stuff, :base_uri => "http://localhost:3001", :path => "/:file", :cache_responses => true, :on_success => lambda {|e| :foo}
332
+ end
333
+ end
334
+
335
+ it "should pull from the cache if :cache_response => true" do
336
+ @cache.should_receive(:get).and_return(:foo)
337
+ @klass.cache = @cache
338
+ Typhoeus.should_receive(:perform_easy_requests).exactly(0).times
339
+ @klass.do_stuff.should == :foo
340
+ end
341
+
342
+ it "should only hit the cache once for the same value" do
343
+ @cache.should_receive(:get).exactly(1).times.and_return(:foo)
344
+ @klass.cache = @cache
345
+ Typhoeus.should_receive(:perform_easy_requests).exactly(0).times
346
+
347
+
348
+ first = @klass.do_stuff
349
+ second = @klass.do_stuff
350
+
351
+ first.should == :foo
352
+ second.should == :foo
353
+ end
354
+
355
+ it "should only hit the cache once if there is a cache miss (don't check again and again inside the same block)." do
356
+ @cache.should_receive(:get).exactly(1).times.and_return(nil)
357
+ @cache.should_receive(:set).exactly(1).times
358
+ @klass.cache = @cache
359
+
360
+ first = @klass.do_stuff
361
+ second = @klass.do_stuff
362
+
363
+ first.should == :foo
364
+ second.should == :foo
365
+ end
366
+
367
+ it "should store an object in the cache with a set ttl"
368
+ it "should take a hash with get and set method pointers to enable custom caching behavior"
369
+ end
370
+ end # define_remote_method
371
+
372
+ describe "cache_server" do
373
+ it "should store a cache_server" do
374
+ @klass.cache = :foo
375
+ end
376
+ end
377
+
378
+ describe "get_memcache_resposne_key" do
379
+ it "should return a key that is an and of the method name, args, and options" do
380
+ @klass.get_memcache_response_key(:do_stuff, ["foo"]).should == "20630a9d4864c41cbbcb8bd8ac91ab4767e72107b93329aa2e6f5629037392f3"
381
+ end
382
+ end
383
+
384
+ # describe "multiple with post" do
385
+ # require 'rubygems'
386
+ # require 'json'
387
+ # it "shoudl do stuff" do
388
+ # @klass.instance_eval do
389
+ # define_remote_method :post_stuff, :path => "/entries/metas/:meta_id/ids", :base_uri => "http://localhost:4567", :method => :post
390
+ # define_remote_method :get_stuff, :base_uri => "http://localhost:4567"
391
+ # end
392
+ #
393
+ # Typhoeus.service_access do
394
+ # @klass.post_stuff("paul-tv", :body => ["foo", "bar"].to_json) {|e| }
395
+ # @klass.get_stuff {|e| }
396
+ # end
397
+ # end
398
+ # end
399
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Typhoeus::Response do
4
+ describe "initialize" do
5
+ it "should store response_code" do
6
+ Typhoeus::Response.new(200, nil, nil, nil).code.should == 200
7
+ end
8
+
9
+ it "should store response_headers" do
10
+ Typhoeus::Response.new(nil, "a header!", nil, nil).headers.should == "a header!"
11
+ end
12
+
13
+ it "should store response_body" do
14
+ Typhoeus::Response.new(nil, nil, "a body!", nil).body.should == "a body!"
15
+ end
16
+
17
+ it "should store request_time" do
18
+ Typhoeus::Response.new(nil, nil, nil, 1.23).time.should == 1.23
19
+ end
20
+ end
21
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pauldix-typhoeus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Paul Dix
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-03-12 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: paul@pauldix.net
18
+ executables: []
19
+
20
+ extensions:
21
+ - ext/typhoeus/extconf.rb
22
+ extra_rdoc_files: []
23
+
24
+ files:
25
+ - ext/typhoeus/extconf.rb
26
+ - ext/typhoeus/typhoeus_easy.h
27
+ - ext/typhoeus/typhoeus_easy.c
28
+ - ext/typhoeus/typhoeus_multi.h
29
+ - ext/typhoeus/typhoeus_multi.c
30
+ - ext/typhoeus/Makefile
31
+ - ext/typhoeus/native.h
32
+ - ext/typhoeus/native.c
33
+ - lib/typhoeus.rb
34
+ - lib/typhoeus/easy.rb
35
+ - lib/typhoeus/multi.rb
36
+ - lib/typhoeus/remote.rb
37
+ - lib/typhoeus/remote_proxy_object.rb
38
+ - lib/typhoeus/filter.rb
39
+ - lib/typhoeus/remote_method.rb
40
+ - lib/typhoeus/response.rb
41
+ - spec/spec.opts
42
+ - spec/spec_helper.rb
43
+ - spec/typhoeus/easy_spec.rb
44
+ - spec/typhoeus/multi_spec.rb
45
+ - spec/typhoeus/remote_spec.rb
46
+ - spec/typhoeus/remote_proxy_object_spec.rb
47
+ - spec/typhoeus/filter_spec.rb
48
+ - spec/typhoeus/remote_method_spec.rb
49
+ - spec/typhoeus/response_spec.rb
50
+ - spec/servers/delay_fixture_server.rb
51
+ - spec/servers/method_server.rb
52
+ has_rdoc: true
53
+ homepage: http://github.com/pauldix/typhoeus
54
+ post_install_message:
55
+ rdoc_options: []
56
+
57
+ require_paths:
58
+ - lib
59
+ - ext
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: "0"
71
+ version:
72
+ requirements: []
73
+
74
+ rubyforge_project:
75
+ rubygems_version: 1.2.0
76
+ signing_key:
77
+ specification_version: 2
78
+ summary: A library for interacting with web services (and building SOAs) at blinding speed.
79
+ test_files: []
80
+