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 +4 -4
- data/.travis.yml +0 -3
- data/CHANGELOG.md +5 -1
- data/Gemfile +8 -2
- data/README.md +12 -25
- data/lib/typhoeus.rb +15 -0
- data/lib/typhoeus/adapters/faraday.rb +25 -6
- data/lib/typhoeus/cache/dalli.rb +26 -0
- data/lib/typhoeus/cache/rails.rb +26 -0
- data/lib/typhoeus/cache/redis.rb +33 -0
- data/lib/typhoeus/version.rb +1 -1
- data/spec/typhoeus/adapters/faraday_spec.rb +228 -213
- data/spec/typhoeus/cache/dalli_spec.rb +41 -0
- data/spec/typhoeus/cache/redis_spec.rb +41 -0
- metadata +9 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c35cbb02b3e9bdcd193c09c248738a23acc50878
|
4
|
+
data.tar.gz: 19be15bdcbf9dc8204353f852aa49c7f4d9748eb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0e87a6220258b47dca9057d3be3c127a1d9b3aca5e5cb15de66e87e5838d8d72c9d442be79159fb1e607a83087c575aebc3f9c10ef1cfb81f0215dc20bff0f3
|
7
|
+
data.tar.gz: 7e79adf1409d6276be2ba4d8424e8c21181e8d8cd5c92998b2b4df41477827f3f8938ad9d133e3a53e65459ffbfebf3f0eefa664e052b82661167ab8bc6fcbd6
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
CHANGED
@@ -2,7 +2,11 @@
|
|
2
2
|
|
3
3
|
## Master
|
4
4
|
|
5
|
-
[Full Changelog](http://github.com/typhoeus/typhoeus/compare/v1.1.
|
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
|
-
|
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
|
-
|
311
|
-
|
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
|
-
|
331
|
-
|
332
|
-
Rails.cache.read(request)
|
333
|
-
end
|
317
|
+
Typhoeus::Config.cache = Typhoeus::Cache::Rails.new
|
318
|
+
```
|
334
319
|
|
335
|
-
|
336
|
-
Rails.cache.write(request, response)
|
337
|
-
end
|
338
|
-
end
|
320
|
+
For use with [Redis](https://github.com/redis/redis-rb):
|
339
321
|
|
340
|
-
|
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
|
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.
|
data/lib/typhoeus.rb
CHANGED
@@ -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 =
|
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
|
data/lib/typhoeus/version.rb
CHANGED
@@ -1,323 +1,338 @@
|
|
1
|
-
|
2
|
-
require '
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
let(:response) { conn.get("/") }
|
14
|
+
let(:response) { conn.get("/") }
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
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
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
35
|
-
|
35
|
+
it "succeeds" do
|
36
|
+
expect(response.status).to be(200)
|
37
|
+
end
|
36
38
|
end
|
37
|
-
end
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
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
|
-
|
48
|
-
|
48
|
+
Typhoeus.stub(base_url + '/').and_return(stub)
|
49
|
+
end
|
49
50
|
|
50
|
-
|
51
|
-
|
52
|
-
|
51
|
+
it 'stubs the status code' do
|
52
|
+
expect(response.status).to eq(200)
|
53
|
+
end
|
53
54
|
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
it 'stubs the response body' do
|
56
|
+
expect(response.body).to eq("Hello")
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
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
|
-
|
64
|
-
|
64
|
+
describe "#initialize" do
|
65
|
+
let(:request) { adapter.method(:typhoeus_request).call({}) }
|
65
66
|
|
66
|
-
|
67
|
-
|
67
|
+
context "when typhoeus request options specified" do
|
68
|
+
let(:adapter) { described_class.new(nil, { :forbid_reuse => true, :maxredirs => 1 }) }
|
68
69
|
|
69
|
-
|
70
|
-
|
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
|
-
|
75
|
-
|
76
|
-
|
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 "
|
79
|
-
adapter.method(:
|
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 "
|
84
|
-
|
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
|
-
|
87
|
-
|
88
|
-
|
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
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
108
|
+
describe "#request" do
|
109
|
+
let(:env) do
|
110
|
+
{ :url => "url", :method => :get, :body => "body", :request_headers => {}, :ssl => {}, :request => {} }
|
111
|
+
end
|
98
112
|
|
99
|
-
|
113
|
+
let(:request) { adapter.method(:request).call(env) }
|
100
114
|
|
101
|
-
|
102
|
-
|
103
|
-
|
115
|
+
it "returns request" do
|
116
|
+
expect(request).to be_a(Typhoeus::Request)
|
117
|
+
end
|
104
118
|
|
105
|
-
|
106
|
-
|
107
|
-
|
119
|
+
it "sets url" do
|
120
|
+
expect(request.base_url).to eq("url")
|
121
|
+
end
|
108
122
|
|
109
|
-
|
110
|
-
|
111
|
-
|
123
|
+
it "sets http method" do
|
124
|
+
expect(request.original_options[:method]).to eq(:get)
|
125
|
+
end
|
112
126
|
|
113
|
-
|
114
|
-
|
115
|
-
|
127
|
+
it "sets body" do
|
128
|
+
expect(request.original_options[:body]).to eq("body")
|
129
|
+
end
|
116
130
|
|
117
|
-
|
118
|
-
|
119
|
-
|
131
|
+
it "sets headers" do
|
132
|
+
expect(request.original_options[:headers]).to eq({})
|
133
|
+
end
|
120
134
|
|
121
|
-
|
122
|
-
|
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
|
-
|
127
|
-
|
128
|
-
|
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
|
-
|
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
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
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
|
-
|
151
|
-
|
152
|
-
|
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
|
-
|
158
|
-
|
171
|
+
describe "#configure_socket" do
|
172
|
+
let(:env) { { :request => { :bind => { :host => "interface" } } } }
|
159
173
|
|
160
|
-
|
174
|
+
before { adapter.method(:configure_socket).call(request, env) }
|
161
175
|
|
162
|
-
|
163
|
-
|
164
|
-
|
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
|
-
|
170
|
-
|
183
|
+
describe "#configure_timeout" do
|
184
|
+
before { adapter.method(:configure_timeout).call(request, env) }
|
171
185
|
|
172
|
-
|
173
|
-
|
186
|
+
context "when timeout" do
|
187
|
+
let(:env) { { :request => { :timeout => 1 } } }
|
174
188
|
|
175
|
-
|
176
|
-
|
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
|
-
|
181
|
-
|
194
|
+
context "when open_timeout" do
|
195
|
+
let(:env) { { :request => { :open_timeout => 1 } } }
|
182
196
|
|
183
|
-
|
184
|
-
|
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
|
-
|
193
|
-
|
203
|
+
describe "#configure_proxy" do
|
204
|
+
before { adapter.method(:configure_proxy).call(request, env) }
|
194
205
|
|
195
|
-
|
196
|
-
|
197
|
-
end
|
206
|
+
context "when proxy" do
|
207
|
+
let(:env) { { :request => { :proxy => { :uri => double(:scheme => 'http', :host => "localhost", :port => "3001") } } } }
|
198
208
|
|
199
|
-
|
200
|
-
|
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
|
-
|
209
|
-
|
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
|
-
|
216
|
-
|
229
|
+
describe "#configure_ssl" do
|
230
|
+
before { adapter.method(:configure_ssl).call(request, env) }
|
217
231
|
|
218
|
-
|
219
|
-
|
232
|
+
context "when version" do
|
233
|
+
let(:env) { { :ssl => { :version => "a" } } }
|
220
234
|
|
221
|
-
|
222
|
-
|
235
|
+
it "sets sslversion" do
|
236
|
+
expect(request.options[:sslversion]).to eq("a")
|
237
|
+
end
|
223
238
|
end
|
224
|
-
end
|
225
239
|
|
226
|
-
|
227
|
-
|
240
|
+
context "when client_cert" do
|
241
|
+
let(:env) { { :ssl => { :client_cert => "a" } } }
|
228
242
|
|
229
|
-
|
230
|
-
|
243
|
+
it "sets sslcert" do
|
244
|
+
expect(request.options[:sslcert]).to eq("a")
|
245
|
+
end
|
231
246
|
end
|
232
|
-
end
|
233
247
|
|
234
|
-
|
235
|
-
|
248
|
+
context "when client_key" do
|
249
|
+
let(:env) { { :ssl => { :client_key => "a" } } }
|
236
250
|
|
237
|
-
|
238
|
-
|
251
|
+
it "sets sslkey" do
|
252
|
+
expect(request.options[:sslkey]).to eq("a")
|
253
|
+
end
|
239
254
|
end
|
240
|
-
end
|
241
255
|
|
242
|
-
|
243
|
-
|
256
|
+
context "when ca_file" do
|
257
|
+
let(:env) { { :ssl => { :ca_file => "a" } } }
|
244
258
|
|
245
|
-
|
246
|
-
|
259
|
+
it "sets cainfo" do
|
260
|
+
expect(request.options[:cainfo]).to eq("a")
|
261
|
+
end
|
247
262
|
end
|
248
|
-
end
|
249
263
|
|
250
|
-
|
251
|
-
|
264
|
+
context "when ca_path" do
|
265
|
+
let(:env) { { :ssl => { :ca_path => "a" } } }
|
252
266
|
|
253
|
-
|
254
|
-
|
267
|
+
it "sets capath" do
|
268
|
+
expect(request.options[:capath]).to eq("a")
|
269
|
+
end
|
255
270
|
end
|
256
|
-
end
|
257
271
|
|
258
|
-
|
259
|
-
|
272
|
+
context "when client_cert_passwd" do
|
273
|
+
let(:env) { { :ssl => { :client_cert_passwd => "a" } } }
|
260
274
|
|
261
|
-
|
262
|
-
|
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
|
-
|
267
|
-
|
280
|
+
context "when client_certificate_password" do
|
281
|
+
let(:env) { { :ssl => { :client_certificate_password => "a" } } }
|
268
282
|
|
269
|
-
|
270
|
-
|
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
|
-
|
275
|
-
|
288
|
+
context "when no client_cert_passwd" do
|
289
|
+
let(:env) { { :ssl => { } } }
|
276
290
|
|
277
|
-
|
278
|
-
|
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
|
-
|
283
|
-
|
296
|
+
context "when verify is false" do
|
297
|
+
let(:env) { { :ssl => { :verify => false } } }
|
284
298
|
|
285
|
-
|
286
|
-
|
287
|
-
|
299
|
+
it "sets ssl_verifyhost to 0" do
|
300
|
+
expect(request.options[:ssl_verifyhost]).to eq(0)
|
301
|
+
end
|
288
302
|
|
289
|
-
|
290
|
-
|
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
|
-
|
295
|
-
|
308
|
+
context "when verify is true" do
|
309
|
+
let(:env) { { :ssl => { :verify => true } } }
|
296
310
|
|
297
|
-
|
298
|
-
|
299
|
-
|
311
|
+
it "sets ssl_verifyhost to 2" do
|
312
|
+
expect(request.options[:ssl_verifyhost]).to eq(2)
|
313
|
+
end
|
300
314
|
|
301
|
-
|
302
|
-
|
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
|
-
|
308
|
-
|
309
|
-
|
321
|
+
describe "#parallel?" do
|
322
|
+
context "when parallel_manager" do
|
323
|
+
let(:env) { { :parallel_manager => true } }
|
310
324
|
|
311
|
-
|
312
|
-
|
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
|
-
|
317
|
-
|
330
|
+
context "when no parallel_manager" do
|
331
|
+
let(:env) { { :parallel_manager => nil } }
|
318
332
|
|
319
|
-
|
320
|
-
|
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.
|
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-
|
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
|