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,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Callbacks do
|
4
|
+
let(:request) { Typhoeus::Request.new("fubar") }
|
5
|
+
|
6
|
+
[:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
|
7
|
+
describe "##{callback}" do
|
8
|
+
it "responds" do
|
9
|
+
expect(request).to respond_to(callback)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "when no block given" do
|
13
|
+
it "returns @#{callback}" do
|
14
|
+
expect(request.method(callback).call).to eq([])
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context "when block given" do
|
19
|
+
it "stores" do
|
20
|
+
request.method(callback).call { p 1 }
|
21
|
+
expect(request.instance_variable_get("@#{callback}").size).to eq(1)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context "when multiple blocks given" do
|
26
|
+
it "stores" do
|
27
|
+
request.method(callback).call { p 1 }
|
28
|
+
request.method(callback).call { p 2 }
|
29
|
+
expect(request.instance_variable_get("@#{callback}").size).to eq(2)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
describe "#execute_callbacks" do
|
36
|
+
[:on_complete, :on_success, :on_failure, :on_progress].each do |callback|
|
37
|
+
context "when #{callback}" do
|
38
|
+
context "when local callback" do
|
39
|
+
before do
|
40
|
+
code = if callback == :on_failure
|
41
|
+
500
|
42
|
+
else
|
43
|
+
200
|
44
|
+
end
|
45
|
+
request.response = Typhoeus::Response.new(:mock => true, :response_code => code)
|
46
|
+
request.method(callback).call {|r| expect(r).to be_a(Typhoeus::Response) }
|
47
|
+
end
|
48
|
+
|
49
|
+
it "executes blocks and passes response" do
|
50
|
+
request.execute_callbacks
|
51
|
+
end
|
52
|
+
|
53
|
+
it "sets handled_response" do
|
54
|
+
request.method(callback).call { 1 }
|
55
|
+
request.execute_callbacks
|
56
|
+
expect(request.response.handled_response).to be(1)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
context "when global callback" do
|
61
|
+
before do
|
62
|
+
request.response = Typhoeus::Response.new
|
63
|
+
Typhoeus.method(callback).call {|r| expect(r).to be_a(Typhoeus::Response) }
|
64
|
+
end
|
65
|
+
|
66
|
+
it "executes blocks and passes response" do
|
67
|
+
request.execute_callbacks
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context "when global and local callbacks" do
|
72
|
+
before do
|
73
|
+
request.response = Typhoeus::Response.new
|
74
|
+
Typhoeus.method(callback).call {|r| r.instance_variable_set(:@fu, 1) }
|
75
|
+
request.method(callback).call {|r| expect(r.instance_variable_get(:@fu)).to eq(1) }
|
76
|
+
end
|
77
|
+
|
78
|
+
it "runs global first" do
|
79
|
+
request.execute_callbacks
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when local on_complete and gobal on_success" do
|
86
|
+
it "runs all global callbacks first" do
|
87
|
+
skip
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Marshal do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:request) { Typhoeus::Request.new(base_url) }
|
6
|
+
|
7
|
+
describe "#marshal_dump" do
|
8
|
+
%w(on_complete on_success on_failure on_progress).each do |name|
|
9
|
+
context "when #{name} handler" do
|
10
|
+
before { request.instance_variable_set("@#{name}", Proc.new{}) }
|
11
|
+
|
12
|
+
it "doesn't include @#{name}" do
|
13
|
+
expect(request.send(:marshal_dump).map(&:first)).to_not include("@#{name}")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "doesn't raise when dumped" do
|
17
|
+
expect { Marshal.dump(request) }.to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
context "when loading" do
|
21
|
+
let(:loaded) { Marshal.load(Marshal.dump(request)) }
|
22
|
+
|
23
|
+
it "includes base_url" do
|
24
|
+
expect(loaded.base_url).to eq(request.base_url)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "doesn't include #{name}" do
|
28
|
+
expect(loaded.instance_variables).to_not include("@#{name}")
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'when run through hydra' do
|
35
|
+
let(:options) { {} }
|
36
|
+
let(:hydra) { Typhoeus::Hydra.new(options) }
|
37
|
+
|
38
|
+
before(:each) do
|
39
|
+
hydra.queue(request)
|
40
|
+
hydra.run
|
41
|
+
end
|
42
|
+
|
43
|
+
it "doesn't include @hydra" do
|
44
|
+
expect(request.send(:marshal_dump).map(&:first)).to_not include("@hydra")
|
45
|
+
end
|
46
|
+
|
47
|
+
context 'when loading' do
|
48
|
+
let(:loaded) { Marshal.load(Marshal.dump(request)) }
|
49
|
+
|
50
|
+
it "includes base_url" do
|
51
|
+
expect(loaded.base_url).to eq(request.base_url)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "doesn't include #{name}" do
|
55
|
+
expect(loaded.instance_variables).to_not include("@hydra")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Memoizable do
|
4
|
+
let(:options) { {} }
|
5
|
+
let(:request) { Typhoeus::Request.new("fu", options) }
|
6
|
+
let(:response) { Typhoeus::Response.new }
|
7
|
+
let(:hydra) { Typhoeus::Hydra.new }
|
8
|
+
|
9
|
+
describe "#response=" do
|
10
|
+
context "when memoization activated" do
|
11
|
+
before { Typhoeus::Config.memoize = true }
|
12
|
+
after { Typhoeus::Config.memoize = false }
|
13
|
+
|
14
|
+
context "when GET request" do
|
15
|
+
let(:options) { {:method => :get} }
|
16
|
+
before { request.hydra = hydra }
|
17
|
+
|
18
|
+
it "stores response in memory" do
|
19
|
+
request.response = response
|
20
|
+
expect(hydra.memory[request]).to be
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "when no GET request" do
|
25
|
+
let(:options) { {:method => :post} }
|
26
|
+
|
27
|
+
it "doesn't store response in memory" do
|
28
|
+
request.response = response
|
29
|
+
expect(hydra.memory[request]).to be_nil
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Operations do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:options) { {} }
|
6
|
+
let(:request) { Typhoeus::Request.new(base_url, options) }
|
7
|
+
|
8
|
+
describe "#run" do
|
9
|
+
let(:easy) { Ethon::Easy.new }
|
10
|
+
before { expect(Typhoeus::Pool).to receive(:get).and_return(easy) }
|
11
|
+
|
12
|
+
it "grabs an easy" do
|
13
|
+
request.run
|
14
|
+
end
|
15
|
+
|
16
|
+
it "generates settings" do
|
17
|
+
expect(easy).to receive(:http_request)
|
18
|
+
request.run
|
19
|
+
end
|
20
|
+
|
21
|
+
it "performs" do
|
22
|
+
expect(easy).to receive(:perform)
|
23
|
+
request.run
|
24
|
+
end
|
25
|
+
|
26
|
+
it "sets response" do
|
27
|
+
request.run
|
28
|
+
expect(request.response).to be
|
29
|
+
end
|
30
|
+
|
31
|
+
it "releases easy" do
|
32
|
+
expect(Typhoeus::Pool).to receive(:release)
|
33
|
+
request.run
|
34
|
+
end
|
35
|
+
|
36
|
+
it "calls on_body" do
|
37
|
+
on_body_called = false
|
38
|
+
request.on_body { |body, response| on_body_called = true }
|
39
|
+
request.run
|
40
|
+
expect(on_body_called).to be_truthy
|
41
|
+
expect(request.response.body).to satisfy { |v| v.nil? || v == '' }
|
42
|
+
end
|
43
|
+
|
44
|
+
it "makes response headers available to on_body" do
|
45
|
+
headers = nil
|
46
|
+
request.on_body { |body, response| headers = response.headers }
|
47
|
+
request.run
|
48
|
+
expect(headers).to be
|
49
|
+
expect(headers).to eq(request.response.headers)
|
50
|
+
end
|
51
|
+
|
52
|
+
it "calls on_headers and on_body" do
|
53
|
+
headers = nil
|
54
|
+
request.on_headers { |response| headers = response.headers }
|
55
|
+
request.on_body { |body, response| expect(headers).not_to be_nil ; expect(response.headers).to eq(headers) }
|
56
|
+
request.on_complete { |response| expect(response).not_to be_nil ; expect(response.headers).to eq(headers) ; expect(response.body).to be_empty }
|
57
|
+
request.run
|
58
|
+
end
|
59
|
+
|
60
|
+
it "calls on_headers and on_complete" do
|
61
|
+
headers = nil
|
62
|
+
request.on_headers { |response| headers = response.headers }
|
63
|
+
request.on_complete { |response| expect(response).not_to be_nil ; expect(response.headers).to eq(headers) ; expect(response.body).not_to be_empty }
|
64
|
+
request.run
|
65
|
+
end
|
66
|
+
|
67
|
+
it "calls on_complete" do
|
68
|
+
callback = double(:call)
|
69
|
+
expect(callback).to receive(:call)
|
70
|
+
request.instance_variable_set(:@on_complete, [callback])
|
71
|
+
request.run
|
72
|
+
end
|
73
|
+
|
74
|
+
it "returns a response" do
|
75
|
+
expect(request.run).to be_a(Typhoeus::Response)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#finish" do
|
80
|
+
let(:response) { Typhoeus::Response.new }
|
81
|
+
|
82
|
+
it "assigns response" do
|
83
|
+
request.finish(response)
|
84
|
+
expect(request.response).to be(response)
|
85
|
+
end
|
86
|
+
|
87
|
+
it "assigns request to response" do
|
88
|
+
request.finish(response)
|
89
|
+
expect(request.response.request).to be(request)
|
90
|
+
end
|
91
|
+
|
92
|
+
it "executes callbacks" do
|
93
|
+
expect(request).to receive(:execute_callbacks)
|
94
|
+
request.finish(response)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "returns response" do
|
98
|
+
expect(request.finish(response)).to be(response)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Responseable do
|
4
|
+
let(:request) { Typhoeus::Request.new("base_url", {}) }
|
5
|
+
let(:response) { Typhoeus::Response.new }
|
6
|
+
|
7
|
+
describe "#response=" do
|
8
|
+
it "stores response" do
|
9
|
+
request.response = response
|
10
|
+
expect(request.response).to eq(response)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request::Stubbable do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:request) { Typhoeus::Request.new(base_url) }
|
6
|
+
let(:response) { Typhoeus::Response.new }
|
7
|
+
|
8
|
+
before { Typhoeus.stub(base_url).and_return(response) }
|
9
|
+
|
10
|
+
describe "#run" do
|
11
|
+
it "checks expectations" do
|
12
|
+
request.run
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when expectation found" do
|
16
|
+
it "calls on_headers callbacks" do
|
17
|
+
canary = :not_called
|
18
|
+
request.on_headers do
|
19
|
+
canary = :called
|
20
|
+
end
|
21
|
+
request.run
|
22
|
+
expect(canary).to eq(:called)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "calls on_body callbacks" do
|
26
|
+
canary = :not_called
|
27
|
+
request.on_body do
|
28
|
+
canary = :called
|
29
|
+
end
|
30
|
+
request.run
|
31
|
+
expect(canary).to eq(:called)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "finishes request" do
|
35
|
+
expect(request).to receive(:finish)
|
36
|
+
request.run
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sets mock on response" do
|
40
|
+
request.run
|
41
|
+
expect(request.response.mock).to be(true)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,232 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Typhoeus::Request do
|
4
|
+
let(:base_url) { "localhost:3001" }
|
5
|
+
let(:options) { {:verbose => true, :headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :maxredirs => 50} }
|
6
|
+
let(:request) { Typhoeus::Request.new(base_url, options) }
|
7
|
+
|
8
|
+
describe ".url" do
|
9
|
+
context "when no parameters" do
|
10
|
+
it "returns base_url" do
|
11
|
+
expect(request.url).to eq(request.base_url)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context "when parameters" do
|
16
|
+
let(:options) { {:params => {:a => 1}} }
|
17
|
+
|
18
|
+
it "returns full url" do
|
19
|
+
expect(request.url).to eq("#{request.base_url}?a=1")
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "pushes an easy back into the pool" do
|
24
|
+
easy = double.as_null_object
|
25
|
+
allow(Typhoeus::Pool).to receive(:get).and_return(easy)
|
26
|
+
expect(Typhoeus::Pool).to receive(:release).with(easy)
|
27
|
+
request.url
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe ".new" do
|
32
|
+
it "stores base_url" do
|
33
|
+
expect(request.base_url).to eq(base_url)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "stores options" do
|
37
|
+
expect(request.options).to eq(options)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "stores original options" do
|
41
|
+
expect(request.original_options).to eq(options)
|
42
|
+
expect(request.original_options).to_not be(request.options)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "sets defaults" do
|
46
|
+
expect(request.options[:headers]['User-Agent']).to be
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "set_defaults" do
|
51
|
+
context "when header with user agent" do
|
52
|
+
let(:options) { {:headers => {'User-Agent' => "Custom"} } }
|
53
|
+
|
54
|
+
it "doesn't modify user agent" do
|
55
|
+
expect(request.options[:headers]['User-Agent']).to eq("Custom")
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when header without user agent" do
|
60
|
+
let(:options) { {:headers => {} } }
|
61
|
+
|
62
|
+
it "add user agent" do
|
63
|
+
agent = request.options[:headers]['User-Agent']
|
64
|
+
expect(agent).to eq(Typhoeus::USER_AGENT)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
context "when Config.user_agent set" do
|
69
|
+
before { Typhoeus.configure { |config| config.user_agent = "Default" } }
|
70
|
+
after { Typhoeus.configure { |config| config.user_agent = nil } }
|
71
|
+
|
72
|
+
context "with headers" do
|
73
|
+
let(:options) { {:headers => { "User-Agent" => "Fubar" } } }
|
74
|
+
|
75
|
+
it "uses the request options' user agent" do
|
76
|
+
expect(request.options[:headers]["User-Agent"]).to eq("Fubar")
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
context "without headers" do
|
81
|
+
let(:options) { {:headers => {} } }
|
82
|
+
|
83
|
+
it "uses the global options' user agent" do
|
84
|
+
expect(request.options[:headers]["User-Agent"]).to eq("Default")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context "when Config.verbose set" do
|
90
|
+
before { Typhoeus.configure { |config| config.verbose = true} }
|
91
|
+
after { Typhoeus.configure { |config| config.verbose = false} }
|
92
|
+
|
93
|
+
it "respects" do
|
94
|
+
expect(request.options[:verbose]).to be_truthy
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "when maxredirs" do
|
99
|
+
context "when not set" do
|
100
|
+
it "defaults to 50" do
|
101
|
+
expect(request.options[:maxredirs]).to be(50)
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "when set" do
|
106
|
+
let(:options) { {:maxredirs => 1} }
|
107
|
+
|
108
|
+
it "respects" do
|
109
|
+
expect(request.options[:maxredirs]).to be(1)
|
110
|
+
end
|
111
|
+
end
|
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
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "#eql?" do
|
133
|
+
context "when another class" do
|
134
|
+
let(:other) { "" }
|
135
|
+
|
136
|
+
it "returns false" do
|
137
|
+
expect(request).to_not eql other
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
context "when same class" do
|
142
|
+
let(:other) { Typhoeus::Request.new("base_url", options) }
|
143
|
+
|
144
|
+
context "when other base_url" do
|
145
|
+
it "returns false" do
|
146
|
+
expect(request).to_not eql other
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
context "when same base_url and other options" do
|
151
|
+
let(:other) { Typhoeus::Request.new(base_url, {}) }
|
152
|
+
|
153
|
+
it "returns false" do
|
154
|
+
expect(request).to_not eql other
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
context "when same base_url and options" do
|
160
|
+
context "when same order" do
|
161
|
+
let(:other) { Typhoeus::Request.new(base_url, options) }
|
162
|
+
|
163
|
+
it "returns true" do
|
164
|
+
expect(request).to eql other
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
context "when different order" do
|
169
|
+
let(:other_options) {
|
170
|
+
{:headers => { 'User-Agent' => "Fubar", 'Expect' => ""}, :verbose => true }
|
171
|
+
}
|
172
|
+
let(:other) { Typhoeus::Request.new(base_url, other_options)}
|
173
|
+
|
174
|
+
it "returns true" do
|
175
|
+
expect(request).to eql other
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
181
|
+
|
182
|
+
describe "#hash" do
|
183
|
+
context "when request.eql?(other)" do
|
184
|
+
context "when different order" do
|
185
|
+
let(:other_options) {
|
186
|
+
{:headers => { 'User-Agent' => "Fubar", 'Expect' => "" }, :verbose => true }
|
187
|
+
}
|
188
|
+
let(:other) { Typhoeus::Request.new(base_url, other_options)}
|
189
|
+
|
190
|
+
it "has same hashes" do
|
191
|
+
expect(request.hash).to eq(other.hash)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
|
195
|
+
context "when same order" do
|
196
|
+
let(:other) { Typhoeus::Request.new(base_url, options) }
|
197
|
+
|
198
|
+
it "has same hashes" do
|
199
|
+
expect(request.hash).to eq(other.hash)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context "when hashes with different orders are contained in arrays" do
|
204
|
+
let(:request) { Typhoeus::Request.new(base_url, :params => [{:b => 2, :a => 1}]) }
|
205
|
+
let(:other) { Typhoeus::Request.new(base_url, :params => [{:a => 1, :b => 2}]) }
|
206
|
+
it "has different hashes" do
|
207
|
+
expect(request.hash).to eq(other.hash)
|
208
|
+
end
|
209
|
+
end
|
210
|
+
end
|
211
|
+
|
212
|
+
context "when not request.eql?(other)" do
|
213
|
+
let(:request) { Typhoeus::Request.new(base_url, :params => {:foo => 'bar'}) }
|
214
|
+
let(:other) { Typhoeus::Request.new(base_url, :params => {:foo => 'baz'}) }
|
215
|
+
|
216
|
+
it "has different hashes" do
|
217
|
+
expect(request.hash).to_not eq(other.hash)
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
describe "#encoded_body" do
|
223
|
+
let(:request) {
|
224
|
+
Typhoeus::Request.new("www.example.com",:body => {:a => 1})
|
225
|
+
}
|
226
|
+
|
227
|
+
it "returns encoded body" do
|
228
|
+
expect(request.encoded_body).to eq("a=1")
|
229
|
+
end
|
230
|
+
end
|
231
|
+
|
232
|
+
end
|