gravis-typhoeus 0.1.29
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.
- data/.gitignore +2 -0
- data/README.textile +301 -0
- data/Rakefile +39 -0
- data/VERSION +1 -0
- data/benchmarks/profile.rb +25 -0
- data/benchmarks/vs_nethttp.rb +35 -0
- data/examples/twitter.rb +21 -0
- data/ext/typhoeus/.gitignore +5 -0
- data/ext/typhoeus/Makefile +157 -0
- data/ext/typhoeus/extconf.rb +65 -0
- data/ext/typhoeus/native.c +11 -0
- data/ext/typhoeus/native.h +21 -0
- data/ext/typhoeus/typhoeus_easy.c +207 -0
- data/ext/typhoeus/typhoeus_easy.h +19 -0
- data/ext/typhoeus/typhoeus_multi.c +225 -0
- data/ext/typhoeus/typhoeus_multi.h +16 -0
- data/lib/typhoeus/.gitignore +1 -0
- data/lib/typhoeus/easy.rb +322 -0
- data/lib/typhoeus/filter.rb +28 -0
- data/lib/typhoeus/hydra.rb +227 -0
- data/lib/typhoeus/multi.rb +35 -0
- data/lib/typhoeus/remote.rb +306 -0
- data/lib/typhoeus/remote_method.rb +108 -0
- data/lib/typhoeus/remote_proxy_object.rb +48 -0
- data/lib/typhoeus/request.rb +124 -0
- data/lib/typhoeus/response.rb +49 -0
- data/lib/typhoeus/service.rb +20 -0
- data/lib/typhoeus.rb +55 -0
- data/profilers/valgrind.rb +24 -0
- data/spec/fixtures/result_set.xml +60 -0
- data/spec/servers/app.rb +73 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +11 -0
- data/spec/typhoeus/easy_spec.rb +228 -0
- data/spec/typhoeus/filter_spec.rb +35 -0
- data/spec/typhoeus/hydra_spec.rb +311 -0
- data/spec/typhoeus/multi_spec.rb +74 -0
- data/spec/typhoeus/remote_method_spec.rb +141 -0
- data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
- data/spec/typhoeus/remote_spec.rb +695 -0
- data/spec/typhoeus/request_spec.rb +169 -0
- data/spec/typhoeus/response_spec.rb +63 -0
- data/typhoeus.gemspec +112 -0
- metadata +203 -0
@@ -0,0 +1,311 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
# some of these tests assume that you have some local services running.
|
4
|
+
# ruby spec/servers/app.rb -p 3000
|
5
|
+
# ruby spec/servers/app.rb -p 3001
|
6
|
+
# ruby spec/servers/app.rb -p 3002
|
7
|
+
describe Typhoeus::Hydra do
|
8
|
+
before(:all) do
|
9
|
+
cache_class = Class.new do
|
10
|
+
def initialize
|
11
|
+
@cache = {}
|
12
|
+
end
|
13
|
+
def get(key)
|
14
|
+
@cache[key]
|
15
|
+
end
|
16
|
+
def set(key, object, timeout = 0)
|
17
|
+
@cache[key] = object
|
18
|
+
end
|
19
|
+
end
|
20
|
+
@cache = cache_class.new
|
21
|
+
end
|
22
|
+
|
23
|
+
it "has a singleton" do
|
24
|
+
Typhoeus::Hydra.hydra.should be_a Typhoeus::Hydra
|
25
|
+
end
|
26
|
+
|
27
|
+
it "has a setter for the singleton" do
|
28
|
+
Typhoeus::Hydra.hydra = :foo
|
29
|
+
Typhoeus::Hydra.hydra.should == :foo
|
30
|
+
Typhoeus::Hydra.hydra = Typhoeus::Hydra.new
|
31
|
+
end
|
32
|
+
|
33
|
+
context "#stub" do
|
34
|
+
before do
|
35
|
+
@hydra = Typhoeus::Hydra.new
|
36
|
+
@on_complete_handler_called = nil
|
37
|
+
@request = Typhoeus::Request.new("http://localhost:3000/foo")
|
38
|
+
@request.on_complete do |response|
|
39
|
+
@on_complete_handler_called = true
|
40
|
+
response.code.should == 404
|
41
|
+
response.headers.should == "whatever"
|
42
|
+
end
|
43
|
+
@response = Typhoeus::Response.new(:code => 404, :headers => "whatever", :body => "not found", :time => 0.1)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "stubs requests to a specific URI" do
|
47
|
+
@hydra.stub(:get, "http://localhost:3000/foo").and_return(@response)
|
48
|
+
@hydra.queue(@request)
|
49
|
+
@hydra.run
|
50
|
+
@on_complete_handler_called.should be_true
|
51
|
+
@response.request.should == @request
|
52
|
+
end
|
53
|
+
|
54
|
+
it "stubs requests to URIs matching a pattern" do
|
55
|
+
@hydra.stub(:get, /foo/).and_return(@response)
|
56
|
+
@hydra.queue(@request)
|
57
|
+
@hydra.run
|
58
|
+
@on_complete_handler_called.should be_true
|
59
|
+
@response.request.should == @request
|
60
|
+
end
|
61
|
+
|
62
|
+
it "can clear stubs" do
|
63
|
+
@hydra.clear_stubs
|
64
|
+
end
|
65
|
+
|
66
|
+
it "clears out previously queued requests once they are called" do
|
67
|
+
@hydra.stub(:get, "asdf").and_return(@response)
|
68
|
+
|
69
|
+
call_count = 0
|
70
|
+
request = Typhoeus::Request.new("asdf")
|
71
|
+
request.on_complete do |response|
|
72
|
+
call_count += 1
|
73
|
+
end
|
74
|
+
@hydra.queue(request)
|
75
|
+
@hydra.run
|
76
|
+
call_count.should == 1
|
77
|
+
@hydra.run
|
78
|
+
call_count.should == 1
|
79
|
+
end
|
80
|
+
|
81
|
+
it "calls stubs for requests that are queued up in the on_complete of a first stub" do
|
82
|
+
@hydra.stub(:get, "asdf").and_return(@response)
|
83
|
+
@hydra.stub(:get, "bar").and_return(@response)
|
84
|
+
|
85
|
+
second_handler_called = false
|
86
|
+
request = Typhoeus::Request.new("asdf")
|
87
|
+
request.on_complete do |response|
|
88
|
+
r = Typhoeus::Request.new("bar")
|
89
|
+
r.on_complete do |res|
|
90
|
+
second_handler_called = true
|
91
|
+
end
|
92
|
+
@hydra.queue(r)
|
93
|
+
end
|
94
|
+
@hydra.queue(request)
|
95
|
+
@hydra.run
|
96
|
+
|
97
|
+
second_handler_called.should be_true
|
98
|
+
end
|
99
|
+
|
100
|
+
it "matches a stub only when the HTTP method also matches"
|
101
|
+
end
|
102
|
+
|
103
|
+
it "queues a request" do
|
104
|
+
hydra = Typhoeus::Hydra.new
|
105
|
+
hydra.queue Typhoeus::Request.new("http://localhost:3000")
|
106
|
+
end
|
107
|
+
|
108
|
+
it "runs a batch of requests" do
|
109
|
+
hydra = Typhoeus::Hydra.new
|
110
|
+
first = Typhoeus::Request.new("http://localhost:3000/first")
|
111
|
+
second = Typhoeus::Request.new("http://localhost:3001/second")
|
112
|
+
hydra.queue first
|
113
|
+
hydra.queue second
|
114
|
+
hydra.run
|
115
|
+
first.response.body.should include("first")
|
116
|
+
second.response.body.should include("second")
|
117
|
+
end
|
118
|
+
|
119
|
+
it "has a cache_setter proc" do
|
120
|
+
hydra = Typhoeus::Hydra.new
|
121
|
+
hydra.cache_setter do |request|
|
122
|
+
# @cache.set(request.cache_key, request.response, request.cache_timeout)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
it "has a cache_getter" do
|
127
|
+
hydra = Typhoeus::Hydra.new
|
128
|
+
hydra.cache_getter do |request|
|
129
|
+
# @cache.get(request.cache_key) rescue nil
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
it "memoizes GET reqeusts" do
|
134
|
+
hydra = Typhoeus::Hydra.new
|
135
|
+
first = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
|
136
|
+
second = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
|
137
|
+
hydra.queue first
|
138
|
+
hydra.queue second
|
139
|
+
start_time = Time.now
|
140
|
+
hydra.run
|
141
|
+
first.response.body.should include("foo")
|
142
|
+
first.handled_response.body.should include("foo")
|
143
|
+
first.response.should == second.response
|
144
|
+
first.handled_response.should == second.handled_response
|
145
|
+
(Time.now - start_time).should < 1.2 # if it had run twice it would be ~ 2 seconds
|
146
|
+
end
|
147
|
+
|
148
|
+
it "can turn off memoization for GET requests" do
|
149
|
+
hydra = Typhoeus::Hydra.new
|
150
|
+
hydra.disable_memoization
|
151
|
+
first = Typhoeus::Request.new("http://localhost:3000/foo")
|
152
|
+
second = Typhoeus::Request.new("http://localhost:3000/foo")
|
153
|
+
hydra.queue first
|
154
|
+
hydra.queue second
|
155
|
+
hydra.run
|
156
|
+
first.response.body.should include("foo")
|
157
|
+
first.response.object_id.should_not == second.response.object_id
|
158
|
+
end
|
159
|
+
|
160
|
+
it "pulls GETs from cache" do
|
161
|
+
hydra = Typhoeus::Hydra.new
|
162
|
+
start_time = Time.now
|
163
|
+
hydra.cache_getter do |request|
|
164
|
+
@cache.get(request.cache_key) rescue nil
|
165
|
+
end
|
166
|
+
hydra.cache_setter do |request|
|
167
|
+
@cache.set(request.cache_key, request.response, request.cache_timeout)
|
168
|
+
end
|
169
|
+
|
170
|
+
first = Typhoeus::Request.new("http://localhost:3000/foo", :params => {:delay => 1})
|
171
|
+
@cache.set(first.cache_key, :foo, 60)
|
172
|
+
hydra.queue first
|
173
|
+
hydra.run
|
174
|
+
(Time.now - start_time).should < 0.1
|
175
|
+
first.response.should == :foo
|
176
|
+
end
|
177
|
+
|
178
|
+
it "sets GET responses to cache when the request has a cache_timeout value" do
|
179
|
+
hydra = Typhoeus::Hydra.new
|
180
|
+
hydra.cache_getter do |request|
|
181
|
+
@cache.get(request.cache_key) rescue nil
|
182
|
+
end
|
183
|
+
hydra.cache_setter do |request|
|
184
|
+
@cache.set(request.cache_key, request.response, request.cache_timeout)
|
185
|
+
end
|
186
|
+
|
187
|
+
first = Typhoeus::Request.new("http://localhost:3000/first", :cache_timeout => 0)
|
188
|
+
second = Typhoeus::Request.new("http://localhost:3000/second")
|
189
|
+
hydra.queue first
|
190
|
+
hydra.queue second
|
191
|
+
hydra.run
|
192
|
+
first.response.body.should include("first")
|
193
|
+
@cache.get(first.cache_key).should == first.response
|
194
|
+
@cache.get(second.cache_key).should be_nil
|
195
|
+
end
|
196
|
+
|
197
|
+
it "has a global on_complete" do
|
198
|
+
foo = nil
|
199
|
+
hydra = Typhoeus::Hydra.new
|
200
|
+
hydra.on_complete do |response|
|
201
|
+
foo = :called
|
202
|
+
end
|
203
|
+
|
204
|
+
first = Typhoeus::Request.new("http://localhost:3000/first")
|
205
|
+
hydra.queue first
|
206
|
+
hydra.run
|
207
|
+
first.response.body.should include("first")
|
208
|
+
foo.should == :called
|
209
|
+
end
|
210
|
+
|
211
|
+
it "has a global on_complete setter" do
|
212
|
+
foo = nil
|
213
|
+
hydra = Typhoeus::Hydra.new
|
214
|
+
proc = Proc.new {|response| foo = :called}
|
215
|
+
hydra.on_complete = proc
|
216
|
+
|
217
|
+
first = Typhoeus::Request.new("http://localhost:3000/first")
|
218
|
+
hydra.queue first
|
219
|
+
hydra.run
|
220
|
+
first.response.body.should include("first")
|
221
|
+
foo.should == :called
|
222
|
+
end
|
223
|
+
|
224
|
+
it "should reuse connections from the pool for a host"
|
225
|
+
|
226
|
+
it "should queue up requests while others are running" do
|
227
|
+
hydra = Typhoeus::Hydra.new
|
228
|
+
|
229
|
+
start_time = Time.now
|
230
|
+
@responses = []
|
231
|
+
|
232
|
+
request = Typhoeus::Request.new("http://localhost:3000/first", :params => {:delay => 1})
|
233
|
+
request.on_complete do |response|
|
234
|
+
@responses << response
|
235
|
+
response.body.should include("first")
|
236
|
+
end
|
237
|
+
|
238
|
+
request.after_complete do |object|
|
239
|
+
second_request = Typhoeus::Request.new("http://localhost:3001/second", :params => {:delay => 2})
|
240
|
+
second_request.on_complete do |response|
|
241
|
+
@responses << response
|
242
|
+
response.body.should include("second")
|
243
|
+
end
|
244
|
+
hydra.queue second_request
|
245
|
+
end
|
246
|
+
hydra.queue request
|
247
|
+
|
248
|
+
third_request = Typhoeus::Request.new("http://localhost:3002/third", :params => {:delay => 3})
|
249
|
+
third_request.on_complete do |response|
|
250
|
+
@responses << response
|
251
|
+
response.body.should include("third")
|
252
|
+
end
|
253
|
+
hydra.queue third_request
|
254
|
+
|
255
|
+
hydra.run
|
256
|
+
@responses.size.should == 3
|
257
|
+
(Time.now - start_time).should < 3.3
|
258
|
+
end
|
259
|
+
|
260
|
+
it "should fire and forget" do
|
261
|
+
# this test is totally hacky. I have no clue how to make it verify. I just look at the test servers
|
262
|
+
# to verify that stuff is running
|
263
|
+
hydra = Typhoeus::Hydra.new
|
264
|
+
first = Typhoeus::Request.new("http://localhost:3000/first?delay=1")
|
265
|
+
second = Typhoeus::Request.new("http://localhost:3001/second?delay=2")
|
266
|
+
hydra.queue first
|
267
|
+
hydra.queue second
|
268
|
+
hydra.fire_and_forget
|
269
|
+
third = Typhoeus::Request.new("http://localhost:3002/third?delay=3")
|
270
|
+
hydra.queue third
|
271
|
+
hydra.fire_and_forget
|
272
|
+
sleep 3 # have to do this or future tests may break.
|
273
|
+
end
|
274
|
+
|
275
|
+
it "should take the maximum number of concurrent requests as an argument" do
|
276
|
+
hydra = Typhoeus::Hydra.new(:max_concurrency => 2)
|
277
|
+
first = Typhoeus::Request.new("http://localhost:3000/first?delay=1")
|
278
|
+
second = Typhoeus::Request.new("http://localhost:3001/second?delay=1")
|
279
|
+
third = Typhoeus::Request.new("http://localhost:3002/third?delay=1")
|
280
|
+
hydra.queue first
|
281
|
+
hydra.queue second
|
282
|
+
hydra.queue third
|
283
|
+
|
284
|
+
start_time = Time.now
|
285
|
+
hydra.run
|
286
|
+
finish_time = Time.now
|
287
|
+
|
288
|
+
first.response.code.should == 200
|
289
|
+
second.response.code.should == 200
|
290
|
+
third.response.code.should == 200
|
291
|
+
(finish_time - start_time).should > 2.0
|
292
|
+
end
|
293
|
+
|
294
|
+
it "should respect the follow_location option when set on a request" do
|
295
|
+
hydra = Typhoeus::Hydra.new
|
296
|
+
request = Typhoeus::Request.new("http://localhost:3000/redirect", :follow_location => true)
|
297
|
+
hydra.queue request
|
298
|
+
hydra.run
|
299
|
+
|
300
|
+
request.response.code.should == 200
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should pass through the max_redirects option when set on a request" do
|
304
|
+
hydra = Typhoeus::Hydra.new
|
305
|
+
request = Typhoeus::Request.new("http://localhost:3000/bad_redirect", :max_redirects => 5)
|
306
|
+
hydra.queue request
|
307
|
+
hydra.run
|
308
|
+
|
309
|
+
request.response.code.should == 302
|
310
|
+
end
|
311
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Easy do
|
4
|
+
it "should save easy handles that get added" do
|
5
|
+
multi = Typhoeus::Multi.new
|
6
|
+
easy = Typhoeus::Easy.new
|
7
|
+
easy.url = "http://localhost:3002"
|
8
|
+
easy.method = :get
|
9
|
+
|
10
|
+
multi.add(easy)
|
11
|
+
multi.easy_handles.should == [easy]
|
12
|
+
multi.perform
|
13
|
+
multi.easy_handles.should == []
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should be reusable" do
|
17
|
+
easy = Typhoeus::Easy.new
|
18
|
+
easy.url = "http://localhost:3002"
|
19
|
+
easy.method = :get
|
20
|
+
|
21
|
+
multi = Typhoeus::Multi.new
|
22
|
+
multi.add(easy)
|
23
|
+
multi.perform
|
24
|
+
easy.response_code.should == 200
|
25
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"
|
26
|
+
|
27
|
+
e2 = Typhoeus::Easy.new
|
28
|
+
e2.url = "http://localhost:3002"
|
29
|
+
e2.method = :post
|
30
|
+
multi.add(e2)
|
31
|
+
multi.perform
|
32
|
+
|
33
|
+
e2.response_code.should == 200
|
34
|
+
JSON.parse(e2.response_body)["REQUEST_METHOD"].should == "POST"
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should perform easy handles added after the first one runs" do
|
38
|
+
easy = Typhoeus::Easy.new
|
39
|
+
easy.url = "http://localhost:3002"
|
40
|
+
easy.method = :get
|
41
|
+
multi = Typhoeus::Multi.new
|
42
|
+
multi.add(easy)
|
43
|
+
|
44
|
+
e2 = Typhoeus::Easy.new
|
45
|
+
e2.url = "http://localhost:3002"
|
46
|
+
e2.method = :post
|
47
|
+
easy.on_success do |e|
|
48
|
+
multi.add(e2)
|
49
|
+
end
|
50
|
+
|
51
|
+
multi.perform
|
52
|
+
easy.response_code.should == 200
|
53
|
+
JSON.parse(easy.response_body)["REQUEST_METHOD"].should == "GET"
|
54
|
+
e2.response_code.should == 200
|
55
|
+
JSON.parse(e2.response_body)["REQUEST_METHOD"].should == "POST"
|
56
|
+
end
|
57
|
+
|
58
|
+
# it "should do multiple gets" do
|
59
|
+
# multi = Typhoeus::Multi.new
|
60
|
+
#
|
61
|
+
# handles = []
|
62
|
+
# 5.times do |i|
|
63
|
+
# easy = Typhoeus::Easy.new
|
64
|
+
# easy.url = "http://localhost:3002"
|
65
|
+
# easy.method = :get
|
66
|
+
# easy.on_success {|e| puts "get #{i} succeeded"}
|
67
|
+
# easy.on_failure {|e| puts "get #{i} failed with #{e.response_code}"}
|
68
|
+
# handles << easy
|
69
|
+
# multi.add(easy)
|
70
|
+
# end
|
71
|
+
#
|
72
|
+
# multi.perform
|
73
|
+
# end
|
74
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::RemoteMethod do
|
4
|
+
it "should take options" do
|
5
|
+
Typhoeus::RemoteMethod.new(:body => "foo")
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "http_method" do
|
9
|
+
it "should return the http method" do
|
10
|
+
m = Typhoeus::RemoteMethod.new(:method => :put)
|
11
|
+
m.http_method.should == :put
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should default to :get" do
|
15
|
+
m = Typhoeus::RemoteMethod.new
|
16
|
+
m.http_method.should == :get
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should return the options" do
|
21
|
+
m = Typhoeus::RemoteMethod.new(:body => "foo")
|
22
|
+
m.options.should == {:body => "foo"}
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should pull uri out of the options hash" do
|
26
|
+
m = Typhoeus::RemoteMethod.new(:base_uri => "http://pauldix.net")
|
27
|
+
m.base_uri.should == "http://pauldix.net"
|
28
|
+
m.options.should_not have_key(:base_uri)
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "on_success" do
|
32
|
+
it "should return the block" do
|
33
|
+
m = Typhoeus::RemoteMethod.new(:on_success => lambda {:foo})
|
34
|
+
m.on_success.call.should == :foo
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "on_failure" do
|
39
|
+
it "should return method name" do
|
40
|
+
m = Typhoeus::RemoteMethod.new(:on_failure => lambda {:bar})
|
41
|
+
m.on_failure.call.should == :bar
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "path" do
|
46
|
+
it "should pull path out of the options hash" do
|
47
|
+
m = Typhoeus::RemoteMethod.new(:path => "foo")
|
48
|
+
m.path.should == "foo"
|
49
|
+
m.options.should_not have_key(:path)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should output argument names from the symbols in the path" do
|
53
|
+
m = Typhoeus::RemoteMethod.new(:path => "/posts/:post_id/comments/:comment_id")
|
54
|
+
m.argument_names.should == [:post_id, :comment_id]
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should output an empty string when there are no arguments in path" do
|
58
|
+
m = Typhoeus::RemoteMethod.new(:path => "/default.html")
|
59
|
+
m.argument_names.should == []
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should output and empty string when there is no path specified" do
|
63
|
+
m = Typhoeus::RemoteMethod.new
|
64
|
+
m.argument_names.should == []
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should interpolate a path with arguments" do
|
68
|
+
m = Typhoeus::RemoteMethod.new(:path => "/posts/:post_id/comments/:comment_id")
|
69
|
+
m.interpolate_path_with_arguments(:post_id => 1, :comment_id => "asdf").should == "/posts/1/comments/asdf"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should provide the path when interpolated called and there is nothing to interpolate" do
|
73
|
+
m = Typhoeus::RemoteMethod.new(:path => "/posts/123")
|
74
|
+
m.interpolate_path_with_arguments(:foo => :bar).should == "/posts/123"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe "#merge_options" do
|
79
|
+
it "should keep the passed in options first" do
|
80
|
+
m = Typhoeus::RemoteMethod.new("User-Agent" => "whatev", :foo => :bar)
|
81
|
+
m.merge_options({"User-Agent" => "http-machine"}).should == {"User-Agent" => "http-machine", :foo => :bar}
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should combine the params" do
|
85
|
+
m = Typhoeus::RemoteMethod.new(:foo => :bar, :params => {:id => :asdf})
|
86
|
+
m.merge_options({:params => {:desc => :jkl}}).should == {:foo => :bar, :params => {:id => :asdf, :desc => :jkl}}
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "memoize_reponses" do
|
91
|
+
before(:each) do
|
92
|
+
@m = Typhoeus::RemoteMethod.new(:memoize_responses => true)
|
93
|
+
@args = ["foo", "bar"]
|
94
|
+
@options = {:asdf => {:jkl => :bar}}
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should store if responses should be memoized" do
|
98
|
+
@m.memoize_responses?.should be_true
|
99
|
+
@m.options.should == {}
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should tell when a method is already called" do
|
103
|
+
@m.already_called?(@args, @options).should be_false
|
104
|
+
@m.calling(@args, @options)
|
105
|
+
@m.already_called?(@args, @options).should be_true
|
106
|
+
@m.already_called?([], {}).should be_false
|
107
|
+
end
|
108
|
+
|
109
|
+
it "should call response blocks and clear the methods that have been called" do
|
110
|
+
response_block_called = mock('response_block')
|
111
|
+
response_block_called.should_receive(:call).exactly(1).times
|
112
|
+
|
113
|
+
@m.add_response_block(lambda {|res| res.should == :foo; response_block_called.call}, @args, @options)
|
114
|
+
@m.calling(@args, @options)
|
115
|
+
@m.call_response_blocks(:foo, @args, @options)
|
116
|
+
@m.already_called?(@args, @options).should be_false
|
117
|
+
@m.call_response_blocks(:asdf, @args, @options) #just to make sure it doesn't actually call that block again
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
describe "cache_reponses" do
|
122
|
+
before(:each) do
|
123
|
+
@m = Typhoeus::RemoteMethod.new(:cache_responses => true)
|
124
|
+
@args = ["foo", "bar"]
|
125
|
+
@options = {:asdf => {:jkl => :bar}}
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should store if responses should be cached" do
|
129
|
+
@m.cache_responses?.should be_true
|
130
|
+
@m.options.should == {}
|
131
|
+
end
|
132
|
+
|
133
|
+
it "should force memoization if caching is enabled" do
|
134
|
+
@m.memoize_responses?.should be_true
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should store cache ttl" do
|
138
|
+
Typhoeus::RemoteMethod.new(:cache_responses => 30).cache_ttl.should == 30
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::RemoteProxyObject do
|
4
|
+
before(:each) do
|
5
|
+
@easy = Typhoeus::Easy.new
|
6
|
+
@easy.method = :get
|
7
|
+
@easy.url = "http://localhost:3001"
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should take a caller and call the clear_memoized_proxy_objects" do
|
11
|
+
clear_proxy = lambda {}
|
12
|
+
clear_proxy.should_receive(:call)
|
13
|
+
response = Typhoeus::RemoteProxyObject.new(clear_proxy, @easy)
|
14
|
+
response.code.should == 200
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should take an easy object and return the body when requested" do
|
18
|
+
response = Typhoeus::RemoteProxyObject.new(lambda {}, @easy)
|
19
|
+
@easy.response_code.should == 0
|
20
|
+
response.code.should == 200
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should perform requests only on the first access" do
|
24
|
+
response = Typhoeus::RemoteProxyObject.new(lambda {}, @easy)
|
25
|
+
response.code.should == 200
|
26
|
+
Typhoeus.should_receive(:perform_easy_requests).exactly(0).times
|
27
|
+
response.code.should == 200
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should set the requested_url and requested_http_method on the response" do
|
31
|
+
response = Typhoeus::RemoteProxyObject.new(lambda {}, @easy)
|
32
|
+
response.requested_url.should == "http://localhost:3001"
|
33
|
+
response.requested_http_method.should == :get
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should call the on_success method with an easy object and proxy to the result of on_success" do
|
37
|
+
klass = Class.new do
|
38
|
+
def initialize(r)
|
39
|
+
@response = r
|
40
|
+
end
|
41
|
+
|
42
|
+
def blah
|
43
|
+
@response.code
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
k = Typhoeus::RemoteProxyObject.new(lambda {}, @easy, :on_success => lambda {|e| klass.new(e)})
|
48
|
+
k.blah.should == 200
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should call the on_failure method with an easy object and proxy to the result of on_failure" do
|
52
|
+
klass = Class.new do
|
53
|
+
def initialize(r)
|
54
|
+
@response = r
|
55
|
+
end
|
56
|
+
|
57
|
+
def blah
|
58
|
+
@response.code
|
59
|
+
end
|
60
|
+
end
|
61
|
+
@easy.url = "http://localhost:3005" #bad port
|
62
|
+
k = Typhoeus::RemoteProxyObject.new(lambda {}, @easy, :on_failure => lambda {|e| klass.new(e)})
|
63
|
+
k.blah.should == 0
|
64
|
+
end
|
65
|
+
end
|