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,300 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Request do
4
+ describe "#inspect" do
5
+ before(:each) do
6
+ @request = Typhoeus::Request.new('http://www.google.com/',
7
+ :body => "a=1&b=2",
8
+ :params => { :c => 'ok' },
9
+ :method => :get,
10
+ :headers => { 'Content-Type' => 'text/html' })
11
+ end
12
+
13
+ it "should dump out the URI" do
14
+ @request.inspect.should =~ /http:\/\/www\.google\.com/
15
+ end
16
+
17
+ it "should dump out the body" do
18
+ @request.inspect.should =~ /a=1&b=2/
19
+ end
20
+
21
+ it "should dump params" do
22
+ @request.inspect.should =~ /:c\s*=>\s*"ok"/
23
+ end
24
+
25
+ it "should dump the method" do
26
+ @request.inspect.should =~ /:get/
27
+ end
28
+
29
+ it "should dump out headers" do
30
+ @request.inspect.should =~ /"Content-Type"\s*=>\s*"text\/html"/
31
+ end
32
+ end
33
+
34
+ describe "#localhost?" do
35
+ %w(localhost 127.0.0.1 0.0.0.0).each do |host|
36
+ it "should be true for the #{host} host" do
37
+ req = Typhoeus::Request.new("http://#{host}")
38
+ req.should be_localhost
39
+ end
40
+ end
41
+
42
+ it "should be false for other domains" do
43
+ req = Typhoeus::Request.new("http://google.com")
44
+ req.should_not be_localhost
45
+ end
46
+ end
47
+
48
+ describe "#params_string" do
49
+ it "should dump a sorted string" do
50
+ request = Typhoeus::Request.new(
51
+ "http://google.com",
52
+ :params => {
53
+ 'b' => 'fdsa',
54
+ 'a' => 'jlk',
55
+ 'c' => '789'
56
+ }
57
+ )
58
+
59
+ request.params_string.should == "a=jlk&b=fdsa&c=789"
60
+ end
61
+
62
+ it "should accept symboled keys" do
63
+ request = Typhoeus::Request.new('http://google.com',
64
+ :params => {
65
+ :b => 'fdsa',
66
+ :a => 'jlk',
67
+ :c => '789'
68
+ })
69
+ request.params_string.should == "a=jlk&b=fdsa&c=789"
70
+ end
71
+
72
+ it "should translate params with values that are arrays to the proper format" do
73
+ request = Typhoeus::Request.new('http://google.com',
74
+ :params => {
75
+ :a => ['789','2434']
76
+ })
77
+ request.params_string.should == "a=789&a=2434"
78
+ end
79
+
80
+ it "should allow the newer bracket notation for array params" do
81
+ request = Typhoeus::Request.new('http://google.com',
82
+ :params => {
83
+ "a[]" => ['789','2434']
84
+ })
85
+ request.params_string.should == "a%5B%5D=789&a%5B%5D=2434"
86
+ end
87
+
88
+ it "should nest arrays in hashes" do
89
+ request = Typhoeus::Request.new('http://google.com',
90
+ :params => {
91
+ :a => { :b => { :c => ['d','e'] } }
92
+ })
93
+ request.params_string.should == "a%5Bb%5D%5Bc%5D=d&a%5Bb%5D%5Bc%5D=e"
94
+ end
95
+
96
+ it "should translate nested params correctly" do
97
+ request = Typhoeus::Request.new('http://google.com',
98
+ :params => {
99
+ :a => { :b => { :c => 'd' } }
100
+ })
101
+ request.params_string.should == "a%5Bb%5D%5Bc%5D=d"
102
+ end
103
+ end
104
+
105
+ describe "quick request methods" do
106
+ it "can run a GET synchronously" do
107
+ response = Typhoeus::Request.get("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
108
+ response.code.should == 200
109
+ JSON.parse(response.body)["REQUEST_METHOD"].should == "GET"
110
+ end
111
+
112
+ it "can run a POST synchronously" do
113
+ response = Typhoeus::Request.post("http://localhost:3000", :params => {:q => { :a => "hi" } }, :headers => {:foo => "bar"})
114
+ response.code.should == 200
115
+ json = JSON.parse(response.body)
116
+ json["REQUEST_METHOD"].should == "POST"
117
+ json["rack.request.query_hash"]["q"]["a"].should == "hi"
118
+ end
119
+
120
+ it "can run a PUT synchronously" do
121
+ response = Typhoeus::Request.put("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
122
+ response.code.should == 200
123
+ JSON.parse(response.body)["REQUEST_METHOD"].should == "PUT"
124
+ end
125
+
126
+ it "can run a DELETE synchronously" do
127
+ response = Typhoeus::Request.delete("http://localhost:3000", :params => {:q => "hi"}, :headers => {:foo => "bar"})
128
+ response.code.should == 200
129
+ JSON.parse(response.body)["REQUEST_METHOD"].should == "DELETE"
130
+ end
131
+ end
132
+
133
+ it "takes url as the first argument" do
134
+ Typhoeus::Request.new("http://localhost:3000").url.should == "http://localhost:3000"
135
+ end
136
+
137
+ it "should parse the host from the url" do
138
+ Typhoeus::Request.new("http://localhost:3000/whatever?hi=foo").host.should == "http://localhost:3000"
139
+ Typhoeus::Request.new("http://localhost:3000?hi=foo").host.should == "http://localhost:3000"
140
+ Typhoeus::Request.new("http://localhost:3000").host.should == "http://localhost:3000"
141
+ end
142
+
143
+ it "takes method as an option" do
144
+ Typhoeus::Request.new("http://localhost:3000", :method => :get).method.should == :get
145
+ end
146
+
147
+ it "takes headers as an option" do
148
+ headers = {:foo => :bar}
149
+ request = Typhoeus::Request.new("http://localhost:3000", :headers => headers)
150
+ request.headers.should == headers
151
+ end
152
+
153
+ it "takes params as an option and adds them to the url" do
154
+ Typhoeus::Request.new("http://localhost:3000", :params => {:foo => "bar"}).url.should == "http://localhost:3000?foo=bar"
155
+ end
156
+
157
+ it "takes request body as an option" do
158
+ Typhoeus::Request.new("http://localhost:3000", :body => "whatever").body.should == "whatever"
159
+ end
160
+
161
+ it "takes timeout as an option" do
162
+ Typhoeus::Request.new("http://localhost:3000", :timeout => 10).timeout.should == 10
163
+ end
164
+
165
+ it "takes cache_timeout as an option" do
166
+ Typhoeus::Request.new("http://localhost:3000", :cache_timeout => 60).cache_timeout.should == 60
167
+ end
168
+
169
+ it "takes follow_location as an option" do
170
+ Typhoeus::Request.new("http://localhost:3000", :follow_location => true).follow_location.should == true
171
+ end
172
+
173
+ it "takes max_redirects as an option" do
174
+ Typhoeus::Request.new("http://localhost:3000", :max_redirects => 10).max_redirects.should == 10
175
+ end
176
+
177
+ it "has the associated response object" do
178
+ request = Typhoeus::Request.new("http://localhost:3000")
179
+ request.response = :foo
180
+ request.response.should == :foo
181
+ end
182
+
183
+ it "has an on_complete handler that is called when the request is completed" do
184
+ request = Typhoeus::Request.new("http://localhost:3000")
185
+ foo = nil
186
+ request.on_complete do |response|
187
+ foo = response
188
+ end
189
+ request.response = :bar
190
+ request.call_handlers
191
+ foo.should == :bar
192
+ end
193
+
194
+ it "has an on_complete setter" do
195
+ foo = nil
196
+ proc = Proc.new {|response| foo = response}
197
+ request = Typhoeus::Request.new("http://localhost:3000")
198
+ request.on_complete = proc
199
+ request.response = :bar
200
+ request.call_handlers
201
+ foo.should == :bar
202
+ end
203
+
204
+ it "stores the handled response that is the return value from the on_complete block" do
205
+ request = Typhoeus::Request.new("http://localhost:3000")
206
+ request.on_complete do |response|
207
+ "handled"
208
+ end
209
+ request.response = :bar
210
+ request.call_handlers
211
+ request.handled_response.should == "handled"
212
+ end
213
+
214
+ it "has an after_complete handler that recieves what on_complete returns" do
215
+ request = Typhoeus::Request.new("http://localhost:3000")
216
+ request.on_complete do |response|
217
+ "handled"
218
+ end
219
+ good = nil
220
+ request.after_complete do |object|
221
+ good = object == "handled"
222
+ end
223
+ request.call_handlers
224
+ good.should be_true
225
+ end
226
+
227
+ it "has an after_complete setter" do
228
+ request = Typhoeus::Request.new("http://localhost:3000")
229
+ request.on_complete do |response|
230
+ "handled"
231
+ end
232
+ good = nil
233
+ proc = Proc.new {|object| good = object == "handled"}
234
+ request.after_complete = proc
235
+
236
+ request.call_handlers
237
+ good.should be_true
238
+ end
239
+
240
+ describe "time info" do
241
+ it "should have time" do
242
+ response = Typhoeus::Request.get("http://localhost:3000")
243
+ response.time.should > 0
244
+ end
245
+
246
+ it "should have connect time" do
247
+ response = Typhoeus::Request.get("http://localhost:3000")
248
+ response.connect_time.should > 0
249
+ end
250
+
251
+ it "should have app connect time" do
252
+ response = Typhoeus::Request.get("http://localhost:3000")
253
+ response.app_connect_time.should > 0
254
+ end
255
+
256
+ it "should have start transfer time" do
257
+ response = Typhoeus::Request.get("http://localhost:3000")
258
+ response.start_transfer_time.should > 0
259
+ end
260
+
261
+ it "should have pre-transfer time" do
262
+ response = Typhoeus::Request.get("http://localhost:3000")
263
+ response.pretransfer_time.should > 0
264
+ end
265
+
266
+ end
267
+
268
+
269
+ describe "authentication" do
270
+
271
+ it "should allow to set username and password" do
272
+ auth = { :username => 'foo', :password => 'bar' }
273
+ e = Typhoeus::Request.get(
274
+ "http://localhost:3001/auth_basic/#{auth[:username]}/#{auth[:password]}",
275
+ auth
276
+ )
277
+ e.code.should == 200
278
+ end
279
+
280
+ it "should allow to set authentication method" do
281
+ auth = {
282
+ :username => 'username',
283
+ :password => 'password',
284
+ :auth_method => :ntlm
285
+ }
286
+ e = Typhoeus::Request.get(
287
+ "http://localhost:3001/auth_ntlm",
288
+ auth
289
+ )
290
+ e.code.should == 200
291
+ end
292
+
293
+ end
294
+
295
+ describe "retry" do
296
+ it "should take a retry option"
297
+ it "should count the number of times a request has failed"
298
+ end
299
+
300
+ end
@@ -0,0 +1,151 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
+
3
+ describe Typhoeus::Response do
4
+ describe "timed_out?" do
5
+ it "should return true if curl return code is 28" do
6
+ response = Typhoeus::Response.new(:curl_return_code => 28)
7
+ response.should be_timed_out
8
+ end
9
+
10
+ it "should return false for not 28" do
11
+ response = Typhoeus::Response.new(:curl_return_code => 14)
12
+ response.should_not be_timed_out
13
+ end
14
+ end
15
+
16
+ describe "initialize" do
17
+ it "should store headers_hash" do
18
+ response = Typhoeus::Response.new(:headers_hash => {})
19
+ response.headers_hash.should == {}
20
+ end
21
+
22
+ it "allows header access using a different casing of the header key" do
23
+ response = Typhoeus::Response.new(:headers_hash => { 'content-type' => 'text/html' } )
24
+ response.headers_hash['Content-Type'].should == 'text/html'
25
+ end
26
+
27
+ it "should store response_code" do
28
+ Typhoeus::Response.new(:code => 200).code.should == 200
29
+ end
30
+
31
+ it "should store status_message" do
32
+ Typhoeus::Response.new(:status_message => 'Not Found').status_message.should == 'Not Found'
33
+ end
34
+
35
+ it "should return nil for status_message if none is given and no header is given" do
36
+ Typhoeus::Response.new.status_message.should be_nil
37
+ end
38
+
39
+ it "should store http_version" do
40
+ Typhoeus::Response.new(:http_version => '1.1').http_version.should == '1.1'
41
+ end
42
+
43
+ it "should return nil for http version if none is given and no header is given" do
44
+ Typhoeus::Response.new.http_version.should be_nil
45
+ end
46
+
47
+ it "should store response_headers" do
48
+ Typhoeus::Response.new(:headers => "a header!").headers.should == "a header!"
49
+ end
50
+
51
+ it "should store response_body" do
52
+ Typhoeus::Response.new(:body => "a body!").body.should == "a body!"
53
+ end
54
+
55
+ it "should store request_time" do
56
+ Typhoeus::Response.new(:time => 1.23).time.should == 1.23
57
+ end
58
+
59
+ it "should store requested_url" do
60
+ response = Typhoeus::Response.new(:requested_url => "http://test.com")
61
+ response.requested_url.should == "http://test.com"
62
+ end
63
+
64
+ it "should store requested_http_method" do
65
+ response = Typhoeus::Response.new(:requested_http_method => :delete)
66
+ response.requested_http_method.should == :delete
67
+ end
68
+
69
+ it "should store an associated request object" do
70
+ response = Typhoeus::Response.new(:request => "whatever")
71
+ response.request.should == "whatever"
72
+ end
73
+
74
+ it "should not default to be a mock response" do
75
+ response = Typhoeus::Response.new
76
+ response.should_not be_mock
77
+ end
78
+ end
79
+
80
+ describe "#mock?" do
81
+ it "should be true if it's a mock response" do
82
+ response = Typhoeus::Response.new(:mock => true)
83
+ response.should be_mock
84
+ end
85
+ end
86
+
87
+ describe "headers" do
88
+ it 'should return an empty hash from #headers_hash when no headers string is given' do
89
+ response = Typhoeus::Response.new.headers_hash.should == {}
90
+ end
91
+
92
+ describe "basic parsing" do
93
+ before(:all) do
94
+ @response = Typhoeus::Response.new(:headers => "HTTP/1.1 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nStatus: 200\r\nX-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.9\r\nX-Cache: miss\r\nX-Runtime: 184\r\nETag: e001d08d9354ab7bc7c27a00163a3afa\r\nCache-Control: private, max-age=0, must-revalidate\r\nContent-Length: 4725\r\nSet-Cookie: _some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly\r\nServer: nginx/0.6.37 + Phusion Passenger 2.2.4 (mod_rails/mod_rack)\r\nSet-Cookie: foo=bar; path=/;\r\nP3P: CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"\r\n\r\n")
95
+ end
96
+
97
+ it "can be accessed with lowercase keys" do
98
+ @response.headers_hash['content-type'].should == 'text/html; charset=utf-8'
99
+ end
100
+
101
+ it "can parse the headers into a hash" do
102
+ @response.headers_hash["Status"].should == "200"
103
+ @response.headers_hash["Set-Cookie"].should == ["_some_session=BAh7CDoGciIAOg9zZXNzaW9uX2lkIiU1OTQ2OTcwMjljMWM5ZTQwODU1NjQwYTViMmQxMTkxMjoGcyIKL2NhcnQ%3D--b4c4663932243090c961bb93d4ad5e4327064730; path=/; HttpOnly", "foo=bar; path=/;"]
104
+ @response.headers_hash["Content-Type"].should == "text/html; charset=utf-8"
105
+ end
106
+
107
+ it 'parses the status message' do
108
+ @response.status_message.should == 'OK'
109
+ end
110
+
111
+ it 'parses the HTTP version' do
112
+ @response.http_version.should == '1.1'
113
+ end
114
+
115
+ it 'parses all header keys except HTTP version declaration' do
116
+ @response.headers_hash.keys.should =~ %w[
117
+ X-Powered-By
118
+ P3p
119
+ X-Cache
120
+ Etag
121
+ X-Runtime
122
+ Content-Type
123
+ Content-Length
124
+ Server
125
+ Set-Cookie
126
+ Cache-Control
127
+ Connection
128
+ Status
129
+ ]
130
+ end
131
+ end
132
+
133
+ it "parses a header key that appears multiple times into an array" do
134
+ response = Typhoeus::Response.new(:headers => "HTTP/1.1 302 Found\r\nContent-Type: text/html; charset=utf-8\r\nConnection: close\r\nStatus: 302\r\nX-Powered-By: Phusion Passenger (mod_rails/mod_rack) 2.2.9\r\nLocation: http://mckenzie-greenholt1512.myshopify.com/cart\r\nX-Runtime: 22\r\nCache-Control: no-cache\r\nContent-Length: 114\r\nSet-Cookie: cart=8fdd6a828d9c89a737a52668be0cebaf; path=/; expires=Fri, 12-Mar-2010 18:30:19 GMT\r\nSet-Cookie: _session=BAh7CToPc2Vzc2lvbl9pZCIlZTQzMDQzMDg1YjI3MTQ4MzAzMTZmMWZmMWJjMTU1NmI6CWNhcnQiJThmZGQ2YTgyOGQ5Yzg5YTczN2E1MjY2OGJlMGNlYmFmOgZyIgA6BnMiDi9jYXJ0L2FkZA%3D%3D--6b0a699625caed9597580d8e9b6ca5f5e5954125; path=/; HttpOnly\r\nServer: nginx/0.6.37 + Phusion Passenger 2.2.4 (mod_rails/mod_rack)\r\nP3P: CP=\"NOI DSP COR NID ADMa OPTa OUR NOR\"\r\n\r\n")
135
+ response.headers_hash["Set-Cookie"].should include("cart=8fdd6a828d9c89a737a52668be0cebaf; path=/; expires=Fri, 12-Mar-2010 18:30:19 GMT")
136
+ response.headers_hash["Set-Cookie"].should include("_session=BAh7CToPc2Vzc2lvbl9pZCIlZTQzMDQzMDg1YjI3MTQ4MzAzMTZmMWZmMWJjMTU1NmI6CWNhcnQiJThmZGQ2YTgyOGQ5Yzg5YTczN2E1MjY2OGJlMGNlYmFmOgZyIgA6BnMiDi9jYXJ0L2FkZA%3D%3D--6b0a699625caed9597580d8e9b6ca5f5e5954125; path=/; HttpOnly")
137
+ end
138
+ end
139
+
140
+ describe "status checking" do
141
+ it "is successful if response code is 200-299" do
142
+ Typhoeus::Response.new(:code => 220).success?.should be
143
+ Typhoeus::Response.new(:code => 400).success?.should_not be
144
+ end
145
+
146
+ it "is not modified if the status code is 304" do
147
+ Typhoeus::Response.new(:code => 304).modified?.should_not be
148
+ Typhoeus::Response.new(:code => 200).modified?.should be
149
+ end
150
+ end
151
+ end