typhoeus 0.4.2 → 1.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rspec +4 -0
- data/.travis.yml +26 -0
- data/CHANGELOG.md +341 -28
- data/CONTRIBUTING.md +20 -0
- data/Gemfile +31 -2
- data/Guardfile +9 -0
- data/LICENSE +1 -1
- data/README.md +486 -357
- data/Rakefile +21 -12
- data/UPGRADE.md +55 -0
- data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +76 -0
- data/lib/rack/typhoeus/middleware/params_decoder.rb +57 -0
- data/lib/rack/typhoeus.rb +1 -0
- data/lib/typhoeus/adapters/faraday.rb +180 -0
- data/lib/typhoeus/cache/dalli.rb +28 -0
- data/lib/typhoeus/cache/rails.rb +28 -0
- data/lib/typhoeus/cache/redis.rb +35 -0
- data/lib/typhoeus/config.rb +69 -0
- data/lib/typhoeus/easy_factory.rb +180 -0
- data/lib/typhoeus/errors/no_stub.rb +12 -0
- data/lib/typhoeus/errors/typhoeus_error.rb +8 -0
- data/lib/typhoeus/errors.rb +9 -0
- data/lib/typhoeus/expectation.rb +217 -0
- data/lib/typhoeus/hydra/addable.rb +23 -0
- data/lib/typhoeus/hydra/before.rb +31 -0
- data/lib/typhoeus/hydra/block_connection.rb +35 -0
- data/lib/typhoeus/hydra/cacheable.rb +15 -0
- data/lib/typhoeus/hydra/memoizable.rb +56 -0
- data/lib/typhoeus/hydra/queueable.rb +83 -0
- data/lib/typhoeus/hydra/runnable.rb +19 -0
- data/lib/typhoeus/hydra/stubbable.rb +28 -0
- data/lib/typhoeus/hydra.rb +84 -236
- data/lib/typhoeus/pool.rb +70 -0
- data/lib/typhoeus/railtie.rb +12 -0
- data/lib/typhoeus/request/actions.rb +125 -0
- data/lib/typhoeus/request/before.rb +30 -0
- data/lib/typhoeus/request/block_connection.rb +52 -0
- data/lib/typhoeus/request/cacheable.rb +38 -0
- data/lib/typhoeus/request/callbacks.rb +151 -0
- data/lib/typhoeus/request/marshal.rb +22 -0
- data/lib/typhoeus/request/memoizable.rb +38 -0
- data/lib/typhoeus/request/operations.rb +40 -0
- data/lib/typhoeus/request/responseable.rb +29 -0
- data/lib/typhoeus/request/streamable.rb +34 -0
- data/lib/typhoeus/request/stubbable.rb +30 -0
- data/lib/typhoeus/request.rb +186 -231
- data/lib/typhoeus/response/cacheable.rb +14 -0
- data/lib/typhoeus/response/header.rb +105 -0
- data/lib/typhoeus/response/informations.rb +248 -0
- data/lib/typhoeus/response/status.rb +106 -0
- data/lib/typhoeus/response.rb +60 -115
- data/lib/typhoeus/version.rb +3 -1
- data/lib/typhoeus.rb +126 -39
- data/perf/profile.rb +14 -0
- data/perf/vs_nethttp.rb +64 -0
- data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +156 -0
- data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/localhost_server.rb +94 -0
- data/spec/support/memory_cache.rb +15 -0
- data/spec/support/server.rb +116 -0
- data/spec/typhoeus/adapters/faraday_spec.rb +339 -0
- data/spec/typhoeus/cache/dalli_spec.rb +41 -0
- data/spec/typhoeus/cache/redis_spec.rb +41 -0
- data/spec/typhoeus/config_spec.rb +15 -0
- data/spec/typhoeus/easy_factory_spec.rb +143 -0
- data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
- data/spec/typhoeus/expectation_spec.rb +280 -0
- data/spec/typhoeus/hydra/addable_spec.rb +22 -0
- data/spec/typhoeus/hydra/before_spec.rb +98 -0
- data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
- data/spec/typhoeus/hydra/cacheable_spec.rb +88 -0
- data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
- data/spec/typhoeus/hydra/queueable_spec.rb +98 -0
- data/spec/typhoeus/hydra/runnable_spec.rb +137 -0
- data/spec/typhoeus/hydra/stubbable_spec.rb +48 -0
- data/spec/typhoeus/hydra_spec.rb +22 -0
- data/spec/typhoeus/pool_spec.rb +137 -0
- data/spec/typhoeus/request/actions_spec.rb +19 -0
- data/spec/typhoeus/request/before_spec.rb +93 -0
- data/spec/typhoeus/request/block_connection_spec.rb +75 -0
- data/spec/typhoeus/request/cacheable_spec.rb +94 -0
- data/spec/typhoeus/request/callbacks_spec.rb +91 -0
- data/spec/typhoeus/request/marshal_spec.rb +60 -0
- data/spec/typhoeus/request/memoizable_spec.rb +34 -0
- data/spec/typhoeus/request/operations_spec.rb +101 -0
- data/spec/typhoeus/request/responseable_spec.rb +13 -0
- data/spec/typhoeus/request/stubbable_spec.rb +45 -0
- data/spec/typhoeus/request_spec.rb +232 -0
- data/spec/typhoeus/response/header_spec.rb +147 -0
- data/spec/typhoeus/response/informations_spec.rb +283 -0
- data/spec/typhoeus/response/status_spec.rb +256 -0
- data/spec/typhoeus/response_spec.rb +100 -0
- data/spec/typhoeus_spec.rb +105 -0
- data/typhoeus.gemspec +25 -0
- metadata +146 -158
- data/lib/typhoeus/curl.rb +0 -453
- data/lib/typhoeus/easy/auth.rb +0 -14
- data/lib/typhoeus/easy/callbacks.rb +0 -33
- data/lib/typhoeus/easy/ffi_helper.rb +0 -61
- data/lib/typhoeus/easy/infos.rb +0 -90
- data/lib/typhoeus/easy/options.rb +0 -115
- data/lib/typhoeus/easy/proxy.rb +0 -20
- data/lib/typhoeus/easy/ssl.rb +0 -82
- data/lib/typhoeus/easy.rb +0 -115
- data/lib/typhoeus/filter.rb +0 -28
- data/lib/typhoeus/form.rb +0 -61
- data/lib/typhoeus/header.rb +0 -54
- data/lib/typhoeus/hydra/callbacks.rb +0 -24
- data/lib/typhoeus/hydra/connect_options.rb +0 -61
- data/lib/typhoeus/hydra/stubbing.rb +0 -68
- data/lib/typhoeus/hydra_mock.rb +0 -131
- data/lib/typhoeus/multi.rb +0 -146
- data/lib/typhoeus/param_processor.rb +0 -43
- data/lib/typhoeus/remote.rb +0 -306
- data/lib/typhoeus/remote_method.rb +0 -108
- data/lib/typhoeus/remote_proxy_object.rb +0 -50
- data/lib/typhoeus/utils.rb +0 -50
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Hydra::Runnable do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:options) { {} }
|
6
|
+
let(:hydra) { Typhoeus::Hydra.new(options) }
|
7
|
+
let(:receive_counter) { double :mark => :twain }
|
8
|
+
|
9
|
+
describe "#run" do
|
10
|
+
let(:requests) { [] }
|
11
|
+
|
12
|
+
before do
|
13
|
+
requests.each { |r| hydra.queue r }
|
14
|
+
end
|
15
|
+
|
16
|
+
it "runs multi#dequeue_many" do
|
17
|
+
expect(hydra).to receive(:dequeue_many)
|
18
|
+
hydra.run
|
19
|
+
end
|
20
|
+
|
21
|
+
it "runs multi#perform" do
|
22
|
+
expect(hydra.multi).to receive(:perform)
|
23
|
+
hydra.run
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when request queued" do
|
27
|
+
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
|
28
|
+
let(:requests) { [first] }
|
29
|
+
|
30
|
+
it "sends" do
|
31
|
+
hydra.run
|
32
|
+
expect(first.response).to be
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "when three request queued" do
|
37
|
+
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
|
38
|
+
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
|
39
|
+
let(:third) { Typhoeus::Request.new("localhost:3001/third") }
|
40
|
+
let(:requests) { [first, second, third] }
|
41
|
+
|
42
|
+
it "sends first" do
|
43
|
+
hydra.run
|
44
|
+
expect(first.response).to be
|
45
|
+
end
|
46
|
+
|
47
|
+
it "sends second" do
|
48
|
+
hydra.run
|
49
|
+
expect(second.response).to be
|
50
|
+
end
|
51
|
+
|
52
|
+
it "sends third" do
|
53
|
+
hydra.run
|
54
|
+
expect(third.response).to be
|
55
|
+
end
|
56
|
+
|
57
|
+
it "sends first first" do
|
58
|
+
first.on_complete do
|
59
|
+
expect(second.response).to be_nil
|
60
|
+
expect(third.response).to be_nil
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "sends second second" do
|
65
|
+
first.on_complete do
|
66
|
+
expect(first.response).to be
|
67
|
+
expect(third.response).to be_nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
it "sends thirds last" do
|
72
|
+
first.on_complete do
|
73
|
+
expect(second.response).to be
|
74
|
+
expect(third.response).to be
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
context "when really queued request" do
|
80
|
+
let(:options) { {:max_concurrency => 1} }
|
81
|
+
let(:first) { Typhoeus::Request.new("localhost:3001/first") }
|
82
|
+
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
|
83
|
+
let(:third) { Typhoeus::Request.new("localhost:3001/third") }
|
84
|
+
let(:requests) { [first, second, third] }
|
85
|
+
|
86
|
+
it "sends first" do
|
87
|
+
hydra.run
|
88
|
+
expect(first.response).to be
|
89
|
+
end
|
90
|
+
|
91
|
+
it "sends second" do
|
92
|
+
hydra.run
|
93
|
+
expect(second.response).to be
|
94
|
+
end
|
95
|
+
|
96
|
+
it "sends third" do
|
97
|
+
hydra.run
|
98
|
+
expect(third.response).to be
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
context "when request queued in callback" do
|
103
|
+
let(:first) do
|
104
|
+
Typhoeus::Request.new("localhost:3001/first").tap do |r|
|
105
|
+
r.on_complete{ hydra.queue(second) }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
let(:second) { Typhoeus::Request.new("localhost:3001/second") }
|
109
|
+
let(:requests) { [first] }
|
110
|
+
|
111
|
+
before { Typhoeus.on_complete { |r| receive_counter.mark } }
|
112
|
+
after { Typhoeus.on_complete.clear; Typhoeus.before.clear }
|
113
|
+
|
114
|
+
context "when real request" do
|
115
|
+
context "when max_concurrency default" do
|
116
|
+
let(:options) { {} }
|
117
|
+
|
118
|
+
it "calls on_complete callback once for every response" do
|
119
|
+
expect(receive_counter).to receive(:mark).exactly(2).times
|
120
|
+
hydra.run
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
context "when no real request" do
|
126
|
+
context "when before hook returns and finishes response" do
|
127
|
+
before { Typhoeus.before{ |request| request.finish(Typhoeus::Response.new) } }
|
128
|
+
|
129
|
+
it "simulates real multi run and adds and finishes both requests" do
|
130
|
+
expect(receive_counter).to receive(:mark).exactly(2).times
|
131
|
+
hydra.run
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Hydra::Stubbable do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:request) { Typhoeus::Request.new(base_url) }
|
6
|
+
let(:response) { Typhoeus::Response.new }
|
7
|
+
let(:hydra) { Typhoeus::Hydra.new }
|
8
|
+
|
9
|
+
before { Typhoeus.stub(base_url).and_return(response) }
|
10
|
+
|
11
|
+
describe "#add" do
|
12
|
+
it "checks expectations" do
|
13
|
+
hydra.add(request)
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when expectation found" do
|
17
|
+
it "calls on_headers callbacks" do
|
18
|
+
canary = :not_called
|
19
|
+
request.on_headers do
|
20
|
+
canary = :called
|
21
|
+
end
|
22
|
+
hydra.add(request)
|
23
|
+
hydra.run
|
24
|
+
expect(canary).to eq(:called)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "calls on_body callbacks" do
|
28
|
+
canary = :not_called
|
29
|
+
request.on_body do
|
30
|
+
canary = :called
|
31
|
+
end
|
32
|
+
hydra.add(request)
|
33
|
+
hydra.run
|
34
|
+
expect(canary).to eq(:called)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "finishes response" do
|
38
|
+
expect(request).to receive(:finish)
|
39
|
+
hydra.add(request)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "is a mock" do
|
43
|
+
hydra.add(request)
|
44
|
+
expect(request.response.mock).to be(true)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
describe Typhoeus::Hydra do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:options) { {} }
|
6
|
+
let(:hydra) { Typhoeus::Hydra.new(options) }
|
7
|
+
|
8
|
+
describe "#new" do
|
9
|
+
let(:options) { {:pipeling => true} }
|
10
|
+
|
11
|
+
it "passes options to multi" do
|
12
|
+
expect(Ethon::Multi).to receive(:new).with(options)
|
13
|
+
hydra
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#hydra" do
|
18
|
+
it "returns a hydra" do
|
19
|
+
expect(Typhoeus::Hydra.hydra).to be_a(Typhoeus::Hydra)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Pool do
|
4
|
+
let(:easy) { Ethon::Easy.new }
|
5
|
+
after { Typhoeus::Pool.clear }
|
6
|
+
|
7
|
+
describe "#easies" do
|
8
|
+
it "returns array" do
|
9
|
+
expect(Typhoeus::Pool.send(:easies)).to be_a(Array)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#release" do
|
14
|
+
it "resets easy" do
|
15
|
+
expect(easy).to receive(:reset)
|
16
|
+
Typhoeus::Pool.release(easy)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "flush cookies to disk" do
|
20
|
+
expect(easy).to receive(:cookielist=).with('flush')
|
21
|
+
expect(easy).to receive(:reset)
|
22
|
+
expect(easy).to receive(:cookielist=).with('all')
|
23
|
+
Typhoeus::Pool.release(easy)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "writes cookies to disk" do
|
27
|
+
tempfile1 = Tempfile.new('cookies')
|
28
|
+
tempfile2 = Tempfile.new('cookies')
|
29
|
+
|
30
|
+
easy.cookiejar = tempfile1.path
|
31
|
+
easy.url = "localhost:3001/cookies-test"
|
32
|
+
easy.perform
|
33
|
+
|
34
|
+
Typhoeus::Pool.release(easy)
|
35
|
+
|
36
|
+
expect(File.zero?(tempfile1.path)).to be(false)
|
37
|
+
expect(File.read(tempfile1.path)).to match(/\s+foo\s+bar$/)
|
38
|
+
expect(File.read(tempfile1.path)).to match(/\s+bar\s+foo$/)
|
39
|
+
|
40
|
+
# do it again - and check if tempfile1 wasn't change
|
41
|
+
easy.cookiejar = tempfile2.path
|
42
|
+
easy.url = "localhost:3001/cookies-test2"
|
43
|
+
easy.perform
|
44
|
+
|
45
|
+
Typhoeus::Pool.release(easy)
|
46
|
+
|
47
|
+
# tempfile 1
|
48
|
+
expect(File.zero?(tempfile1.path)).to be(false)
|
49
|
+
expect(File.read(tempfile1.path)).to match(/\s+foo\s+bar$/)
|
50
|
+
expect(File.read(tempfile1.path)).to match(/\s+bar\s+foo$/)
|
51
|
+
|
52
|
+
# tempfile2
|
53
|
+
expect(File.zero?(tempfile2.path)).to be(false)
|
54
|
+
expect(File.read(tempfile2.path)).to match(/\s+foo2\s+bar$/)
|
55
|
+
expect(File.read(tempfile2.path)).to match(/\s+bar2\s+foo$/)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "puts easy back into pool" do
|
59
|
+
Typhoeus::Pool.release(easy)
|
60
|
+
expect(Typhoeus::Pool.send(:easies)).to include(easy)
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when threaded access" do
|
64
|
+
it "releases correct number of easies" do
|
65
|
+
(0..9).map do |n|
|
66
|
+
Thread.new do
|
67
|
+
Typhoeus::Pool.release(Ethon::Easy.new)
|
68
|
+
end
|
69
|
+
end.map(&:join)
|
70
|
+
expect(Typhoeus::Pool.send(:easies).size).to eq(10)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#get" do
|
76
|
+
context "when easy in pool" do
|
77
|
+
before { Typhoeus::Pool.send(:easies) << easy }
|
78
|
+
|
79
|
+
it "takes" do
|
80
|
+
expect(Typhoeus::Pool.get).to eq(easy)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
context "when no easy in pool" do
|
85
|
+
it "creates" do
|
86
|
+
expect(Typhoeus::Pool.get).to be_a(Ethon::Easy)
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when threaded access" do
|
90
|
+
it "creates correct number of easies" do
|
91
|
+
queue = Queue.new
|
92
|
+
(0..9).map do |n|
|
93
|
+
Thread.new do
|
94
|
+
queue.enq(Typhoeus::Pool.get)
|
95
|
+
end
|
96
|
+
end.map(&:join)
|
97
|
+
|
98
|
+
array = Array.new(queue.size) { queue.pop }
|
99
|
+
expect(array.uniq.size).to eq(10)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context "when forked" do
|
105
|
+
before do
|
106
|
+
allow(Process).to receive(:pid).and_return(1)
|
107
|
+
Typhoeus::Pool.send(:easies) << easy
|
108
|
+
allow(Process).to receive(:pid).and_return(2)
|
109
|
+
end
|
110
|
+
|
111
|
+
after do
|
112
|
+
allow(Process).to receive(:pid).and_call_original
|
113
|
+
Typhoeus::Pool.instance_variable_set(:@pid, Process.pid)
|
114
|
+
end
|
115
|
+
|
116
|
+
it "creates" do
|
117
|
+
expect(Typhoeus::Pool.get).to_not eq(easy)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
describe "#with" do
|
123
|
+
it "is re-entrant" do
|
124
|
+
array = []
|
125
|
+
Typhoeus::Pool.with_easy do |e1|
|
126
|
+
array << e1
|
127
|
+
Typhoeus::Pool.with_easy do |e2|
|
128
|
+
array << e2
|
129
|
+
Typhoeus::Pool.with_easy do |e3|
|
130
|
+
array << e3
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
expect(array.uniq.size).to eq(3)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Actions do
|
4
|
+
[:get, :post, :put, :delete, :head, :patch, :options].each do |name|
|
5
|
+
describe ".#{name}" do
|
6
|
+
let(:response) { Typhoeus::Request.method(name).call("http://localhost:3001") }
|
7
|
+
|
8
|
+
it "returns ok" do
|
9
|
+
expect(response.return_code).to eq(:ok)
|
10
|
+
end
|
11
|
+
|
12
|
+
unless name == :head
|
13
|
+
it "makes #{name.to_s.upcase} Request" do
|
14
|
+
expect(response.response_body).to include("\"REQUEST_METHOD\":\"#{name.to_s.upcase}\"")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Before do
|
4
|
+
let(:request) { Typhoeus::Request.new("") }
|
5
|
+
let(:receive_counter) { double :mark => :twain }
|
6
|
+
|
7
|
+
describe "#queue" do
|
8
|
+
context "when before" do
|
9
|
+
context "when one" do
|
10
|
+
it "executes" do
|
11
|
+
Typhoeus.before { |r| receive_counter.mark }
|
12
|
+
expect(receive_counter).to receive(:mark)
|
13
|
+
request.run
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when true" do
|
17
|
+
it "calls super" do
|
18
|
+
Typhoeus.before { true }
|
19
|
+
expect(Typhoeus::Expectation).to receive(:response_for)
|
20
|
+
request.run
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when false" do
|
25
|
+
it "doesn't call super" do
|
26
|
+
Typhoeus.before { false }
|
27
|
+
expect(Typhoeus::Expectation).to receive(:response_for).never
|
28
|
+
request.run
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns response" do
|
32
|
+
Typhoeus.before { |r| r.response = 1; false }
|
33
|
+
expect(request.run).to be(1)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "when a response" do
|
38
|
+
it "doesn't call super" do
|
39
|
+
Typhoeus.before { Typhoeus::Response.new }
|
40
|
+
expect(Typhoeus::Expectation).to receive(:response_for).never
|
41
|
+
request.run
|
42
|
+
end
|
43
|
+
|
44
|
+
it "returns response" do
|
45
|
+
Typhoeus.before { |r| r.response = Typhoeus::Response.new }
|
46
|
+
expect(request.run).to be_a(Typhoeus::Response)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when multi" do
|
52
|
+
context "when all true" do
|
53
|
+
before { 3.times { Typhoeus.before { |r| receive_counter.mark } } }
|
54
|
+
|
55
|
+
it "calls super" do
|
56
|
+
expect(Typhoeus::Expectation).to receive(:response_for)
|
57
|
+
request.run
|
58
|
+
end
|
59
|
+
|
60
|
+
it "executes all" do
|
61
|
+
expect(receive_counter).to receive(:mark).exactly(3)
|
62
|
+
request.run
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
context "when middle false" do
|
67
|
+
before do
|
68
|
+
Typhoeus.before { |r| receive_counter.mark }
|
69
|
+
Typhoeus.before { |r| receive_counter.mark; nil }
|
70
|
+
Typhoeus.before { |r| receive_counter.mark }
|
71
|
+
end
|
72
|
+
|
73
|
+
it "doesn't call super" do
|
74
|
+
expect(Typhoeus::Expectation).to receive(:response_for).never
|
75
|
+
request.run
|
76
|
+
end
|
77
|
+
|
78
|
+
it "executes only two" do
|
79
|
+
expect(receive_counter).to receive(:mark).exactly(2).times
|
80
|
+
request.run
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
context "when no before" do
|
87
|
+
it "calls super" do
|
88
|
+
expect(Typhoeus::Expectation).to receive(:response_for)
|
89
|
+
request.run
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::BlockConnection do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:request) { Typhoeus::Request.new(base_url, {:method => :get}) }
|
6
|
+
|
7
|
+
describe "run" do
|
8
|
+
context "when blocked" do
|
9
|
+
before { request.block_connection = true }
|
10
|
+
|
11
|
+
it "raises" do
|
12
|
+
expect{ request.run }.to raise_error(Typhoeus::Errors::NoStub)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
context "when not blocked" do
|
17
|
+
before { request.block_connection = false }
|
18
|
+
|
19
|
+
it "doesn't raise" do
|
20
|
+
expect{ request.run }.to_not raise_error
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#blocked?" do
|
26
|
+
context "when local block_connection" do
|
27
|
+
context "when true" do
|
28
|
+
before { request.block_connection = true }
|
29
|
+
|
30
|
+
it "returns true" do
|
31
|
+
expect(request.blocked?).to be_truthy
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "when false" do
|
36
|
+
before { request.block_connection = false }
|
37
|
+
|
38
|
+
it "returns false" do
|
39
|
+
expect(request.blocked?).to be_falsey
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context "when global block_connection" do
|
45
|
+
context "when true" do
|
46
|
+
before { Typhoeus::Config.block_connection = true }
|
47
|
+
after { Typhoeus::Config.block_connection = false }
|
48
|
+
|
49
|
+
it "returns true" do
|
50
|
+
expect(request.blocked?).to be_truthy
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
context "when false" do
|
55
|
+
before { Typhoeus::Config.block_connection = false }
|
56
|
+
|
57
|
+
it "returns false" do
|
58
|
+
expect(request.blocked?).to be_falsey
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "when global and local block_connection" do
|
64
|
+
before do
|
65
|
+
Typhoeus::Config.block_connection = true
|
66
|
+
request.block_connection = false
|
67
|
+
end
|
68
|
+
after { Typhoeus::Config.block_connection = false }
|
69
|
+
|
70
|
+
it "takes local" do
|
71
|
+
expect(request.blocked?).to be_falsey
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,94 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Cacheable do
|
4
|
+
let(:cache) { MemoryCache.new }
|
5
|
+
let(:options) { {} }
|
6
|
+
let(:request) { Typhoeus::Request.new("http://localhost:3001", options) }
|
7
|
+
let(:response) { Typhoeus::Response.new }
|
8
|
+
|
9
|
+
before { Typhoeus::Config.cache = cache }
|
10
|
+
after { Typhoeus::Config.cache = false }
|
11
|
+
|
12
|
+
describe "#response=" do
|
13
|
+
context "when cache activated" do
|
14
|
+
context "when request new" do
|
15
|
+
it "caches response" do
|
16
|
+
request.response = response
|
17
|
+
expect(cache.memory[request]).to be
|
18
|
+
end
|
19
|
+
|
20
|
+
it "doesn't set cached on response" do
|
21
|
+
request.response = response
|
22
|
+
expect(request.response.cached?).to be_falsey
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "when request in memory" do
|
27
|
+
before { cache.memory[request] = response }
|
28
|
+
|
29
|
+
it "finishes request" do
|
30
|
+
expect(request).to receive(:finish).with(response)
|
31
|
+
request.run
|
32
|
+
end
|
33
|
+
|
34
|
+
it "sets cached to true for response" do
|
35
|
+
request.run
|
36
|
+
expect(request.response.cached?).to be_truthy
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
describe "#run" do
|
43
|
+
context "when cache activated" do
|
44
|
+
context "when request new" do
|
45
|
+
it "fetches response" do
|
46
|
+
expect(request.response).to_not be(response)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when request in memory" do
|
51
|
+
before { cache.memory[request] = response }
|
52
|
+
|
53
|
+
it "finishes request" do
|
54
|
+
expect(request).to receive(:finish).with(response)
|
55
|
+
request.run
|
56
|
+
end
|
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
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
describe "#cache_ttl" do
|
86
|
+
context "when option[:cache_ttl]" do
|
87
|
+
let(:options) { {:cache_ttl => 1} }
|
88
|
+
|
89
|
+
it "returns" do
|
90
|
+
expect(request.cache_ttl).to be(1)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|