typhoeus 1.0.2 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +11 -8
  3. data/CHANGELOG.md +29 -1
  4. data/CONTRIBUTING.md +4 -0
  5. data/Gemfile +10 -3
  6. data/README.md +66 -41
  7. data/lib/typhoeus/adapters/faraday.rb +29 -9
  8. data/lib/typhoeus/cache/dalli.rb +28 -0
  9. data/lib/typhoeus/cache/rails.rb +28 -0
  10. data/lib/typhoeus/cache/redis.rb +35 -0
  11. data/lib/typhoeus/config.rb +8 -1
  12. data/lib/typhoeus/easy_factory.rb +7 -2
  13. data/lib/typhoeus/hydra/cacheable.rb +1 -1
  14. data/lib/typhoeus/request/actions.rb +7 -7
  15. data/lib/typhoeus/request/cacheable.rb +14 -3
  16. data/lib/typhoeus/request/callbacks.rb +21 -3
  17. data/lib/typhoeus/request/marshal.rb +2 -2
  18. data/lib/typhoeus/request/streamable.rb +1 -1
  19. data/lib/typhoeus/request.rb +2 -0
  20. data/lib/typhoeus/response/header.rb +12 -5
  21. data/lib/typhoeus/response/informations.rb +7 -3
  22. data/lib/typhoeus/response/status.rb +22 -2
  23. data/lib/typhoeus/response.rb +1 -1
  24. data/lib/typhoeus/version.rb +1 -1
  25. data/lib/typhoeus.rb +19 -3
  26. data/spec/typhoeus/adapters/faraday_spec.rb +237 -191
  27. data/spec/typhoeus/cache/dalli_spec.rb +41 -0
  28. data/spec/typhoeus/cache/redis_spec.rb +41 -0
  29. data/spec/typhoeus/config_spec.rb +1 -1
  30. data/spec/typhoeus/easy_factory_spec.rb +6 -0
  31. data/spec/typhoeus/hydra/cacheable_spec.rb +31 -1
  32. data/spec/typhoeus/pool_spec.rb +4 -2
  33. data/spec/typhoeus/request/cacheable_spec.rb +24 -0
  34. data/spec/typhoeus/request/callbacks_spec.rb +2 -2
  35. data/spec/typhoeus/request/marshal_spec.rb +1 -1
  36. data/spec/typhoeus/request_spec.rb +21 -3
  37. data/spec/typhoeus/response/header_spec.rb +51 -1
  38. data/spec/typhoeus/response/informations_spec.rb +12 -1
  39. data/spec/typhoeus/response/status_spec.rb +54 -0
  40. metadata +10 -3
@@ -4,6 +4,7 @@ describe Typhoeus::Hydra::Cacheable do
4
4
  let(:base_url) { "localhost:3001" }
5
5
  let(:hydra) { Typhoeus::Hydra.new() }
6
6
  let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
7
+ let(:response) { Typhoeus::Response.new }
7
8
  let(:cache) { MemoryCache.new }
8
9
 
9
10
  describe "add" do
@@ -24,7 +25,6 @@ describe Typhoeus::Hydra::Cacheable do
24
25
  end
25
26
 
26
27
  context "when request in memory" do
27
- let(:response) { Typhoeus::Response.new }
28
28
  before { cache.memory[request] = response }
29
29
 
30
30
  it "returns response with cached status" do
@@ -53,6 +53,36 @@ describe Typhoeus::Hydra::Cacheable do
53
53
  end
54
54
  end
55
55
  end
56
+
57
+ context "when cache is specified on a request" do
58
+ before { Typhoeus::Config.cache = false }
59
+
60
+ context "when cache is false" do
61
+ let(:non_cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => false}) }
62
+
63
+ it "initiates an HTTP call" do
64
+ expect(Typhoeus::EasyFactory).to receive(:new).with(non_cached_request, hydra).and_call_original
65
+
66
+ hydra.add(non_cached_request)
67
+ end
68
+ end
69
+
70
+ context "when cache is defined" do
71
+ let(:cached_request) { Typhoeus::Request.new(base_url, {:method => :get, :cache => cache}) }
72
+
73
+ before { cache.memory[cached_request] = response }
74
+
75
+ it "uses the cache instead of making a new request" do
76
+ expect(Typhoeus::EasyFactory).not_to receive(:new)
77
+
78
+ hydra.add(cached_request)
79
+
80
+ expect(cached_request.response).to be_cached
81
+ expect(cached_request.response).to eq(response)
82
+ end
83
+ end
84
+ end
85
+
56
86
  end
57
87
  end
58
88
  end
@@ -88,12 +88,14 @@ describe Typhoeus::Pool do
88
88
 
89
89
  context "when threaded access" do
90
90
  it "creates correct number of easies" do
91
- array = []
91
+ queue = Queue.new
92
92
  (0..9).map do |n|
93
93
  Thread.new do
94
- array << Typhoeus::Pool.get
94
+ queue.enq(Typhoeus::Pool.get)
95
95
  end
96
96
  end.map(&:join)
97
+
98
+ array = Array.new(queue.size) { queue.pop }
97
99
  expect(array.uniq.size).to eq(10)
98
100
  end
99
101
  end
@@ -55,6 +55,30 @@ describe Typhoeus::Request::Cacheable do
55
55
  request.run
56
56
  end
57
57
  end
58
+
59
+ context "when cache is specified on a request" do
60
+ before { Typhoeus::Config.cache = false }
61
+
62
+ context "when cache is false" do
63
+ let(:options) { { :cache => false } }
64
+
65
+ it "finishes request" do
66
+ expect(request.response).to_not be(response)
67
+ request.run
68
+ end
69
+ end
70
+
71
+ context "when cache is defined" do
72
+ let(:options) { { :cache => cache } }
73
+
74
+ before { cache.memory[request] = response }
75
+
76
+ it "finishes request" do
77
+ expect(request).to receive(:finish).with(response)
78
+ request.run
79
+ end
80
+ end
81
+ end
58
82
  end
59
83
  end
60
84
 
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe Typhoeus::Request::Callbacks do
4
4
  let(:request) { Typhoeus::Request.new("fubar") }
5
5
 
6
- [:on_complete, :on_success, :on_failure].each do |callback|
6
+ [:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
7
7
  describe "##{callback}" do
8
8
  it "responds" do
9
9
  expect(request).to respond_to(callback)
@@ -33,7 +33,7 @@ describe Typhoeus::Request::Callbacks do
33
33
  end
34
34
 
35
35
  describe "#execute_callbacks" do
36
- [:on_complete, :on_success, :on_failure].each do |callback|
36
+ [:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
37
37
  context "when #{callback}" do
38
38
  context "when local callback" do
39
39
  before do
@@ -5,7 +5,7 @@ describe Typhoeus::Request::Marshal do
5
5
  let(:request) { Typhoeus::Request.new(base_url) }
6
6
 
7
7
  describe "#marshal_dump" do
8
- %w(on_complete on_success on_failure).each do |name|
8
+ %w(on_complete on_success on_failure on_progress).each do |name|
9
9
  context "when #{name} handler" do
10
10
  before { request.instance_variable_set("@#{name}", Proc.new{}) }
11
11
 
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Typhoeus::Request do
4
4
  let(:base_url) { "localhost:3001" }
5
- let(:options) { {:verbose => true, :headers => { 'User-Agent' => "Fubar" }, :maxredirs => 50} }
5
+ let(:options) { {:verbose => true, :headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :maxredirs => 50} }
6
6
  let(:request) { Typhoeus::Request.new(base_url, options) }
7
7
 
8
8
  describe ".url" do
@@ -110,6 +110,23 @@ describe Typhoeus::Request do
110
110
  end
111
111
  end
112
112
  end
113
+
114
+ context "when Config.proxy set" do
115
+ before { Typhoeus.configure { |config| config.proxy = "http://proxy.internal" } }
116
+ after { Typhoeus.configure { |config| config.proxy = nil } }
117
+
118
+ it "respects" do
119
+ expect(request.options[:proxy]).to eq("http://proxy.internal")
120
+ end
121
+
122
+ context "when option proxy set" do
123
+ let(:options) { {:proxy => nil} }
124
+
125
+ it "does not override" do
126
+ expect(request.options[:proxy]).to be_nil
127
+ end
128
+ end
129
+ end
113
130
  end
114
131
 
115
132
  describe "#eql?" do
@@ -150,7 +167,7 @@ describe Typhoeus::Request do
150
167
 
151
168
  context "when different order" do
152
169
  let(:other_options) {
153
- {:headers => { 'User-Agent' => "Fubar" }, :verbose => true }
170
+ {:headers => { 'User-Agent' => "Fubar", 'Expect' => ""}, :verbose => true }
154
171
  }
155
172
  let(:other) { Typhoeus::Request.new(base_url, other_options)}
156
173
 
@@ -166,7 +183,7 @@ describe Typhoeus::Request do
166
183
  context "when request.eql?(other)" do
167
184
  context "when different order" do
168
185
  let(:other_options) {
169
- {:headers => { 'User-Agent' => "Fubar" }, :verbose => true }
186
+ {:headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :verbose => true }
170
187
  }
171
188
  let(:other) { Typhoeus::Request.new(base_url, other_options)}
172
189
 
@@ -211,4 +228,5 @@ describe Typhoeus::Request do
211
228
  expect(request.encoded_body).to eq("a=1")
212
229
  end
213
230
  end
231
+
214
232
  end
@@ -55,7 +55,7 @@ describe Typhoeus::Response::Header do
55
55
  Server: gws
56
56
  X-XSS-Protection: 1; mode=block
57
57
  X-Frame-Options: SAMEORIGIN
58
- Transfer-Encoding: chunked'
58
+ Transfer-Encoding: chunked'.gsub(/^\s{8}/, '')
59
59
  end
60
60
 
61
61
  it "sets raw" do
@@ -92,6 +92,56 @@ describe Typhoeus::Response::Header do
92
92
  expect(header[name.downcase]).to eq(value)
93
93
  end
94
94
  end
95
+
96
+ context 'includes a multi-line header' do
97
+ let(:raw) do
98
+ 'HTTP/1.1 200 OK
99
+ Date: Fri, 29 Jun 2012 10:09:23 GMT
100
+ Content-Security-Policy: default-src "self";
101
+ img-src * data: "self";
102
+ upgrade-insecure-requests;'.gsub(/^\s{10}/, '')
103
+ end
104
+
105
+ it "joins header parts" do
106
+ expect(header).to eq({
107
+ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT',
108
+ 'Content-Security-Policy' => 'default-src "self"; img-src * data: "self"; upgrade-insecure-requests;'
109
+ })
110
+ end
111
+ end
112
+
113
+ context 'includes line with only whitespace' do
114
+ let(:raw) do
115
+ 'HTTP/1.1 200 OK
116
+ Date: Fri, 29 Jun 2012 10:09:23 GMT
117
+
118
+ '.gsub(/^\s{10}/, '')
119
+ end
120
+
121
+ it 'ignores it' do
122
+ expect(header).to eq({ 'Date' => 'Fri, 29 Jun 2012 10:09:23 GMT' })
123
+ end
124
+ end
125
+
126
+ context 'with broken headers' do
127
+ let(:raw) do
128
+ 'HTTP/1.1 200 OK
129
+ Date:
130
+ Content-Type
131
+ '.gsub(/^\s{10}/, '')
132
+ end
133
+
134
+ it 'returns empty string for invalid headers' do
135
+ expect(header.to_hash).to include({ 'Date' => '', 'Content-Type' => '' })
136
+ end
137
+ end
95
138
  end
96
139
  end
140
+
141
+ it "can be Marshal'd" do
142
+ header = Typhoeus::Response::Header.new("Foo: Bar")
143
+ expect {
144
+ Marshal.dump(header)
145
+ }.not_to raise_error
146
+ end
97
147
  end
@@ -74,6 +74,17 @@ describe Typhoeus::Response::Informations do
74
74
  expect(response.response_headers).to include("\r\n")
75
75
  end
76
76
  end
77
+
78
+ context "when multiple values for a header" do
79
+ let(:options) { { :mock => true, :headers => {"Length" => 1, "Content-Type" => "text/plain", "set-cookie" => ["cookieone=one","cookietwo=two"] } } }
80
+
81
+ it "constructs response_headers" do
82
+ expect(response.response_headers).to include("Length: 1")
83
+ expect(response.response_headers).to include("Content-Type: text/plain")
84
+ expect(response.response_headers).to include("set-cookie: cookieone=one,cookietwo=two")
85
+ expect(response.response_headers).to include("\r\n")
86
+ end
87
+ end
77
88
  end
78
89
  end
79
90
  end
@@ -232,7 +243,7 @@ describe Typhoeus::Response::Informations do
232
243
  end
233
244
 
234
245
  it "returns headers" do
235
- expect(response.headers).to include("Length" => "1")
246
+ expect(response.headers.to_hash).to include("Length" => "1")
236
247
  end
237
248
  end
238
249
  end
@@ -128,6 +128,60 @@ describe Typhoeus::Response::Status do
128
128
  end
129
129
  end
130
130
 
131
+ describe "#failure?" do
132
+ context "when response code between 300-526 and 100-300" do
133
+ let(:options) { {:return_code => return_code, :response_code => 300} }
134
+
135
+ context "when mock" do
136
+ before { response.mock = true }
137
+
138
+ context "when return_code :internal_server_error" do
139
+ let(:return_code) { :internal_server_error }
140
+
141
+ it "returns true" do
142
+ expect(response.failure?).to be_truthy
143
+ end
144
+ end
145
+
146
+ context "when return_code nil" do
147
+ let(:return_code) { nil }
148
+
149
+ it "returns true" do
150
+ expect(response.failure?).to be_truthy
151
+ end
152
+ end
153
+ end
154
+
155
+ context "when no mock" do
156
+ before { response.mock = nil }
157
+
158
+ context "when return_code :internal_server_error" do
159
+ let(:return_code) { :internal_server_error }
160
+
161
+ it "returns true" do
162
+ expect(response.failure?).to be_truthy
163
+ end
164
+ end
165
+
166
+ context "when return_code nil" do
167
+ let(:return_code) { nil }
168
+
169
+ it "returns false" do
170
+ expect(response.failure?).to be_falsey
171
+ end
172
+ end
173
+ end
174
+ end
175
+
176
+ context "when response code is not 300-526" do
177
+ let(:options) { {:return_code => :ok, :response_code => 200} }
178
+
179
+ it "returns false" do
180
+ expect(response.failure?).to be_falsey
181
+ end
182
+ end
183
+ end
184
+
131
185
  describe "#modified?" do
132
186
  context "when response code 304" do
133
187
  let(:options) { {:return_code => :ok, :response_code => 304} }
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.0.2
4
+ version: 1.4.0
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-04-28 00:00:00.000000000 Z
13
+ date: 2020-05-08 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
@@ -145,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
150
  version: 1.3.6
146
151
  requirements: []
147
152
  rubyforge_project:
148
- rubygems_version: 2.2.2
153
+ rubygems_version: 2.5.2.3
149
154
  signing_key:
150
155
  specification_version: 4
151
156
  summary: Parallel HTTP library on top of libcurl multi.
@@ -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