typhoeus 1.1.0 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e844646bbdb137fea0fc50cf6c8d618ed1ef675
4
- data.tar.gz: f2eef78a13606b8c957cae48afea61fe1118117b
3
+ metadata.gz: c35cbb02b3e9bdcd193c09c248738a23acc50878
4
+ data.tar.gz: 19be15bdcbf9dc8204353f852aa49c7f4d9748eb
5
5
  SHA512:
6
- metadata.gz: 4e2cd0fdcdf92be70c916a75b95a8d3f6baf794e760c14f8dda7e0812ba965f686a9d720176e082e3cbe6b0cacd3013f79ea8f52deae7242f4728b44dc08b608
7
- data.tar.gz: a76443341afde890cc9bfc6feb4e350effc9f8be7f36c5f61eb25d9afd0fcbeba182c675aa7a09bfb3ba514c6269543e4f0d2e5c81f0958c4986e0d9d06a9cd8
6
+ metadata.gz: d0e87a6220258b47dca9057d3be3c127a1d9b3aca5e5cb15de66e87e5838d8d72c9d442be79159fb1e607a83087c575aebc3f9c10ef1cfb81f0215dc20bff0f3
7
+ data.tar.gz: 7e79adf1409d6276be2ba4d8424e8c21181e8d8cd5c92998b2b4df41477827f3f8938ad9d133e3a53e65459ffbfebf3f0eefa664e052b82661167ab8bc6fcbd6
@@ -13,12 +13,9 @@ rvm:
13
13
  - jruby-head
14
14
  - jruby-18mode
15
15
  - jruby-19mode
16
- - rbx # It's actually RBX 3
17
- - rbx-2
18
16
  matrix:
19
17
  fast_finish: true
20
18
  allow_failures:
21
19
  - rvm: ruby-head
22
20
  - rvm: jruby-head
23
21
  - rvm: ree
24
- - rvm: rbx
@@ -2,7 +2,11 @@
2
2
 
3
3
  ## Master
4
4
 
5
- [Full Changelog](http://github.com/typhoeus/typhoeus/compare/v1.1.0...master)
5
+ [Full Changelog](http://github.com/typhoeus/typhoeus/compare/v1.1.1...master)
6
+
7
+ ## 1.1.1
8
+
9
+ [Full Changelog](http://github.com/typhoeus/typhoeus/compare/v1.1.0...v1.1.1)
6
10
 
7
11
  ## 1.1.0
8
12
 
data/Gemfile CHANGED
@@ -3,7 +3,7 @@ gemspec
3
3
 
4
4
  if Gem.ruby_version < Gem::Version.new("2.0.0")
5
5
  gem "rake", "< 11"
6
- gem "json", "< 2"
6
+ gem "json", "< 2"
7
7
  else
8
8
  gem "json"
9
9
  gem "rake"
@@ -13,7 +13,13 @@ group :development, :test do
13
13
  gem "rspec", "~> 3.0"
14
14
 
15
15
  gem "sinatra", "~> 1.3"
16
- gem "faraday", ">= 0.9"
16
+
17
+ if Gem.ruby_version >= Gem::Version.new("1.9.0")
18
+ gem "faraday", ">= 0.9"
19
+ gem "dalli", "~> 2.0"
20
+ end
21
+
22
+ gem "redis", "~> 3.0"
17
23
 
18
24
  if RUBY_PLATFORM == "java"
19
25
  gem "spoon"
data/README.md CHANGED
@@ -307,39 +307,26 @@ Typhoeus.get("www.example.com").cached?
307
307
  For use with [Dalli](https://github.com/mperham/dalli):
308
308
 
309
309
  ```ruby
310
- class Cache
311
- def initialize
312
- @client = Dalli::Client.new
313
- end
314
-
315
- def get(request)
316
- @client.get(request.cache_key)
317
- end
318
-
319
- def set(request, response)
320
- @client.set(request.cache_key, response)
321
- end
322
- end
323
-
324
- Typhoeus::Config.cache = Cache.new
310
+ dalli = Dalli::Client.new(...)
311
+ Typhoeus::Config.cache = Typhoeus::Cache::Dalli.new(dalli)
325
312
  ```
326
313
 
327
314
  For use with Rails:
328
315
 
329
316
  ```ruby
330
- class Cache
331
- def get(request)
332
- Rails.cache.read(request)
333
- end
317
+ Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
318
+ ```
334
319
 
335
- def set(request, response)
336
- Rails.cache.write(request, response)
337
- end
338
- end
320
+ For use with [Redis](https://github.com/redis/redis-rb):
339
321
 
340
- Typhoeus::Config.cache = Cache.new
322
+ ```ruby
323
+ redis = Redis.new(...)
324
+ Typhoeus::Config.cache = Typhoeus::Cache::Redis.new(redis)
341
325
  ```
342
326
 
327
+ All three of these adapters take an optional keyword argument `default_ttl`, which sets a default
328
+ TTL on cached responses (in seconds), for requests which do not have a cache TTL set.
329
+
343
330
  ### Direct Stubbing
344
331
 
345
332
  Hydra allows you to stub out specific urls and patterns to avoid hitting
@@ -397,7 +384,7 @@ and [`connecttimeout`](http://curl.haxx.se/libcurl/c/curl_easy_setopt.html#CURLO
397
384
  `timeout` is the time limit for the entire request in seconds.
398
385
  `connecttimeout` is the time limit for just the connection phase, again in seconds.
399
386
 
400
- There are two additional more fine grained opptions `timeout_ms` and
387
+ There are two additional more fine grained options `timeout_ms` and
401
388
  `connecttimeout_ms`. These options offer millisecond precision but are not always available (for instance on linux if `nosignal` is not set to true).
402
389
 
403
390
  When you pass a floating point `timeout` (or `connecttimeout`) Typhoeus will set `timeout_ms` for you if it has not been defined. The actual timeout values passed to curl will always be rounded up.
@@ -17,6 +17,21 @@ if defined?(Rack)
17
17
  require "rack/typhoeus"
18
18
  end
19
19
 
20
+ # If the Redis gem is available, load the redis cache adapter
21
+ if defined?(Redis)
22
+ require "typhoeus/redis_cache"
23
+ end
24
+
25
+ # If the Dalli gem is available, load the Dalli cache adapter
26
+ if defined?(Dalli)
27
+ require "typhoeus/dalli_cache"
28
+ end
29
+
30
+ # If we are using Rails, load the Rails cache adapter
31
+ if defined?(Rails)
32
+ require "typhoeus/rails_cache"
33
+ end
34
+
20
35
  # If we are using Rails, then we will include the Typhoeus railtie.
21
36
  # if defined?(Rails)
22
37
  # require "typhoeus/railtie"
@@ -12,6 +12,9 @@ module Faraday # :nodoc:
12
12
  #
13
13
  # conn = Faraday.new(url: "www.example.com") do |faraday|
14
14
  # faraday.adapter :typhoeus
15
+ #
16
+ # # You can include Typhoeus options to be used for every request
17
+ # # faraday.adapter :typhoeus, forbid_reuse: true, maxredirs: 1
15
18
  # end
16
19
  #
17
20
  # response = conn.get("/")
@@ -32,6 +35,17 @@ module Faraday # :nodoc:
32
35
  remove_method :configure_socket if method_defined? :configure_socket
33
36
  remove_method :parallel? if method_defined? :parallel?
34
37
 
38
+ # Initialize the Typhoeus adapter
39
+ #
40
+ # @param [ App ] app Farday app
41
+ # @option [ Hash ] adapter_options Typhoeus options
42
+ #
43
+ # @return [ void ]
44
+ def initialize(app, adapter_options = {})
45
+ super(app)
46
+ @adapter_options = adapter_options
47
+ end
48
+
35
49
  # Setup Hydra with provided options.
36
50
  #
37
51
  # @example Setup Hydra.
@@ -72,12 +86,7 @@ module Faraday # :nodoc:
72
86
  def request(env)
73
87
  read_body env
74
88
 
75
- req = ::Typhoeus::Request.new(
76
- env[:url].to_s,
77
- :method => env[:method],
78
- :body => env[:body],
79
- :headers => env[:request_headers]
80
- )
89
+ req = typhoeus_request(env)
81
90
 
82
91
  configure_ssl req, env
83
92
  configure_proxy req, env
@@ -108,6 +117,16 @@ module Faraday # :nodoc:
108
117
  req
109
118
  end
110
119
 
120
+ def typhoeus_request(env)
121
+ opts = {
122
+ :method => env[:method],
123
+ :body => env[:body],
124
+ :headers => env[:request_headers]
125
+ }.merge(@adapter_options)
126
+
127
+ ::Typhoeus::Request.new(env[:url].to_s, opts)
128
+ end
129
+
111
130
  def read_body(env)
112
131
  env[:body] = env[:body].read if env[:body].respond_to? :read
113
132
  end
@@ -0,0 +1,26 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses using Dalli.
4
+ class Dalli
5
+ # @example Set Dalli as the Typhoeus cache backend
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Dalli.new
7
+ #
8
+ # @param [ Dalli::Client ] client
9
+ # A connection to the cache server. Defaults to `Dalli::Client.new`
10
+ # @param [ Integer ] default_ttl
11
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
12
+ def initialize(client = ::Dalli::Client.new, options = {})
13
+ @client = client
14
+ @default_ttl = options[:default_ttl]
15
+ end
16
+
17
+ def get(request)
18
+ @client.get(request.cache_key)
19
+ end
20
+
21
+ def set(request, response)
22
+ @client.set(request.cache_key, response, request.cache_ttl || @default_ttl)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,26 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses in using the Rails cache.
4
+ class Rails
5
+ # @example Use the Rails cache setup to cache Typhoeus responses.
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
7
+ #
8
+ # @param [ ActiveSupport::Cache::Store ] cache
9
+ # A Rails cache backend. Defaults to Rails.cache.
10
+ # @param [ Integer ] default_ttl
11
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
12
+ def initialize(cache = ::Rails.cache, options = {})
13
+ @cache = cache
14
+ @default_ttl = options[:default_ttl]
15
+ end
16
+
17
+ def get(request)
18
+ @cache.read(request)
19
+ end
20
+
21
+ def set(request, response)
22
+ @cache.write(request, response, :expires_in => request.cache_ttl || @default_ttl)
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ module Typhoeus
2
+ module Cache
3
+ # This module provides a simple way to cache HTTP responses in Redis.
4
+ class Redis
5
+ # @example Set Redis as the Typhoeus cache backend
6
+ # Typhoeus::Config.cache = Typhoeus::Cache::Redis.new
7
+ #
8
+ # @param [ Redis ] redis
9
+ # A connection to Redis. Defaults to `Redis.new`, which uses the
10
+ # `REDIS_URL` environment variable to connect
11
+ # @param [ Integer ] default_ttl
12
+ # The default TTL of cached responses in seconds, for requests which do not set a cache_ttl.
13
+ def initialize(redis = ::Redis.new, options = {})
14
+ @redis = redis
15
+ @default_ttl = options[:default_ttl]
16
+ end
17
+
18
+ def get(request)
19
+ serialized_response = @redis.get(request.cache_key)
20
+ return unless serialized_response
21
+ Marshal.load(serialized_response)
22
+ end
23
+
24
+ def set(request, response)
25
+ ttl = request.cache_ttl || @default_ttl
26
+ key = request.cache_key
27
+ serialized_response = Marshal.dump(response)
28
+ @redis.set(key, serialized_response)
29
+ @redis.expire(key, ttl) if ttl
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,5 +1,5 @@
1
1
  module Typhoeus
2
2
 
3
3
  # The current Typhoeus version.
4
- VERSION = '1.1.0'
4
+ VERSION = '1.1.1'
5
5
  end
@@ -1,323 +1,338 @@
1
- require 'spec_helper'
2
- require 'typhoeus/adapters/faraday'
3
-
4
- describe Faraday::Adapter::Typhoeus do
5
- let(:base_url) { "http://localhost:3001" }
6
- let(:adapter) { described_class.new }
7
- let(:request) { Typhoeus::Request.new(base_url) }
8
- let(:conn) do
9
- Faraday.new(:url => base_url) do |faraday|
10
- faraday.adapter :typhoeus
1
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
2
+ require 'spec_helper'
3
+ require 'typhoeus/adapters/faraday'
4
+
5
+ describe Faraday::Adapter::Typhoeus do
6
+ let(:base_url) { "http://localhost:3001" }
7
+ let(:adapter) { described_class.new(nil) }
8
+ let(:request) { Typhoeus::Request.new(base_url) }
9
+ let(:conn) do
10
+ Faraday.new(:url => base_url) do |faraday|
11
+ faraday.adapter :typhoeus
12
+ end
11
13
  end
12
- end
13
- let(:response) { conn.get("/") }
14
+ let(:response) { conn.get("/") }
14
15
 
15
- context "when parallel" do
16
- it "returns a faraday response" do
17
- response = nil
18
- conn.in_parallel { response = conn.get("/") }
19
- expect(response).to be_a(Faraday::Response)
20
- end
16
+ context "when parallel" do
17
+ it "returns a faraday response" do
18
+ response = nil
19
+ conn.in_parallel { response = conn.get("/") }
20
+ expect(response).to be_a(Faraday::Response)
21
+ end
21
22
 
22
- it "succeeds" do
23
- response = nil
24
- conn.in_parallel { response = conn.get("/") }
25
- expect(response.status).to be(200)
23
+ it "succeeds" do
24
+ response = nil
25
+ conn.in_parallel { response = conn.get("/") }
26
+ expect(response.status).to be(200)
27
+ end
26
28
  end
27
- end
28
29
 
29
- context "when not parallel" do
30
- it "returns a faraday response" do
31
- expect(response).to be_a(Faraday::Response)
32
- end
30
+ context "when not parallel" do
31
+ it "returns a faraday response" do
32
+ expect(response).to be_a(Faraday::Response)
33
+ end
33
34
 
34
- it "succeeds" do
35
- expect(response.status).to be(200)
35
+ it "succeeds" do
36
+ expect(response.status).to be(200)
37
+ end
36
38
  end
37
- end
38
39
 
39
- context "when a response is stubbed" do
40
- before do
41
- stub = Typhoeus::Response.new \
42
- :code => 200,
43
- :headers => { "Foo" => "2", "Bar" => "3" },
44
- :body => "Hello",
45
- :mock => true
40
+ context "when a response is stubbed" do
41
+ before do
42
+ stub = Typhoeus::Response.new \
43
+ :code => 200,
44
+ :headers => { "Foo" => "2", "Bar" => "3" },
45
+ :body => "Hello",
46
+ :mock => true
46
47
 
47
- Typhoeus.stub(base_url + '/').and_return(stub)
48
- end
48
+ Typhoeus.stub(base_url + '/').and_return(stub)
49
+ end
49
50
 
50
- it 'stubs the status code' do
51
- expect(response.status).to eq(200)
52
- end
51
+ it 'stubs the status code' do
52
+ expect(response.status).to eq(200)
53
+ end
53
54
 
54
- it 'stubs the response body' do
55
- expect(response.body).to eq("Hello")
56
- end
55
+ it 'stubs the response body' do
56
+ expect(response.body).to eq("Hello")
57
+ end
57
58
 
58
- it 'stubs the headers' do
59
- expect(response.headers).to eq("Foo" => "2", "Bar" => "3")
59
+ it 'stubs the headers' do
60
+ expect(response.headers).to eq("Foo" => "2", "Bar" => "3")
61
+ end
60
62
  end
61
- end
62
63
 
63
- describe "#perform_request" do
64
- let(:env) { {} }
64
+ describe "#initialize" do
65
+ let(:request) { adapter.method(:typhoeus_request).call({}) }
65
66
 
66
- context "when body" do
67
- let(:env) { { :body => double(:read => "body") } }
67
+ context "when typhoeus request options specified" do
68
+ let(:adapter) { described_class.new(nil, { :forbid_reuse => true, :maxredirs => 1 }) }
68
69
 
69
- it "reads body" do
70
- expect(adapter.method(:read_body).call(env)).to eq("body")
70
+ it "should set option for request" do
71
+ expect(request.options[:forbid_reuse]).to be_truthy
72
+ expect(request.options[:maxredirs]).to eq(1)
73
+ end
71
74
  end
72
75
  end
73
76
 
74
- context "parallel_manager" do
75
- context "when given" do
76
- let(:env) { { :parallel_manager => double(:queue => true), :ssl => {}, :request => {} } }
77
+ describe "#perform_request" do
78
+ let(:env) { {} }
79
+
80
+ context "when body" do
81
+ let(:env) { { :body => double(:read => "body") } }
77
82
 
78
- it "uses" do
79
- adapter.method(:perform_request).call(env)
83
+ it "reads body" do
84
+ expect(adapter.method(:read_body).call(env)).to eq("body")
80
85
  end
81
86
  end
82
87
 
83
- context "when not given" do
84
- let(:env) { { :method => :get, :ssl => {}, :request => {} } }
88
+ context "parallel_manager" do
89
+ context "when given" do
90
+ let(:env) { { :parallel_manager => double(:queue => true), :ssl => {}, :request => {} } }
91
+
92
+ it "uses" do
93
+ adapter.method(:perform_request).call(env)
94
+ end
95
+ end
96
+
97
+ context "when not given" do
98
+ let(:env) { { :method => :get, :ssl => {}, :request => {} } }
85
99
 
86
- it "falls back to single" do
87
- expect(Typhoeus::Request).to receive(:new).and_return(double(:options => {}, :on_complete => [], :run => true))
88
- adapter.method(:perform_request).call(env)
100
+ it "falls back to single" do
101
+ expect(Typhoeus::Request).to receive(:new).and_return(double(:options => {}, :on_complete => [], :run => true))
102
+ adapter.method(:perform_request).call(env)
103
+ end
89
104
  end
90
105
  end
91
106
  end
92
- end
93
107
 
94
- describe "#request" do
95
- let(:env) do
96
- { :url => "url", :method => :get, :body => "body", :request_headers => {}, :ssl => {}, :request => {} }
97
- end
108
+ describe "#request" do
109
+ let(:env) do
110
+ { :url => "url", :method => :get, :body => "body", :request_headers => {}, :ssl => {}, :request => {} }
111
+ end
98
112
 
99
- let(:request) { adapter.method(:request).call(env) }
113
+ let(:request) { adapter.method(:request).call(env) }
100
114
 
101
- it "returns request" do
102
- expect(request).to be_a(Typhoeus::Request)
103
- end
115
+ it "returns request" do
116
+ expect(request).to be_a(Typhoeus::Request)
117
+ end
104
118
 
105
- it "sets url" do
106
- expect(request.base_url).to eq("url")
107
- end
119
+ it "sets url" do
120
+ expect(request.base_url).to eq("url")
121
+ end
108
122
 
109
- it "sets http method" do
110
- expect(request.original_options[:method]).to eq(:get)
111
- end
123
+ it "sets http method" do
124
+ expect(request.original_options[:method]).to eq(:get)
125
+ end
112
126
 
113
- it "sets body" do
114
- expect(request.original_options[:body]).to eq("body")
115
- end
127
+ it "sets body" do
128
+ expect(request.original_options[:body]).to eq("body")
129
+ end
116
130
 
117
- it "sets headers" do
118
- expect(request.original_options[:headers]).to eq({})
119
- end
131
+ it "sets headers" do
132
+ expect(request.original_options[:headers]).to eq({})
133
+ end
120
134
 
121
- it "sets on_complete callback" do
122
- expect(request.on_complete.size).to eq(1)
135
+ it "sets on_complete callback" do
136
+ expect(request.on_complete.size).to eq(1)
137
+ end
123
138
  end
124
- end
125
139
 
126
- context "when the connection failed" do
127
- before do
128
- stub = Typhoeus::Response.new \
140
+ context "when the connection failed" do
141
+ before do
142
+ stub = Typhoeus::Response.new \
129
143
  :response_code => 0,
130
144
  :return_code => 0,
131
145
  :mock => true
132
146
 
133
- Typhoeus.stub(base_url + '/').and_return(stub)
134
- end
135
-
136
- context "when parallel" do
137
- it "isn't successful" do
138
- response = nil
139
- conn.in_parallel { response = conn.get("/") }
140
- expect(response.success?).to be_falsey
147
+ Typhoeus.stub(base_url + '/').and_return(stub)
141
148
  end
142
149
 
143
- it "translates the response code into an error message" do
144
- response = nil
145
- conn.in_parallel { response = conn.get("/") }
146
- expect(response.env[:typhoeus_return_message]).to eq("No error")
150
+ context "when parallel" do
151
+ it "isn't successful" do
152
+ response = nil
153
+ conn.in_parallel { response = conn.get("/") }
154
+ expect(response.success?).to be_falsey
155
+ end
156
+
157
+ it "translates the response code into an error message" do
158
+ response = nil
159
+ conn.in_parallel { response = conn.get("/") }
160
+ expect(response.env[:typhoeus_return_message]).to eq("No error")
161
+ end
147
162
  end
148
- end
149
163
 
150
- context "when not parallel" do
151
- it "raises an error" do
152
- expect { conn.get("/") }.to raise_error(Faraday::Error::ConnectionFailed, "No error")
164
+ context "when not parallel" do
165
+ it "raises an error" do
166
+ expect { conn.get("/") }.to raise_error(Faraday::Error::ConnectionFailed, "No error")
167
+ end
153
168
  end
154
169
  end
155
- end
156
170
 
157
- describe "#configure_socket" do
158
- let(:env) { { :request => { :bind => { :host => "interface" } } } }
171
+ describe "#configure_socket" do
172
+ let(:env) { { :request => { :bind => { :host => "interface" } } } }
159
173
 
160
- before { adapter.method(:configure_socket).call(request, env) }
174
+ before { adapter.method(:configure_socket).call(request, env) }
161
175
 
162
- context "when host" do
163
- it "sets interface" do
164
- expect(request.options[:interface]).to eq("interface")
176
+ context "when host" do
177
+ it "sets interface" do
178
+ expect(request.options[:interface]).to eq("interface")
179
+ end
165
180
  end
166
181
  end
167
- end
168
182
 
169
- describe "#configure_timeout" do
170
- before { adapter.method(:configure_timeout).call(request, env) }
183
+ describe "#configure_timeout" do
184
+ before { adapter.method(:configure_timeout).call(request, env) }
171
185
 
172
- context "when timeout" do
173
- let(:env) { { :request => { :timeout => 1 } } }
186
+ context "when timeout" do
187
+ let(:env) { { :request => { :timeout => 1 } } }
174
188
 
175
- it "sets timeout_ms" do
176
- expect(request.options[:timeout_ms]).to eq(1000)
189
+ it "sets timeout_ms" do
190
+ expect(request.options[:timeout_ms]).to eq(1000)
191
+ end
177
192
  end
178
- end
179
193
 
180
- context "when open_timeout" do
181
- let(:env) { { :request => { :open_timeout => 1 } } }
194
+ context "when open_timeout" do
195
+ let(:env) { { :request => { :open_timeout => 1 } } }
182
196
 
183
- it "sets connecttimeout_ms" do
184
- expect(request.options[:connecttimeout_ms]).to eq(1000)
197
+ it "sets connecttimeout_ms" do
198
+ expect(request.options[:connecttimeout_ms]).to eq(1000)
199
+ end
185
200
  end
186
201
  end
187
- end
188
-
189
- describe "#configure_proxy" do
190
- before { adapter.method(:configure_proxy).call(request, env) }
191
202
 
192
- context "when proxy" do
193
- let(:env) { { :request => { :proxy => { :uri => double(:scheme => 'http', :host => "localhost", :port => "3001") } } } }
203
+ describe "#configure_proxy" do
204
+ before { adapter.method(:configure_proxy).call(request, env) }
194
205
 
195
- it "sets proxy" do
196
- expect(request.options[:proxy]).to eq("http://localhost:3001")
197
- end
206
+ context "when proxy" do
207
+ let(:env) { { :request => { :proxy => { :uri => double(:scheme => 'http', :host => "localhost", :port => "3001") } } } }
198
208
 
199
- context "when username and password" do
200
- let(:env) do
201
- { :request => { :proxy => {
202
- :uri => double(:scheme => 'http', :host => :a, :port => :b),
203
- :user => "a",
204
- :password => "b"
205
- } } }
209
+ it "sets proxy" do
210
+ expect(request.options[:proxy]).to eq("http://localhost:3001")
206
211
  end
207
212
 
208
- it "sets proxyuserpwd" do
209
- expect(request.options[:proxyuserpwd]).to eq("a:b")
213
+ context "when username and password" do
214
+ let(:env) do
215
+ { :request => { :proxy => {
216
+ :uri => double(:scheme => 'http', :host => :a, :port => :b),
217
+ :user => "a",
218
+ :password => "b"
219
+ } } }
220
+ end
221
+
222
+ it "sets proxyuserpwd" do
223
+ expect(request.options[:proxyuserpwd]).to eq("a:b")
224
+ end
210
225
  end
211
226
  end
212
227
  end
213
- end
214
228
 
215
- describe "#configure_ssl" do
216
- before { adapter.method(:configure_ssl).call(request, env) }
229
+ describe "#configure_ssl" do
230
+ before { adapter.method(:configure_ssl).call(request, env) }
217
231
 
218
- context "when version" do
219
- let(:env) { { :ssl => { :version => "a" } } }
232
+ context "when version" do
233
+ let(:env) { { :ssl => { :version => "a" } } }
220
234
 
221
- it "sets sslversion" do
222
- expect(request.options[:sslversion]).to eq("a")
235
+ it "sets sslversion" do
236
+ expect(request.options[:sslversion]).to eq("a")
237
+ end
223
238
  end
224
- end
225
239
 
226
- context "when client_cert" do
227
- let(:env) { { :ssl => { :client_cert => "a" } } }
240
+ context "when client_cert" do
241
+ let(:env) { { :ssl => { :client_cert => "a" } } }
228
242
 
229
- it "sets sslcert" do
230
- expect(request.options[:sslcert]).to eq("a")
243
+ it "sets sslcert" do
244
+ expect(request.options[:sslcert]).to eq("a")
245
+ end
231
246
  end
232
- end
233
247
 
234
- context "when client_key" do
235
- let(:env) { { :ssl => { :client_key => "a" } } }
248
+ context "when client_key" do
249
+ let(:env) { { :ssl => { :client_key => "a" } } }
236
250
 
237
- it "sets sslkey" do
238
- expect(request.options[:sslkey]).to eq("a")
251
+ it "sets sslkey" do
252
+ expect(request.options[:sslkey]).to eq("a")
253
+ end
239
254
  end
240
- end
241
255
 
242
- context "when ca_file" do
243
- let(:env) { { :ssl => { :ca_file => "a" } } }
256
+ context "when ca_file" do
257
+ let(:env) { { :ssl => { :ca_file => "a" } } }
244
258
 
245
- it "sets cainfo" do
246
- expect(request.options[:cainfo]).to eq("a")
259
+ it "sets cainfo" do
260
+ expect(request.options[:cainfo]).to eq("a")
261
+ end
247
262
  end
248
- end
249
263
 
250
- context "when ca_path" do
251
- let(:env) { { :ssl => { :ca_path => "a" } } }
264
+ context "when ca_path" do
265
+ let(:env) { { :ssl => { :ca_path => "a" } } }
252
266
 
253
- it "sets capath" do
254
- expect(request.options[:capath]).to eq("a")
267
+ it "sets capath" do
268
+ expect(request.options[:capath]).to eq("a")
269
+ end
255
270
  end
256
- end
257
271
 
258
- context "when client_cert_passwd" do
259
- let(:env) { { :ssl => { :client_cert_passwd => "a" } } }
272
+ context "when client_cert_passwd" do
273
+ let(:env) { { :ssl => { :client_cert_passwd => "a" } } }
260
274
 
261
- it "sets keypasswd to the value of client_cert_passwd" do
262
- expect(request.options[:keypasswd]).to eq("a")
275
+ it "sets keypasswd to the value of client_cert_passwd" do
276
+ expect(request.options[:keypasswd]).to eq("a")
277
+ end
263
278
  end
264
- end
265
279
 
266
- context "when client_certificate_password" do
267
- let(:env) { { :ssl => { :client_certificate_password => "a" } } }
280
+ context "when client_certificate_password" do
281
+ let(:env) { { :ssl => { :client_certificate_password => "a" } } }
268
282
 
269
- it "sets keypasswd to the value of client_cert_passwd" do
270
- expect(request.options[:keypasswd]).to eq("a")
283
+ it "sets keypasswd to the value of client_cert_passwd" do
284
+ expect(request.options[:keypasswd]).to eq("a")
285
+ end
271
286
  end
272
- end
273
287
 
274
- context "when no client_cert_passwd" do
275
- let(:env) { { :ssl => { } } }
288
+ context "when no client_cert_passwd" do
289
+ let(:env) { { :ssl => { } } }
276
290
 
277
- it "does not set keypasswd on options" do
278
- expect(request.options).not_to have_key :keypasswd
291
+ it "does not set keypasswd on options" do
292
+ expect(request.options).not_to have_key :keypasswd
293
+ end
279
294
  end
280
- end
281
295
 
282
- context "when verify is false" do
283
- let(:env) { { :ssl => { :verify => false } } }
296
+ context "when verify is false" do
297
+ let(:env) { { :ssl => { :verify => false } } }
284
298
 
285
- it "sets ssl_verifyhost to 0" do
286
- expect(request.options[:ssl_verifyhost]).to eq(0)
287
- end
299
+ it "sets ssl_verifyhost to 0" do
300
+ expect(request.options[:ssl_verifyhost]).to eq(0)
301
+ end
288
302
 
289
- it "sets ssl_verifypeer to false" do
290
- expect(request.options[:ssl_verifypeer]).to be_falsey
303
+ it "sets ssl_verifypeer to false" do
304
+ expect(request.options[:ssl_verifypeer]).to be_falsey
305
+ end
291
306
  end
292
- end
293
307
 
294
- context "when verify is true" do
295
- let(:env) { { :ssl => { :verify => true } } }
308
+ context "when verify is true" do
309
+ let(:env) { { :ssl => { :verify => true } } }
296
310
 
297
- it "sets ssl_verifyhost to 2" do
298
- expect(request.options[:ssl_verifyhost]).to eq(2)
299
- end
311
+ it "sets ssl_verifyhost to 2" do
312
+ expect(request.options[:ssl_verifyhost]).to eq(2)
313
+ end
300
314
 
301
- it "sets ssl_verifypeer to true" do
302
- expect(request.options[:ssl_verifypeer]).to be_truthy
315
+ it "sets ssl_verifypeer to true" do
316
+ expect(request.options[:ssl_verifypeer]).to be_truthy
317
+ end
303
318
  end
304
319
  end
305
- end
306
320
 
307
- describe "#parallel?" do
308
- context "when parallel_manager" do
309
- let(:env) { { :parallel_manager => true } }
321
+ describe "#parallel?" do
322
+ context "when parallel_manager" do
323
+ let(:env) { { :parallel_manager => true } }
310
324
 
311
- it "returns true" do
312
- expect(adapter.method(:parallel?).call(env)).to be_truthy
325
+ it "returns true" do
326
+ expect(adapter.method(:parallel?).call(env)).to be_truthy
327
+ end
313
328
  end
314
- end
315
329
 
316
- context "when no parallel_manager" do
317
- let(:env) { { :parallel_manager => nil } }
330
+ context "when no parallel_manager" do
331
+ let(:env) { { :parallel_manager => nil } }
318
332
 
319
- it "returns false" do
320
- expect(adapter.method(:parallel?).call(env)).to be_falsey
333
+ it "returns false" do
334
+ expect(adapter.method(:parallel?).call(env)).to be_falsey
335
+ end
321
336
  end
322
337
  end
323
338
  end
@@ -0,0 +1,41 @@
1
+ if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("1.9.0")
2
+ require 'dalli'
3
+ require 'typhoeus/cache/dalli'
4
+ require 'spec_helper'
5
+
6
+ describe Typhoeus::Cache::Dalli do
7
+ let(:dalli) { instance_double(Dalli::Client) }
8
+ let(:cache) { Typhoeus::Cache::Dalli.new(dalli) }
9
+
10
+ let(:base_url) { "localhost:3001" }
11
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
12
+ let(:response) { Typhoeus::Response.new(:response_code => 0, :return_code => 0, :mock => true) }
13
+
14
+ describe "#set" do
15
+ it "sends the request to Dalli" do
16
+ expect(dalli).to receive(:set).with(request.cache_key, response, nil)
17
+
18
+ cache.set(request, response)
19
+ end
20
+ end
21
+
22
+ describe "#get" do
23
+ it "returns nil when the key is not in the cache" do
24
+ expect(dalli).to receive(:get).with(request.cache_key).and_return(nil)
25
+
26
+ expect(cache.get(request)).to be_nil
27
+ end
28
+
29
+ it "returns the cached response when the key is in cache" do
30
+ expect(dalli).to receive(:get).with(request.cache_key).and_return(response)
31
+
32
+ result = cache.get(request)
33
+ expect(result).to_not be_nil
34
+ expect(result.response_code).to eq(response.response_code)
35
+ expect(result.return_code).to eq(response.return_code)
36
+ expect(result.headers).to eq(response.headers)
37
+ expect(result.body).to eq(response.body)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require 'redis'
2
+ require 'typhoeus/cache/redis'
3
+ require 'spec_helper'
4
+
5
+ describe Typhoeus::Cache::Redis do
6
+ let(:redis) { instance_double(Redis) }
7
+ let(:cache) { Typhoeus::Cache::Redis.new(redis) }
8
+
9
+ let(:base_url) { "localhost:3001" }
10
+ let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
11
+ let(:response) { Typhoeus::Response.new(:response_code => 0, :return_code => 0, :mock => true) }
12
+ let(:serialized_response) { Marshal.dump(response) }
13
+
14
+ describe "#set" do
15
+ it "sends the serialized request to Redis" do
16
+ expect(redis).to receive(:set).with(request.cache_key, serialized_response)
17
+ expect(redis).to_not receive(:expire).with(request.cache_key, request.cache_ttl)
18
+
19
+ cache.set(request, response)
20
+ end
21
+ end
22
+
23
+ describe "#get" do
24
+ it "returns nil when the key is not in Redis" do
25
+ expect(redis).to receive(:get).with(request.cache_key).and_return(nil)
26
+
27
+ expect(cache.get(request)).to be_nil
28
+ end
29
+
30
+ it "returns the cached response when the key is in Redis" do
31
+ expect(redis).to receive(:get).with(request.cache_key).and_return(serialized_response)
32
+
33
+ result = cache.get(request)
34
+ expect(result).to_not be_nil
35
+ expect(result.response_code).to eq(response.response_code)
36
+ expect(result.return_code).to eq(response.return_code)
37
+ expect(result.headers).to eq(response.headers)
38
+ expect(result.body).to eq(response.body)
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typhoeus
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Balatero
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-07-21 00:00:00.000000000 Z
13
+ date: 2016-12-02 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ethon
@@ -50,6 +50,9 @@ files:
50
50
  - lib/rack/typhoeus/middleware/params_decoder/helper.rb
51
51
  - lib/typhoeus.rb
52
52
  - lib/typhoeus/adapters/faraday.rb
53
+ - lib/typhoeus/cache/dalli.rb
54
+ - lib/typhoeus/cache/rails.rb
55
+ - lib/typhoeus/cache/redis.rb
53
56
  - lib/typhoeus/config.rb
54
57
  - lib/typhoeus/easy_factory.rb
55
58
  - lib/typhoeus/errors.rb
@@ -94,6 +97,8 @@ files:
94
97
  - spec/support/memory_cache.rb
95
98
  - spec/support/server.rb
96
99
  - spec/typhoeus/adapters/faraday_spec.rb
100
+ - spec/typhoeus/cache/dalli_spec.rb
101
+ - spec/typhoeus/cache/redis_spec.rb
97
102
  - spec/typhoeus/config_spec.rb
98
103
  - spec/typhoeus/easy_factory_spec.rb
99
104
  - spec/typhoeus/errors/no_stub_spec.rb
@@ -157,6 +162,8 @@ test_files:
157
162
  - spec/support/memory_cache.rb
158
163
  - spec/support/server.rb
159
164
  - spec/typhoeus/adapters/faraday_spec.rb
165
+ - spec/typhoeus/cache/dalli_spec.rb
166
+ - spec/typhoeus/cache/redis_spec.rb
160
167
  - spec/typhoeus/config_spec.rb
161
168
  - spec/typhoeus/easy_factory_spec.rb
162
169
  - spec/typhoeus/errors/no_stub_spec.rb