typhoeus 0.6.3 → 0.6.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. checksums.yaml +15 -0
  2. data/.gitignore +8 -0
  3. data/.rspec +4 -0
  4. data/.travis.yml +10 -0
  5. data/CHANGELOG.md +7 -1
  6. data/CONTRIBUTING.md +16 -0
  7. data/Guardfile +9 -0
  8. data/README.md +303 -1
  9. data/UPGRADE.md +55 -0
  10. data/lib/rack/typhoeus/middleware/params_decoder.rb +1 -1
  11. data/lib/rack/typhoeus/middleware/params_decoder/helper.rb +6 -2
  12. data/lib/typhoeus/adapters/faraday.rb +4 -1
  13. data/lib/typhoeus/easy_factory.rb +36 -15
  14. data/lib/typhoeus/hydra/before.rb +2 -1
  15. data/lib/typhoeus/hydra/cacheable.rb +1 -0
  16. data/lib/typhoeus/hydra/memoizable.rb +1 -0
  17. data/lib/typhoeus/hydra/queueable.rb +15 -3
  18. data/lib/typhoeus/request.rb +6 -2
  19. data/lib/typhoeus/request/marshal.rb +3 -2
  20. data/lib/typhoeus/response/informations.rb +4 -0
  21. data/lib/typhoeus/version.rb +1 -1
  22. data/perf/profile.rb +14 -0
  23. data/perf/vs_nethttp.rb +64 -0
  24. data/spec/rack/typhoeus/middleware/params_decoder/helper_spec.rb +132 -0
  25. data/spec/rack/typhoeus/middleware/params_decoder_spec.rb +31 -0
  26. data/spec/spec_helper.rb +35 -0
  27. data/spec/support/localhost_server.rb +94 -0
  28. data/spec/support/server.rb +108 -0
  29. data/spec/typhoeus/adapters/faraday_spec.rb +245 -0
  30. data/spec/typhoeus/config_spec.rb +15 -0
  31. data/spec/typhoeus/easy_factory_spec.rb +96 -0
  32. data/spec/typhoeus/errors/no_stub_spec.rb +13 -0
  33. data/spec/typhoeus/expectation_spec.rb +273 -0
  34. data/spec/typhoeus/hydra/addable_spec.rb +22 -0
  35. data/spec/typhoeus/hydra/before_spec.rb +97 -0
  36. data/spec/typhoeus/hydra/block_connection_spec.rb +18 -0
  37. data/spec/typhoeus/hydra/cacheable_spec.rb +68 -0
  38. data/spec/typhoeus/hydra/memoizable_spec.rb +53 -0
  39. data/spec/typhoeus/hydra/queueable_spec.rb +34 -0
  40. data/spec/typhoeus/hydra/runnable_spec.rb +155 -0
  41. data/spec/typhoeus/hydra/stubbable_spec.rb +28 -0
  42. data/spec/typhoeus/hydra_spec.rb +26 -0
  43. data/spec/typhoeus/pool_spec.rb +79 -0
  44. data/spec/typhoeus/request/actions_spec.rb +19 -0
  45. data/spec/typhoeus/request/before_spec.rb +92 -0
  46. data/spec/typhoeus/request/block_connection_spec.rb +75 -0
  47. data/spec/typhoeus/request/cacheable_spec.rb +80 -0
  48. data/spec/typhoeus/request/callbacks_spec.rb +91 -0
  49. data/spec/typhoeus/request/marshal_spec.rb +62 -0
  50. data/spec/typhoeus/request/memoizable_spec.rb +34 -0
  51. data/spec/typhoeus/request/operations_spec.rb +70 -0
  52. data/spec/typhoeus/request/responseable_spec.rb +13 -0
  53. data/spec/typhoeus/request/stubbable_spec.rb +27 -0
  54. data/spec/typhoeus/request_spec.rb +131 -0
  55. data/spec/typhoeus/response/header_spec.rb +97 -0
  56. data/spec/typhoeus/response/informations_spec.rb +218 -0
  57. data/spec/typhoeus/response/status_spec.rb +218 -0
  58. data/spec/typhoeus/response_spec.rb +81 -0
  59. data/spec/typhoeus_spec.rb +105 -0
  60. data/typhoeus.gemspec +25 -0
  61. metadata +101 -27
@@ -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,92 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Request::Before do
4
+ let(:request) { Typhoeus::Request.new("") }
5
+
6
+ describe "#queue" do
7
+ context "when before" do
8
+ context "when one" do
9
+ it "executes" do
10
+ Typhoeus.before { |r| String.new(r.base_url) }
11
+ String.should_receive(:new).and_return("")
12
+ request.run
13
+ end
14
+
15
+ context "when true" do
16
+ it "calls super" do
17
+ Typhoeus.before { true }
18
+ Typhoeus::Expectation.should_receive(:response_for)
19
+ request.run
20
+ end
21
+ end
22
+
23
+ context "when false" do
24
+ it "doesn't call super" do
25
+ Typhoeus.before { false }
26
+ Typhoeus::Expectation.should_receive(:response_for).never
27
+ request.run
28
+ end
29
+
30
+ it "returns response" do
31
+ Typhoeus.before { |r| r.response = 1; false }
32
+ expect(request.run).to be(1)
33
+ end
34
+ end
35
+
36
+ context "when a response" do
37
+ it "doesn't call super" do
38
+ Typhoeus.before { Typhoeus::Response.new }
39
+ Typhoeus::Expectation.should_receive(:response_for).never
40
+ request.run
41
+ end
42
+
43
+ it "returns response" do
44
+ Typhoeus.before { |r| r.response = Typhoeus::Response.new }
45
+ expect(request.run).to be_a(Typhoeus::Response)
46
+ end
47
+ end
48
+ end
49
+
50
+ context "when multi" do
51
+ context "when all true" do
52
+ before { 3.times { Typhoeus.before { |r| String.new(r.base_url) } } }
53
+
54
+ it "calls super" do
55
+ Typhoeus::Expectation.should_receive(:response_for)
56
+ request.run
57
+ end
58
+
59
+ it "executes all" do
60
+ String.should_receive(:new).exactly(3).times.and_return("")
61
+ request.run
62
+ end
63
+ end
64
+
65
+ context "when middle false" do
66
+ before do
67
+ Typhoeus.before { |r| String.new(r.base_url) }
68
+ Typhoeus.before { |r| String.new(r.base_url); nil }
69
+ Typhoeus.before { |r| String.new(r.base_url) }
70
+ end
71
+
72
+ it "doesn't call super" do
73
+ Typhoeus::Expectation.should_receive(:response_for).never
74
+ request.run
75
+ end
76
+
77
+ it "executes only two" do
78
+ String.should_receive(:new).exactly(2).times.and_return("")
79
+ request.run
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ context "when no before" do
86
+ it "calls super" do
87
+ Typhoeus::Expectation.should_receive(:response_for)
88
+ request.run
89
+ end
90
+ end
91
+ end
92
+ 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_true
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_false
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_true
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_false
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_false
72
+ end
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,80 @@
1
+ require 'spec_helper'
2
+
3
+ describe Typhoeus::Request::Cacheable do
4
+ let(:cache) {
5
+ Class.new do
6
+ attr_reader :memory
7
+
8
+ def initialize
9
+ @memory = {}
10
+ end
11
+
12
+ def get(request)
13
+ memory[request]
14
+ end
15
+
16
+ def set(request, response)
17
+ memory[request] = response
18
+ end
19
+ end.new
20
+ }
21
+ let(:options) { {} }
22
+ let(:request) { Typhoeus::Request.new("http://localhost:3001", options) }
23
+ let(:response) { Typhoeus::Response.new }
24
+
25
+ before { Typhoeus::Config.cache = cache }
26
+ after { Typhoeus::Config.cache = false }
27
+
28
+ describe "#response=" do
29
+ context "when cache activated" do
30
+ context "when nequest new" do
31
+ it "caches response" do
32
+ request.response = response
33
+ expect(cache.memory[request]).to be
34
+ end
35
+ end
36
+
37
+ context "when request in memory" do
38
+ before { cache.memory[request] = response }
39
+
40
+ it "finishes request" do
41
+ request.should_receive(:finish).with(response)
42
+ request.run
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ describe "#run" do
49
+ context "when cache activated" do
50
+ before { Typhoeus::Config.cache = cache }
51
+ after { Typhoeus::Config.cache = false }
52
+
53
+ context "when request new" do
54
+ it "fetches response" do
55
+ expect(request.response).to_not be(response)
56
+ end
57
+ end
58
+
59
+ context "when request in memory" do
60
+ let(:response) { Typhoeus::Response.new }
61
+ before { cache.memory[request] = response }
62
+
63
+ it "finishes request" do
64
+ request.should_receive(:finish).with(response)
65
+ request.run
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#cache_ttl" do
72
+ context "when option[:cache_ttl]" do
73
+ let(:options) { {:cache_ttl => 1} }
74
+
75
+ it "returns" do
76
+ expect(request.cache_ttl).to be(1)
77
+ end
78
+ end
79
+ end
80
+ end
@@ -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].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}")).to have(1).items
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}")).to have(2).items
30
+ end
31
+ end
32
+ end
33
+ end
34
+
35
+ describe "#execute_callbacks" do
36
+ [:on_complete, :on_success, :on_failure].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
+ pending
88
+ end
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,62 @@
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
+ let(:base_url) { "http://www.google.com" }
9
+
10
+ %w(on_complete on_success on_failure).each do |name|
11
+ context "when #{name} handler" do
12
+ before { request.instance_variable_set("@#{name}", Proc.new{}) }
13
+
14
+ it "doesn't include @#{name}" do
15
+ expect(request.send(:marshal_dump).map(&:first)).to_not include("@#{name}")
16
+ end
17
+
18
+ it "doesn't raise when dumped" do
19
+ expect { Marshal.dump(request) }.to_not raise_error
20
+ end
21
+
22
+ context "when loading" do
23
+ let(:loaded) { Marshal.load(Marshal.dump(request)) }
24
+
25
+ it "includes base_url" do
26
+ expect(loaded.base_url).to eq(request.base_url)
27
+ end
28
+
29
+ it "doesn't include #{name}" do
30
+ expect(loaded.instance_variables).to_not include("@#{name}")
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'when run through hydra' do
37
+ let(:options) { {} }
38
+ let(:hydra) { Typhoeus::Hydra.new(options) }
39
+
40
+ before(:each) do
41
+ hydra.queue(request)
42
+ hydra.run
43
+ end
44
+
45
+ it "doesn't include @hydra" do
46
+ expect(request.send(:marshal_dump).map(&:first)).to_not include("@hydra")
47
+ end
48
+
49
+ context 'when loading' do
50
+ let(:loaded) { Marshal.load(Marshal.dump(request)) }
51
+
52
+ it "includes base_url" do
53
+ expect(loaded.base_url).to eq(request.base_url)
54
+ end
55
+
56
+ it "doesn't include #{name}" do
57
+ expect(loaded.instance_variables).to_not include("@hydra")
58
+ end
59
+ end
60
+ end
61
+ end
62
+ 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