typhoeus 0.3.3 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/{CHANGELOG.markdown → CHANGELOG.md} +21 -12
  2. data/LICENSE +2 -0
  3. data/README.md +455 -0
  4. data/Rakefile +6 -26
  5. data/lib/typhoeus.rb +4 -6
  6. data/lib/typhoeus/curl.rb +453 -0
  7. data/lib/typhoeus/easy.rb +60 -358
  8. data/lib/typhoeus/easy/auth.rb +14 -0
  9. data/lib/typhoeus/easy/callbacks.rb +33 -0
  10. data/lib/typhoeus/easy/ffi_helper.rb +61 -0
  11. data/lib/typhoeus/easy/infos.rb +90 -0
  12. data/lib/typhoeus/easy/options.rb +115 -0
  13. data/lib/typhoeus/easy/proxy.rb +20 -0
  14. data/lib/typhoeus/easy/ssl.rb +82 -0
  15. data/lib/typhoeus/form.rb +30 -1
  16. data/lib/typhoeus/{normalized_header_hash.rb → header.rb} +2 -6
  17. data/lib/typhoeus/hydra.rb +9 -12
  18. data/lib/typhoeus/hydra_mock.rb +2 -2
  19. data/lib/typhoeus/multi.rb +118 -9
  20. data/lib/typhoeus/param_processor.rb +43 -0
  21. data/lib/typhoeus/request.rb +18 -21
  22. data/lib/typhoeus/response.rb +5 -4
  23. data/lib/typhoeus/utils.rb +14 -27
  24. data/lib/typhoeus/version.rb +1 -1
  25. metadata +155 -152
  26. data/Gemfile.lock +0 -37
  27. data/ext/typhoeus/.gitignore +0 -7
  28. data/ext/typhoeus/extconf.rb +0 -65
  29. data/ext/typhoeus/native.c +0 -12
  30. data/ext/typhoeus/native.h +0 -22
  31. data/ext/typhoeus/typhoeus_easy.c +0 -232
  32. data/ext/typhoeus/typhoeus_easy.h +0 -20
  33. data/ext/typhoeus/typhoeus_form.c +0 -59
  34. data/ext/typhoeus/typhoeus_form.h +0 -13
  35. data/ext/typhoeus/typhoeus_multi.c +0 -217
  36. data/ext/typhoeus/typhoeus_multi.h +0 -16
  37. data/lib/typhoeus/.gitignore +0 -1
  38. data/lib/typhoeus/service.rb +0 -20
  39. data/spec/fixtures/placeholder.gif +0 -0
  40. data/spec/fixtures/placeholder.txt +0 -1
  41. data/spec/fixtures/placeholder.ukn +0 -0
  42. data/spec/fixtures/result_set.xml +0 -60
  43. data/spec/servers/app.rb +0 -97
  44. data/spec/spec_helper.rb +0 -19
  45. data/spec/support/typhoeus_localhost_server.rb +0 -58
  46. data/spec/typhoeus/easy_spec.rb +0 -391
  47. data/spec/typhoeus/filter_spec.rb +0 -35
  48. data/spec/typhoeus/form_spec.rb +0 -117
  49. data/spec/typhoeus/hydra_mock_spec.rb +0 -300
  50. data/spec/typhoeus/hydra_spec.rb +0 -602
  51. data/spec/typhoeus/multi_spec.rb +0 -74
  52. data/spec/typhoeus/normalized_header_hash_spec.rb +0 -41
  53. data/spec/typhoeus/remote_method_spec.rb +0 -141
  54. data/spec/typhoeus/remote_proxy_object_spec.rb +0 -65
  55. data/spec/typhoeus/remote_spec.rb +0 -695
  56. data/spec/typhoeus/request_spec.rb +0 -387
  57. data/spec/typhoeus/response_spec.rb +0 -192
  58. data/spec/typhoeus/utils_spec.rb +0 -22
  59. data/typhoeus.gemspec +0 -33
@@ -1,35 +0,0 @@
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
@@ -1,117 +0,0 @@
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
@@ -1,300 +0,0 @@
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
- :headers => { '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
- :headers => { '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
- :headers => { '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 hydra(options = {})
102
- Typhoeus::HydraMock.new("http://localhost:3000", :get, options)
103
- end
104
-
105
- context 'when no :headers option is given' do
106
- subject { hydra }
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 { hydra(: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 = hydra(: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
- hydra(
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
- hydra(
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
- hydra(
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 hydra' do
164
- hydra(
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 hydra and request' do
172
- hydra(
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 hydraed values are regexes and match the request values' do
180
- hydra(
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 hydraed values are regexes and do not match the request values' do
188
- hydra(
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
- hydra(
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 hydra array has the same value' do
205
- hydra(
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
- hydra(
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
- hydra(
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
- hydra(
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
- hydra(
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
- :headers => { '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
- :headers => { '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
- :headers => { '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
-