xenda-typhoeus 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. data/.gitignore +4 -0
  2. data/CHANGELOG.markdown +70 -0
  3. data/Gemfile +3 -0
  4. data/Gemfile.lock +38 -0
  5. data/LICENSE +20 -0
  6. data/README.textile +397 -0
  7. data/Rakefile +56 -0
  8. data/VERSION +1 -0
  9. data/benchmarks/profile.rb +25 -0
  10. data/benchmarks/vs_nethttp.rb +35 -0
  11. data/examples/file.rb +12 -0
  12. data/examples/times.rb +40 -0
  13. data/examples/twitter.rb +21 -0
  14. data/ext/typhoeus/.gitignore +7 -0
  15. data/ext/typhoeus/extconf.rb +65 -0
  16. data/ext/typhoeus/native.c +12 -0
  17. data/ext/typhoeus/native.h +22 -0
  18. data/ext/typhoeus/typhoeus_easy.c +232 -0
  19. data/ext/typhoeus/typhoeus_easy.h +20 -0
  20. data/ext/typhoeus/typhoeus_form.c +59 -0
  21. data/ext/typhoeus/typhoeus_form.h +13 -0
  22. data/ext/typhoeus/typhoeus_multi.c +217 -0
  23. data/ext/typhoeus/typhoeus_multi.h +16 -0
  24. data/lib/typhoeus.rb +58 -0
  25. data/lib/typhoeus/.gitignore +1 -0
  26. data/lib/typhoeus/easy.rb +379 -0
  27. data/lib/typhoeus/filter.rb +28 -0
  28. data/lib/typhoeus/form.rb +32 -0
  29. data/lib/typhoeus/hydra.rb +247 -0
  30. data/lib/typhoeus/hydra/callbacks.rb +24 -0
  31. data/lib/typhoeus/hydra/connect_options.rb +61 -0
  32. data/lib/typhoeus/hydra/stubbing.rb +52 -0
  33. data/lib/typhoeus/hydra_mock.rb +131 -0
  34. data/lib/typhoeus/multi.rb +37 -0
  35. data/lib/typhoeus/normalized_header_hash.rb +58 -0
  36. data/lib/typhoeus/remote.rb +306 -0
  37. data/lib/typhoeus/remote_method.rb +108 -0
  38. data/lib/typhoeus/remote_proxy_object.rb +50 -0
  39. data/lib/typhoeus/request.rb +203 -0
  40. data/lib/typhoeus/response.rb +91 -0
  41. data/lib/typhoeus/service.rb +20 -0
  42. data/lib/typhoeus/utils.rb +63 -0
  43. data/profilers/valgrind.rb +24 -0
  44. data/spec/fixtures/placeholder.gif +0 -0
  45. data/spec/fixtures/placeholder.txt +1 -0
  46. data/spec/fixtures/placeholder.ukn +0 -0
  47. data/spec/fixtures/result_set.xml +60 -0
  48. data/spec/servers/app.rb +94 -0
  49. data/spec/spec.opts +3 -0
  50. data/spec/spec_helper.rb +14 -0
  51. data/spec/typhoeus/easy_spec.rb +343 -0
  52. data/spec/typhoeus/filter_spec.rb +35 -0
  53. data/spec/typhoeus/form_spec.rb +117 -0
  54. data/spec/typhoeus/hydra_mock_spec.rb +300 -0
  55. data/spec/typhoeus/hydra_spec.rb +574 -0
  56. data/spec/typhoeus/multi_spec.rb +74 -0
  57. data/spec/typhoeus/normalized_header_hash_spec.rb +41 -0
  58. data/spec/typhoeus/remote_method_spec.rb +141 -0
  59. data/spec/typhoeus/remote_proxy_object_spec.rb +65 -0
  60. data/spec/typhoeus/remote_spec.rb +695 -0
  61. data/spec/typhoeus/request_spec.rb +300 -0
  62. data/spec/typhoeus/response_spec.rb +151 -0
  63. data/spec/typhoeus/utils_spec.rb +22 -0
  64. data/xenda-typhoeus.gemspec +139 -0
  65. metadata +203 -0
@@ -0,0 +1,35 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Filter do
4
+ it "should take a method name and optionally take options" do
5
+ filter = Typhoeus::Filter.new(:bar, :only => :foo)
6
+ filter = Typhoeus::Filter.new(:bar)
7
+ end
8
+
9
+ describe "#apply_filter?" do
10
+ it "should return true for any method when :only and :except aren't specified" do
11
+ filter = Typhoeus::Filter.new(:bar)
12
+ filter.apply_filter?(:asdf).should be_true
13
+ end
14
+
15
+ it "should return true if a method is in only" do
16
+ filter = Typhoeus::Filter.new(:bar, :only => :foo)
17
+ filter.apply_filter?(:foo).should be_true
18
+ end
19
+
20
+ it "should return false if a method isn't in only" do
21
+ filter = Typhoeus::Filter.new(:bar, :only => :foo)
22
+ filter.apply_filter?(:bar).should be_false
23
+ end
24
+
25
+ it "should return true if a method isn't in except" do
26
+ filter = Typhoeus::Filter.new(:bar, :except => :foo)
27
+ filter.apply_filter?(:bar).should be_true
28
+ end
29
+
30
+ it "should return false if a method is in except" do
31
+ filter = Typhoeus::Filter.new(:bar, :except => :foo)
32
+ filter.apply_filter?(:foo).should be_false
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,117 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Form do
4
+ describe "#process!" do
5
+ it "should generate a valid form object" do
6
+ form = Typhoeus::Form.new({
7
+ :name => "John Smith",
8
+ :age => "29"
9
+ })
10
+ form.should_receive(:formadd_param).with("name", "John Smith")
11
+ form.should_receive(:formadd_param).with("age", "29")
12
+ form.process!
13
+ end
14
+
15
+ it "should handle params that are a hash" do
16
+ form = Typhoeus::Form.new({
17
+ :attributes => {
18
+ :eyes => "brown",
19
+ :hair => "green",
20
+ :teeth => "white"
21
+ },
22
+ :name => "John Smith",
23
+ :age => "29"
24
+ })
25
+ form.should_receive(:formadd_param).with("attributes[eyes]", "brown")
26
+ form.should_receive(:formadd_param).with("attributes[hair]", "green")
27
+ form.should_receive(:formadd_param).with("attributes[teeth]", "white")
28
+ form.should_receive(:formadd_param).with("name", "John Smith")
29
+ form.should_receive(:formadd_param).with("age", "29")
30
+ form.process!
31
+ end
32
+
33
+ it "should params that have mutliple values" do
34
+ form = Typhoeus::Form.new({
35
+ :colors => ["brown", "green", "white"],
36
+ :name => "John Smith",
37
+ :age => "29"
38
+ })
39
+ form.should_receive(:formadd_param).with("colors", "brown")
40
+ form.should_receive(:formadd_param).with("colors", "green")
41
+ form.should_receive(:formadd_param).with("colors", "white")
42
+ form.should_receive(:formadd_param).with("name", "John Smith")
43
+ form.should_receive(:formadd_param).with("age", "29")
44
+ form.process!
45
+ end
46
+
47
+ context "when a File object is a param" do
48
+ it "should handle one file" do
49
+ form = Typhoeus::Form.new(
50
+ :file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")
51
+ )
52
+ form.should_receive(:formadd_file).with("file", "placeholder.txt", "text/plain", anything)
53
+ form.process!
54
+ end
55
+
56
+ it "should handle more than one file" do
57
+ form = Typhoeus::Form.new(
58
+ :text_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r"),
59
+ :gif_file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.gif"), "r")
60
+ )
61
+ form.should_receive(:formadd_file).with("gif_file", "placeholder.gif", "image/gif", anything)
62
+ form.should_receive(:formadd_file).with("text_file", "placeholder.txt", "text/plain", anything)
63
+ form.process!
64
+ end
65
+
66
+ it "should handle tempfiles (file subclasses)" do
67
+ tempfile = Tempfile.new('placeholder_temp')
68
+ form = Typhoeus::Form.new(
69
+ :file => tempfile
70
+ )
71
+ form.should_receive(:formadd_file).with("file", File.basename(tempfile.path), "application/octet-stream", anything)
72
+ form.process!
73
+ end
74
+
75
+ it "should default to 'application/octet-stream' if no content type can be determined" do
76
+ pending
77
+ form = Typhoeus::Form.new(
78
+ :file => File.open(File.expand_path(File.dirname(__FILE__) + "/../fixtures/placeholder.txt"), "r")
79
+ )
80
+ form.should_receive(:formadd_file).with("file", "placeholder.ukn", "application/octet-stream", anything)
81
+ form.process!
82
+ end
83
+ end
84
+ end
85
+
86
+ describe "#to_s" do
87
+ it "should generate a valid query string" do
88
+ form = Typhoeus::Form.new({
89
+ :name => "John Smith",
90
+ :age => "29"
91
+ })
92
+ form.to_s.should == "age=29&name=John Smith"
93
+ end
94
+
95
+ it "should handle params that are a hash" do
96
+ form = Typhoeus::Form.new({
97
+ :attributes => {
98
+ :eyes => "brown",
99
+ :hair => "green",
100
+ :teeth => "white"
101
+ },
102
+ :name => "John Smith",
103
+ :age => "29"
104
+ })
105
+ form.to_s.should == "age=29&attributes[eyes]=brown&attributes[hair]=green&attributes[teeth]=white&name=John Smith"
106
+ end
107
+
108
+ it "should params that have multiple values" do
109
+ form = Typhoeus::Form.new({
110
+ :colors => ["brown", "green", "white"],
111
+ :name => "John Smith",
112
+ :age => "29"
113
+ })
114
+ form.to_s.should == "age=29&colors=brown&colors=green&colors=white&name=John Smith"
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,300 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe Typhoeus::HydraMock do
4
+ it "should mark all responses as mocks" do
5
+ response = Typhoeus::Response.new(:mock => false)
6
+ response.should_not be_mock
7
+
8
+ mock = Typhoeus::HydraMock.new("http://localhost", :get)
9
+ mock.and_return(response)
10
+
11
+ mock.response.should be_mock
12
+ response.should be_mock
13
+ end
14
+
15
+ describe "stubbing response values" do
16
+ before(:each) do
17
+ @stub = Typhoeus::HydraMock.new('http://localhost:3000', :get)
18
+ end
19
+
20
+ describe "with a single response" do
21
+ it "should always return that response" do
22
+ response = Typhoeus::Response.new
23
+ @stub.and_return(response)
24
+
25
+ 5.times do
26
+ @stub.response.should == response
27
+ end
28
+ end
29
+ end
30
+
31
+ describe "with multiple responses" do
32
+ it "should return consecutive responses in the array, then keep returning the last one" do
33
+ responses = []
34
+ 3.times do |i|
35
+ responses << Typhoeus::Response.new(:body => "response #{i}")
36
+ end
37
+
38
+ # Stub 3 consecutive responses.
39
+ @stub.and_return(responses)
40
+
41
+ 0.upto(2) do |i|
42
+ @stub.response.should == responses[i]
43
+ end
44
+
45
+ 5.times do
46
+ @stub.response.should == responses.last
47
+ end
48
+ end
49
+ end
50
+ end
51
+
52
+ describe "#matches?" do
53
+ describe "basic matching" do
54
+ it "should not match if the HTTP verbs are different" do
55
+ request = Typhoeus::Request.new("http://localhost:3000",
56
+ :method => :get)
57
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :post)
58
+ mock.matches?(request).should be_false
59
+ end
60
+ end
61
+
62
+ describe "matching on ports" do
63
+ it "should handle default port 80 sanely" do
64
+ mock = Typhoeus::HydraMock.new('http://www.example.com:80/', :get,
65
+ :headers => { 'user-agent' => 'test' })
66
+ request = Typhoeus::Request.new('http://www.example.com/',
67
+ :method => :get,
68
+ :user_agent => 'test')
69
+ mock.matches?(request).should be_true
70
+ end
71
+
72
+ it "should handle default port 443 sanely" do
73
+ mock = Typhoeus::HydraMock.new('https://www.example.com:443/', :get,
74
+ :headers => { 'user-agent' => 'test' })
75
+ request = Typhoeus::Request.new('https://www.example.com/',
76
+ :method => :get,
77
+ :user_agent => 'test')
78
+ mock.matches?(request).should be_true
79
+ end
80
+ end
81
+
82
+
83
+ describe "any HTTP verb" do
84
+ it "should match any verb" do
85
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :any,
86
+ :headers => { 'user-agent' => 'test' })
87
+ [:get, :post, :delete, :put].each do |verb|
88
+ request = Typhoeus::Request.new("http://localhost:3000",
89
+ :method => verb,
90
+ :user_agent => 'test')
91
+ mock.matches?(request).should be_true
92
+ end
93
+ end
94
+ end
95
+
96
+ describe "header matching" do
97
+ def request(options = {})
98
+ Typhoeus::Request.new("http://localhost:3000", options.merge(:method => :get))
99
+ end
100
+
101
+ def mock(options = {})
102
+ Typhoeus::HydraMock.new("http://localhost:3000", :get, options)
103
+ end
104
+
105
+ context 'when no :headers option is given' do
106
+ subject { mock }
107
+
108
+ it "matches regardless of whether or not the request has headers" do
109
+ subject.matches?(request(:headers => nil)).should be_true
110
+ subject.matches?(request(:headers => {})).should be_true
111
+ subject.matches?(request(:headers => { 'a' => 'b' })).should be_true
112
+ end
113
+ end
114
+
115
+ [nil, {}].each do |value|
116
+ context "for :headers => #{value.inspect}" do
117
+ subject { mock(:headers => value) }
118
+
119
+ it "matches when the request has no headers" do
120
+ subject.matches?(request(:headers => nil)).should be_true
121
+ subject.matches?(request(:headers => {})).should be_true
122
+ end
123
+
124
+ it "does not match when the request has headers" do
125
+ subject.matches?(request(:headers => { 'a' => 'b' })).should be_false
126
+ end
127
+ end
128
+ end
129
+
130
+ context 'for :headers => [a hash]' do
131
+ it 'does not match if the request has no headers' do
132
+ m = mock(:headers => { 'A' => 'B', 'C' => 'D' })
133
+
134
+ m.matches?(request).should be_false
135
+ m.matches?(request(:headers => nil)).should be_false
136
+ m.matches?(request(:headers => {})).should be_false
137
+ end
138
+
139
+ it 'does not match if the request lacks any of the given headers' do
140
+ mock(
141
+ :headers => { 'A' => 'B', 'C' => 'D' }
142
+ ).matches?(request(
143
+ :headers => { 'A' => 'B' }
144
+ )).should be_false
145
+ end
146
+
147
+ it 'does not match if any of the specified values are different from the request value' do
148
+ mock(
149
+ :headers => { 'A' => 'B', 'C' => 'D' }
150
+ ).matches?(request(
151
+ :headers => { 'A' => 'B', 'C' => 'E' }
152
+ )).should be_false
153
+ end
154
+
155
+ it 'matches if the given hash is exactly equal to the request headers' do
156
+ mock(
157
+ :headers => { 'A' => 'B', 'C' => 'D' }
158
+ ).matches?(request(
159
+ :headers => { 'A' => 'B', 'C' => 'D' }
160
+ )).should be_true
161
+ end
162
+
163
+ it 'matches even if the request has additional headers not specified in the mock' do
164
+ mock(
165
+ :headers => { 'A' => 'B', 'C' => 'D' }
166
+ ).matches?(request(
167
+ :headers => { 'A' => 'B', 'C' => 'D', 'E' => 'F' }
168
+ )).should be_true
169
+ end
170
+
171
+ it 'matches even if the casing of the header keys is different between the mock and request' do
172
+ mock(
173
+ :headers => { 'A' => 'B', 'c' => 'D' }
174
+ ).matches?(request(
175
+ :headers => { 'a' => 'B', 'C' => 'D' }
176
+ )).should be_true
177
+ end
178
+
179
+ it 'matches if the mocked values are regexes and match the request values' do
180
+ mock(
181
+ :headers => { 'A' => /foo/, }
182
+ ).matches?(request(
183
+ :headers => { 'A' => 'foo bar' }
184
+ )).should be_true
185
+ end
186
+
187
+ it 'does not match if the mocked values are regexes and do not match the request values' do
188
+ mock(
189
+ :headers => { 'A' => /foo/, }
190
+ ).matches?(request(
191
+ :headers => { 'A' => 'bar' }
192
+ )).should be_false
193
+ end
194
+
195
+ context 'when a header is specified as an array' do
196
+ it 'matches when the request header has the same array' do
197
+ mock(
198
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
199
+ ).matches?(request(
200
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
201
+ )).should be_true
202
+ end
203
+
204
+ it 'matches when the request header is a single value and the mock array has the same value' do
205
+ mock(
206
+ :headers => { 'Accept' => ['text/html'] }
207
+ ).matches?(request(
208
+ :headers => { 'Accept' => 'text/html' }
209
+ )).should be_true
210
+ end
211
+
212
+ it 'matches even when the request header array is ordered differently' do
213
+ mock(
214
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
215
+ ).matches?(request(
216
+ :headers => { 'Accept' => ['text/plain', 'text/html'] }
217
+ )).should be_true
218
+ end
219
+
220
+ it 'does not match when the request header array lacks a value' do
221
+ mock(
222
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
223
+ ).matches?(request(
224
+ :headers => { 'Accept' => ['text/plain'] }
225
+ )).should be_false
226
+ end
227
+
228
+ it 'does not match when the request header array has an extra value' do
229
+ mock(
230
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
231
+ ).matches?(request(
232
+ :headers => { 'Accept' => ['text/html', 'text/plain', 'application/xml'] }
233
+ )).should be_false
234
+ end
235
+
236
+ it 'does not match when the request header is not an array' do
237
+ mock(
238
+ :headers => { 'Accept' => ['text/html', 'text/plain'] }
239
+ ).matches?(request(
240
+ :headers => { 'Accept' => 'text/html' }
241
+ )).should be_false
242
+ end
243
+ end
244
+ end
245
+ end
246
+
247
+ describe "post body matching" do
248
+ it "should not bother matching on body if we don't turn the option on" do
249
+ request = Typhoeus::Request.new("http://localhost:3000",
250
+ :method => :get,
251
+ :user_agent => 'test',
252
+ :body => "fdsafdsa")
253
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :get,
254
+ :headers => { 'user-agent' => 'test' })
255
+ mock.matches?(request).should be_true
256
+ end
257
+
258
+ it "should match nil correctly" do
259
+ request = Typhoeus::Request.new("http://localhost:3000",
260
+ :method => :get,
261
+ :body => "fdsafdsa")
262
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :get,
263
+ :body => nil)
264
+ mock.matches?(request).should be_false
265
+ end
266
+
267
+ it "should not match if the bodies do not match" do
268
+ request = Typhoeus::Request.new("http://localhost:3000",
269
+ :method => :get,
270
+ :body => "ffdsadsafdsa")
271
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :get,
272
+ :body => 'fdsafdsa')
273
+ mock.matches?(request).should be_false
274
+ end
275
+
276
+ it "should match on optional body parameter" do
277
+ request = Typhoeus::Request.new("http://localhost:3000",
278
+ :method => :get,
279
+ :user_agent => 'test',
280
+ :body => "fdsafdsa")
281
+ mock = Typhoeus::HydraMock.new("http://localhost:3000", :get,
282
+ :body => 'fdsafdsa',
283
+ :headers => {
284
+ 'User-Agent' => 'test'
285
+ })
286
+ mock.matches?(request).should be_true
287
+ end
288
+
289
+ it "should regex match" do
290
+ request = Typhoeus::Request.new("http://localhost:3000/whatever/fdsa",
291
+ :method => :get,
292
+ :user_agent => 'test')
293
+ mock = Typhoeus::HydraMock.new(/fdsa/, :get,
294
+ :headers => { 'user-agent' => 'test' })
295
+ mock.matches?(request).should be_true
296
+ end
297
+ end
298
+ end
299
+ end
300
+