excon 0.52.0 → 0.53.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of excon might be problematic. Click here for more details.

@@ -0,0 +1,28 @@
1
+ require 'spec_helper'
2
+
3
+ describe Excon::Test::Server do
4
+
5
+ context 'when the web server is webrick' do
6
+ it_should_behave_like "a excon test server", :webrick, 'basic.ru'
7
+ end
8
+
9
+
10
+ context 'when the web server is unicorn' do
11
+ context 'bound to a tcp socket' do
12
+ it_should_behave_like "a excon test server", :unicorn, 'streaming.ru'
13
+ end
14
+
15
+ context "bound to a unix socket" do
16
+ socket_uri = 'unix:///tmp/unicorn.socket'
17
+ it_should_behave_like "a excon test server", :unicorn, 'streaming.ru', socket_uri
18
+ end
19
+ end
20
+
21
+ context 'when the web server is puma' do
22
+ it_should_behave_like "a excon test server", :puma, 'streaming.ru'
23
+ end
24
+
25
+ context 'when the web server is a executable' do
26
+ it_should_behave_like "a excon test server", :exec, 'good.rb'
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # Todo: s/tests/specs when migration is complete
2
+ def get_abs_path(*parts)
3
+ File.join(File.expand_path('../../..', __FILE__), 'tests', *parts)
4
+ end
5
+
6
+ def data_path(*parts)
7
+ get_abs_path('data', *parts)
8
+ end
9
+
10
+ def rackup_path(*parts)
11
+ get_abs_path('rackups', *parts)
12
+ end
13
+
14
+ def webrick_path(*parts) rackup_path(*parts); end
15
+
16
+ def unicorn_path(*parts) rackup_path(*parts); end
17
+
18
+ def puma_path(*parts) rackup_path(*parts); end
19
+
20
+ def exec_path(*parts)
21
+ get_abs_path('servers', *parts)
22
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe Excon::Connection do
4
+ include_context('test server', :webrick, 'basic.ru', before: :start, after: :stop)
5
+ context 'when an explicit uri is passed' do
6
+ let(:conn) do
7
+ Excon::Connection.new(host: '127.0.0.1',
8
+ hostname: '127.0.0.1',
9
+ nonblock: false,
10
+ port: 9292,
11
+ scheme: 'http',
12
+ ssl_verify_peer: false)
13
+ end
14
+
15
+ describe '.new' do
16
+ it 'returns an instance' do
17
+ expect(conn).to be_instance_of Excon::Connection
18
+ end
19
+ end
20
+
21
+ context "when :method is :get and :path is /content-length/100" do
22
+ describe "#request" do
23
+ let(:response) do
24
+ response = conn.request(method: :get, path: '/content-length/100')
25
+ end
26
+ it 'returns an Excon::Response' do
27
+ expect(response).to be_instance_of Excon::Response
28
+ end
29
+ describe Excon::Response do
30
+ describe '#status' do
31
+ it 'returns 200' do
32
+ expect(response.status).to eq 200
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
38
+ include_examples 'a basic client'
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Excon do
4
+ context "when dispatching requests" do
5
+ context('to a server that does not supply response headers') do
6
+ include_context("test server", :exec, 'bad.rb', :before => :start, :after => :stop )
7
+ before(:all) do
8
+ @conn = Excon.new('http://127.0.0.1:9292')
9
+ end
10
+
11
+ context('when no block is given') do
12
+ it 'should rescue from an EOFError and return response' do
13
+ body = @conn.request(:method => :get, :path => '/eof/no_content_length_and_no_chunking').body
14
+ expect(body).to eq 'hello'
15
+ end
16
+ end
17
+
18
+ context('when a block is given') do
19
+ it 'should rescue from EOFError and return response' do
20
+ body = ""
21
+ response_block = lambda {|chunk, remaining, total| body << chunk }
22
+ @conn.request(:method => :get, :path => '/eof/no_content_length_and_no_chunking', :response_block => response_block)
23
+ expect(body).to eq 'hello'
24
+ end
25
+ end
26
+ end
27
+
28
+ context('to a server that prematurely aborts the request with no response') do
29
+ include_context("test server", :exec, 'eof.rb', :before => :start, :after => :stop )
30
+
31
+ it 'should raise an EOFError' do
32
+ expect { Excon.get('http://127.0.0.1:9292/eof') }.to raise_error(Excon::Errors::SocketError)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -1,126 +1,24 @@
1
1
  require 'excon'
2
2
  require 'excon/test/server'
3
+ require 'json'
3
4
 
4
- # This file was generated by the `rspec --init` command. Conventionally, all
5
- # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
- # The generated `.rspec` file contains `--require spec_helper` which will cause
7
- # this file to always be loaded, without a need to explicitly require it in any
8
- # files.
9
- #
10
- # Given that it is always loaded, you are encouraged to keep this file as
11
- # light-weight as possible. Requiring heavyweight dependencies from this file
12
- # will add to the boot time of your test suite on EVERY test run, even for an
13
- # individual file that may not need all of that loaded. Instead, consider making
14
- # a separate helper file that requires the additional dependencies and performs
15
- # the additional setup, and require it from the spec files that actually need
16
- # it.
17
- #
18
- # The `.rspec` file also contains a few flags that are not defaults but that
19
- # users commonly want.
20
- #
21
5
  # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
22
6
  RSpec.configure do |config|
23
- # rspec-expectations config goes here. You can use an alternate
24
- # assertion/expectation library such as wrong or the stdlib/minitest
25
- # assertions if you prefer.
26
7
  config.expect_with :rspec do |expectations|
27
- # This option will default to `true` in RSpec 4. It makes the `description`
28
- # and `failure_message` of custom matchers include text for helper methods
29
- # defined using `chain`, e.g.:
30
- # be_bigger_than(2).and_smaller_than(4).description
31
- # # => "be bigger than 2 and smaller than 4"
32
- # ...rather than:
33
- # # => "be bigger than 2"
34
8
  expectations.include_chain_clauses_in_custom_matcher_descriptions = true
35
9
  end
36
10
 
37
- # rspec-mocks config goes here. You can use an alternate test double
38
- # library (such as bogus or mocha) by changing the `mock_with` option here.
39
- config.mock_with :rspec do |mocks|
40
- # Prevents you from mocking or stubbing a method that does not exist on
41
- # a real object. This is generally recommended, and will default to
42
- # `true` in RSpec 4.
43
- mocks.verify_partial_doubles = true
11
+ config.mock_with :rspec do |mocks|
12
+ mocks.verify_partial_doubles = true
44
13
  end
45
14
 
46
- # This option will default to `:apply_to_host_groups` in RSpec 4 (and will
47
- # have no way to turn it off -- the option exists only for backwards
48
- # compatibility in RSpec 3). It causes shared context metadata to be
49
- # inherited by the metadata hash of host groups and examples, rather than
50
- # triggering implicit auto-inclusion in groups with matching metadata.
51
- config.shared_context_metadata_behavior = :apply_to_host_groups
52
-
53
- # The settings below are suggested to provide a good initial experience
54
- # with RSpec, but feel free to customize to your heart's content.
55
- =begin
56
- # This allows you to limit a spec run to individual examples or groups
57
- # you care about by tagging them with `:focus` metadata. When nothing
58
- # is tagged with `:focus`, all examples get run. RSpec also provides
59
- # aliases for `it`, `describe`, and `context` that include `:focus`
60
- # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
61
- config.filter_run_when_matching :focus
62
-
63
- # Allows RSpec to persist some state between runs in order to support
64
- # the `--only-failures` and `--next-failure` CLI options. We recommend
65
- # you configure your source control system to ignore this file.
66
- config.example_status_persistence_file_path = "spec/examples.txt"
67
-
68
- # Limits the available syntax to the non-monkey patched syntax that is
69
- # recommended. For more details, see:
70
- # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
71
- # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
72
- # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
73
- config.disable_monkey_patching!
74
-
75
- # This setting enables warnings. It's recommended, but in some cases may
76
- # be too noisy due to issues in dependencies.
77
- config.warnings = true
78
-
79
- # Many RSpec users commonly either run the entire suite or an individual
80
- # file, and it's useful to allow more verbose output when running an
81
- # individual spec file.
82
15
  if config.files_to_run.one?
83
- # Use the documentation formatter for detailed output,
84
- # unless a formatter has already been configured
85
- # (e.g. via a command-line flag).
86
16
  config.default_formatter = 'doc'
87
17
  end
88
-
89
- # Print the 10 slowest examples and example groups at the
90
- # end of the spec run, to help surface which specs are running
91
- # particularly slow.
92
- config.profile_examples = 10
93
-
94
- # Run specs in random order to surface order dependencies. If you find an
95
- # order dependency and want to debug it, you can fix the order by providing
96
- # the seed, which is printed after each run.
97
- # --seed 1234
98
- config.order = :random
99
-
100
- # Seed global randomization in this process using the `--seed` CLI option.
101
- # Setting this allows you to use `--seed` to deterministically reproduce
102
- # test failures related to randomization by passing the same `--seed` value
103
- # as the one that triggered the failure.
104
- Kernel.srand config.seed
105
- =end
106
-
107
- end
108
-
109
- # Todo: s/tests/specs when migration is complete
110
- def get_abs_path(*parts)
111
- File.join(File.expand_path('../..', __FILE__), 'tests', *parts)
112
- end
113
-
114
- def rackup_path(*parts)
115
- get_abs_path('rackups', *parts)
116
18
  end
117
19
 
118
- def webrick_path(*parts) rackup_path(*parts); end
119
-
120
- def unicorn_path(*parts) rackup_path(*parts); end
20
+ # Load helpers
21
+ Dir["./spec/helpers/**/*.rb"].sort.each { |f| require f}
121
22
 
122
- def puma_path(*parts) rackup_path(*parts); end
123
-
124
- def exec_path(*parts)
125
- get_abs_path('servers', *parts)
126
- end
23
+ # Load shared examples and contexts
24
+ Dir["./spec/support/**/*.rb"].sort.each { |f| require f}
@@ -0,0 +1,83 @@
1
+ # TODO: Clean up this doc and dry up the conditionals
2
+ #
3
+ # Required params:
4
+ # plugin (e.g., webrick, unicorn, etc)
5
+ # file (e.g. a rackup )
6
+ #
7
+ # Optional params:
8
+ # optional paramters may given as a hash
9
+ # opts may contain a bind argument
10
+ # opts may also contain before and after options
11
+ #
12
+ # In its simplest form:
13
+ # { :before => :start, :after => :stop }
14
+ #
15
+ # With lambdas, which recieve @server as an argument
16
+ # { before: lambda {|s| s.start }, after: lambda { |s| s.stop} }
17
+ #
18
+ # In both the cases above, before defaults to before(:all)
19
+ # This can be circumvented with a Hash
20
+ # { before: { :context => :start }, after: { :context => :stop } }
21
+ # or
22
+ # { before: { context: lambda { |s| s.start } }, after: { context: lambda { |s| s.stop } } }
23
+
24
+ shared_context "test server" do |plugin, file, opts = {}|
25
+ plugin = plugin.to_sym unless plugin.is_a? Symbol
26
+ if plugin == :unicorn && RUBY_PLATFORM == "java"
27
+ before { skip("until unicorn supports jruby") }
28
+ end
29
+ abs_file = Object.send("#{plugin}_path", file)
30
+ args = { plugin => abs_file}
31
+ args[:bind] = opts[:bind] if opts.key? :bind
32
+
33
+
34
+ before_hook = opts.key?(:before) && (opts[:before].is_a?(Symbol) || opts[:before].is_a?(Proc) || opts[:before].is_a?(Hash))
35
+
36
+ if before_hook && opts[:before].is_a?(Hash)
37
+ event = opts[:before].keys.first
38
+ before(event) {
39
+ @server = Excon::Test::Server.new(args)
40
+ if opts[:before][event].is_a? Symbol
41
+ @server.send(opts[:before][event])
42
+ else
43
+ opts[:before][event].call(@server)
44
+ end
45
+ }
46
+ elsif
47
+ before(:all) {
48
+ @server = Excon::Test::Server.new(args)
49
+ before_hook = opts.key?(:before) && (opts[:before].is_a?(Symbol) || opts[:before].is_a?(Proc) || opts[:before].is_a?(Hash))
50
+
51
+ if before_hook
52
+ if opts[:before].is_a? Symbol
53
+ @server.send(opts[:before])
54
+ else
55
+ opts[:before].call(@server)
56
+ end
57
+ end
58
+ }
59
+ end
60
+
61
+ after_hook = opts.key?(:after) && (opts[:after].is_a?(Symbol) || opts[:after].is_a?(Proc) || opts[:after].is_a?(Hash))
62
+
63
+ if after_hook && opts[:after].is_a?(Hash)
64
+ event = opts[:after].keys.first
65
+ after(event) {
66
+ if opts[:after][event].is_a? Symbol
67
+ @server.send(opts[:after][event])
68
+ else
69
+ opts[:after][event].call(@server)
70
+ end
71
+ }
72
+ elsif after_hook
73
+ after(:all) {
74
+ if opts[:after].is_a? Symbol
75
+ @server.send(opts[:after])
76
+ elsif opts[:after].is_a? Hash
77
+
78
+ else
79
+ opts[:after].call(@server)
80
+ end
81
+ }
82
+ end
83
+ end
@@ -0,0 +1,207 @@
1
+ shared_examples_for 'a basic client' do |url = 'http://127.0.0.1:9292', opts = {}|
2
+ # TODO: Ditch iterator and manually write a context for each set of options
3
+ ([true, false] * 2).combination(2).to_a.uniq.each do |nonblock, persistent|
4
+ context "when nonblock is #{nonblock} and persistent is #{persistent}" do
5
+ opts = opts.merge(ssl_verify_peer: false, nonblock: nonblock, persistent: persistent)
6
+
7
+ let(:conn) { Excon.new(url, opts) }
8
+
9
+ context 'when :method is get and :path is /content-length/100' do
10
+ describe '#request' do
11
+ let(:response) do
12
+ conn.request(method: :get, path: '/content-length/100')
13
+ end
14
+
15
+ it 'returns an Excon::Response' do
16
+ expect(response).to be_instance_of Excon::Response
17
+ end
18
+ describe Excon::Response do
19
+ describe '#status' do
20
+ it 'returns 200' do
21
+ expect(response.status).to eq 200
22
+ end
23
+
24
+ it '#status returns 200' do
25
+ expect(response[:status]).to eq 200
26
+ end
27
+ end
28
+ describe '#headers' do
29
+ it '["Content-Length"] returns 100' do
30
+ expect(response.headers['Content-Length']).to eq '100'
31
+ end
32
+ it '["Content-Type"] returns "text/html;charset=utf-8"' do
33
+ expect(response.headers['Content-Type']).to eq 'text/html;charset=utf-8'
34
+ end
35
+
36
+ it "['Date'] returns a valid date" do
37
+ if RUBY_PLATFORM == 'java' && conn.data[:scheme] == Excon::UNIX
38
+ skip('until puma responds with a date header')
39
+ else
40
+ time = Time.parse(response.headers['Date'])
41
+ expect(time.is_a?(Time)).to be true
42
+ end
43
+ end
44
+
45
+ it "['Server'] matches /^WEBrick/" do
46
+ pending('until unix_socket response has server header') if conn.data[:scheme] == Excon::UNIX
47
+ expect(!!(response.headers['Server'] =~ /^WEBrick/)).to be true
48
+ end
49
+
50
+ it "['Custom'] returns Foo: bar" do
51
+ expect(response.headers['Custom']).to eq 'Foo: bar'
52
+ end
53
+ end
54
+ describe '#remote_ip' do
55
+ it 'returns 127.0.0.1' do
56
+ pending('until pigs can fly') if conn.data[:scheme] == Excon::UNIX
57
+ expect(response.remote_ip).to eq '127.0.0.1'
58
+ end
59
+ end
60
+ end
61
+
62
+ context('when tcp_nodelay is true') do
63
+ describe '#request' do
64
+ response = nil
65
+ options = opts.merge(ssl_verify_peer: false, nonblock: nonblock, tcp_nodelay: true)
66
+ connection = Excon.new(url, options)
67
+
68
+ it 'returns an Excon::Response' do
69
+ expect do
70
+ response = connection.request(method: :get, path: '/content-length/100')
71
+ end.to_not raise_error
72
+ end
73
+
74
+ describe Excon::Response do
75
+ describe '#body' do
76
+ describe '.status' do
77
+ it '#returns 200' do
78
+ expect(response.status).to eq 200
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+
87
+ context 'when utilizing deprecated block usage' do
88
+ describe '#request' do
89
+ data = []
90
+ it 'yields with a chunk, remaining length, and total length' do
91
+ expect do
92
+ conn.request(method: :get, path: '/content-length/100') do |chunk, remaining_length, total_length|
93
+ data = [chunk, remaining_length, total_length]
94
+ end
95
+ end.to_not raise_error
96
+ end
97
+ it 'completes with expected data' do
98
+ expect(data).to eq ['x' * 100, 0, 100]
99
+ end
100
+ end
101
+ end
102
+
103
+ context 'when utilizing response_block usage' do
104
+ describe '#request' do
105
+ data = []
106
+ it 'yields a chunk, remaining length, and total_length' do
107
+ response_block = lambda do |chunk, remaining_length, total_length|
108
+ data = [chunk, remaining_length, total_length]
109
+ end
110
+ expect do
111
+ conn.request(method: :get, path: '/content-length/100', response_block: response_block)
112
+ end.to_not raise_error
113
+ end
114
+ it 'completes with expected data' do
115
+ expect(data).to eq ['x' * 100, 0, 100]
116
+ end
117
+ end
118
+ end
119
+ context 'when method is :post' do
120
+ context 'when :path is /body-sink' do
121
+ context 'when a body parameter is supplied' do
122
+ response = nil
123
+ it 'returns an Excon::Response' do
124
+ response = conn.request(method: :post, path: '/body-sink', headers: { 'Content-Type' => 'text/plain' }, body: 'x' * 5_000_000)
125
+ expect(response).to be_instance_of Excon::Response
126
+ end
127
+ describe Excon::Response do
128
+ describe '#body' do
129
+ it 'equals "5000000"' do
130
+ expect(response.body).to eq '5000000'
131
+ end
132
+ end
133
+ end
134
+ end
135
+ context 'when the body parameter is an empty string' do
136
+ response = nil
137
+
138
+ it 'returns an Excon::Response' do
139
+ response = conn.request(method: :post, path: '/body-sink', headers: { 'Content-Type' => 'text/plain' }, body: '')
140
+ expect(response).to be_instance_of Excon::Response
141
+ end
142
+ describe Excon::Response do
143
+ describe '#body' do
144
+ it 'equals "0"' do
145
+ expect(response.body).to eq '0'
146
+ end
147
+ end
148
+ end
149
+ end
150
+ end
151
+
152
+ context 'when :path is /echo' do
153
+ context('when a file handle is the body paramter') do
154
+ describe Excon::Response do
155
+ it '#body equals "x" * 100 + "\n"' do
156
+ file_path = data_path('xs')
157
+ response = conn.request(method: :post, path: '/echo', body: File.open(file_path))
158
+ expect(response.body).to eq 'x' * 100 + "\n"
159
+ end
160
+ end
161
+ end
162
+
163
+ context 'when a string is the body paramter' do
164
+ context 'without request_block' do
165
+ describe Excon::Response do
166
+ it "#body equals 'x' * 100)" do
167
+ response = conn.request(method: :post, path: '/echo', body: 'x' * 100)
168
+ expect(response.body).to eq 'x' * 100
169
+ end
170
+ end
171
+ end
172
+
173
+ context 'when a request_block paramter is supplied' do
174
+ describe Excon::Response do
175
+ it "#body equals'x' * 100" do
176
+ data = ['x'] * 100
177
+ request_block = lambda do
178
+ data.shift.to_s
179
+ end
180
+ response = conn.request(method: :post, path: '/echo', request_block: request_block)
181
+ expect(response.body).to eq 'x' * 100
182
+ end
183
+ end
184
+ end
185
+
186
+ context('when a multi-byte string is the body paramter') do
187
+ body = "\xC3\xBC" * 100
188
+ headers = { 'Custom' => body.dup }
189
+ if RUBY_VERSION >= '1.9'
190
+ body.force_encoding('BINARY')
191
+ headers['Custom'].force_encoding('UTF-8')
192
+ end
193
+ describe Excon::Response do
194
+ it '#body properly concatenates request+headers and body' do
195
+ response = conn.request(method: :post, path: '/echo',
196
+ headers: headers, body: body)
197
+ expect(response.body).to eq body
198
+ end
199
+ end
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
206
+ end
207
+ end