faraday 0.11.0 → 0.17.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGELOG.md +232 -0
- data/LICENSE.md +1 -1
- data/README.md +151 -13
- data/Rakefile +13 -0
- data/lib/faraday/adapter/em_http.rb +9 -9
- data/lib/faraday/adapter/em_synchrony.rb +5 -5
- data/lib/faraday/adapter/excon.rb +6 -4
- data/lib/faraday/adapter/httpclient.rb +5 -5
- data/lib/faraday/adapter/net_http.rb +27 -9
- data/lib/faraday/adapter/net_http_persistent.rb +34 -16
- data/lib/faraday/adapter/patron.rb +32 -13
- data/lib/faraday/adapter/rack.rb +1 -1
- data/lib/faraday/adapter/test.rb +21 -13
- data/lib/faraday/adapter/typhoeus.rb +4 -115
- data/lib/faraday/adapter.rb +2 -0
- data/lib/faraday/autoload.rb +1 -1
- data/lib/faraday/connection.rb +59 -12
- data/lib/faraday/deprecate.rb +109 -0
- data/lib/faraday/error.rb +130 -35
- data/lib/faraday/options.rb +31 -25
- data/lib/faraday/parameters.rb +2 -1
- data/lib/faraday/rack_builder.rb +26 -2
- data/lib/faraday/request/multipart.rb +7 -2
- data/lib/faraday/request/retry.rb +76 -17
- data/lib/faraday/request.rb +20 -0
- data/lib/faraday/response/logger.rb +3 -3
- data/lib/faraday/response/raise_error.rb +7 -3
- data/lib/faraday/response.rb +3 -3
- data/lib/faraday/utils.rb +18 -9
- data/lib/faraday.rb +9 -5
- data/spec/faraday/deprecate_spec.rb +147 -0
- data/spec/faraday/error_spec.rb +102 -0
- data/spec/faraday/response/raise_error_spec.rb +106 -0
- data/spec/spec_helper.rb +105 -0
- data/test/adapters/default_test.rb +14 -0
- data/test/adapters/em_http_test.rb +30 -0
- data/test/adapters/em_synchrony_test.rb +32 -0
- data/test/adapters/excon_test.rb +30 -0
- data/test/adapters/httpclient_test.rb +34 -0
- data/test/adapters/integration.rb +263 -0
- data/test/adapters/logger_test.rb +136 -0
- data/test/adapters/net_http_persistent_test.rb +114 -0
- data/test/adapters/net_http_test.rb +79 -0
- data/test/adapters/patron_test.rb +40 -0
- data/test/adapters/rack_test.rb +38 -0
- data/test/adapters/test_middleware_test.rb +157 -0
- data/test/adapters/typhoeus_test.rb +38 -0
- data/test/authentication_middleware_test.rb +65 -0
- data/test/composite_read_io_test.rb +109 -0
- data/test/connection_test.rb +738 -0
- data/test/env_test.rb +268 -0
- data/test/helper.rb +75 -0
- data/test/live_server.rb +67 -0
- data/test/middleware/instrumentation_test.rb +88 -0
- data/test/middleware/retry_test.rb +282 -0
- data/test/middleware_stack_test.rb +260 -0
- data/test/multibyte.txt +1 -0
- data/test/options_test.rb +333 -0
- data/test/parameters_test.rb +157 -0
- data/test/request_middleware_test.rb +126 -0
- data/test/response_middleware_test.rb +72 -0
- data/test/strawberry.rb +2 -0
- data/test/utils_test.rb +98 -0
- metadata +48 -7
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class EMHttpTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :em_http end
|
7
|
+
|
8
|
+
Integration.apply(self, :Parallel) do
|
9
|
+
# https://github.com/eventmachine/eventmachine/pull/289
|
10
|
+
undef :test_timeout
|
11
|
+
|
12
|
+
def test_binds_local_socket
|
13
|
+
host = '1.2.3.4'
|
14
|
+
conn = create_connection :request => { :bind => { :host => host } }
|
15
|
+
assert_equal host, conn.options[:bind][:host]
|
16
|
+
end
|
17
|
+
end unless jruby? and ssl_mode?
|
18
|
+
# https://github.com/eventmachine/eventmachine/issues/180
|
19
|
+
|
20
|
+
def test_custom_adapter_config
|
21
|
+
url = URI('https://example.com:1234')
|
22
|
+
|
23
|
+
adapter = Faraday::Adapter::EMHttp.new nil, inactivity_timeout: 20
|
24
|
+
|
25
|
+
req = adapter.create_request(url: url, request: {})
|
26
|
+
|
27
|
+
assert_equal 20, req.connopts.inactivity_timeout
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class EMSynchronyTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :em_synchrony end
|
7
|
+
|
8
|
+
unless jruby?
|
9
|
+
Integration.apply(self, :Parallel) do
|
10
|
+
# https://github.com/eventmachine/eventmachine/pull/289
|
11
|
+
undef :test_timeout
|
12
|
+
|
13
|
+
def test_binds_local_socket
|
14
|
+
host = '1.2.3.4'
|
15
|
+
conn = create_connection :request => { :bind => { :host => host } }
|
16
|
+
#put conn.get('/who-am-i').body
|
17
|
+
assert_equal host, conn.options[:bind][:host]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_custom_adapter_config
|
23
|
+
url = URI('https://example.com:1234')
|
24
|
+
|
25
|
+
adapter = Faraday::Adapter::EMSynchrony.new nil, inactivity_timeout: 20
|
26
|
+
|
27
|
+
req = adapter.create_request(url: url, request: {})
|
28
|
+
|
29
|
+
assert_equal 20, req.connopts.inactivity_timeout
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class ExconTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :excon end
|
7
|
+
|
8
|
+
Integration.apply(self, :NonParallel) do
|
9
|
+
# https://github.com/geemus/excon/issues/126 ?
|
10
|
+
undef :test_timeout if ssl_mode?
|
11
|
+
|
12
|
+
# Excon lets OpenSSL::SSL::SSLError be raised without any way to
|
13
|
+
# distinguish whether it happened because of a 407 proxy response
|
14
|
+
undef :test_proxy_auth_fail if ssl_mode?
|
15
|
+
|
16
|
+
# https://github.com/geemus/excon/issues/358
|
17
|
+
undef :test_connection_error if RUBY_VERSION >= '2.1.0'
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_custom_adapter_config
|
21
|
+
url = URI('https://example.com:1234')
|
22
|
+
|
23
|
+
adapter = Faraday::Adapter::Excon.new nil, debug_request: true
|
24
|
+
|
25
|
+
conn = adapter.create_connection({url: url}, {})
|
26
|
+
|
27
|
+
assert_equal true, conn.data[:debug_request]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class HttpclientTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :httpclient end
|
7
|
+
|
8
|
+
Integration.apply(self, :NonParallel, :Compression) do
|
9
|
+
def setup
|
10
|
+
require 'httpclient' unless defined?(HTTPClient)
|
11
|
+
HTTPClient::NO_PROXY_HOSTS.delete('localhost')
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_binds_local_socket
|
15
|
+
host = '1.2.3.4'
|
16
|
+
conn = create_connection :request => { :bind => { :host => host } }
|
17
|
+
assert_equal host, conn.options[:bind][:host]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_custom_adapter_config
|
21
|
+
adapter = Faraday::Adapter::HTTPClient.new do |client|
|
22
|
+
client.keep_alive_timeout = 20
|
23
|
+
client.ssl_config.timeout = 25
|
24
|
+
end
|
25
|
+
|
26
|
+
client = adapter.client
|
27
|
+
adapter.configure_client
|
28
|
+
|
29
|
+
assert_equal 20, client.keep_alive_timeout
|
30
|
+
assert_equal 25, client.ssl_config.timeout
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,263 @@
|
|
1
|
+
require 'forwardable'
|
2
|
+
require File.expand_path("../../helper", __FILE__)
|
3
|
+
Faraday.require_lib 'autoload'
|
4
|
+
|
5
|
+
module Adapters
|
6
|
+
# Adapter integration tests. To use, implement two methods:
|
7
|
+
#
|
8
|
+
# `#adapter` required. returns a symbol for the adapter middleware name
|
9
|
+
# `#adapter_options` optional. extra arguments for building an adapter
|
10
|
+
module Integration
|
11
|
+
def self.apply(base, *extra_features)
|
12
|
+
if base.live_server
|
13
|
+
features = [:Common]
|
14
|
+
features.concat extra_features
|
15
|
+
features << :SSL if base.ssl_mode?
|
16
|
+
features.each {|name| base.send(:include, self.const_get(name)) }
|
17
|
+
yield if block_given?
|
18
|
+
elsif !defined? @warned
|
19
|
+
warn "Warning: Not running integration tests against a live server."
|
20
|
+
warn "Start the server `ruby test/live_server.rb` and set the LIVE=1 env variable."
|
21
|
+
warn "See CONTRIBUTING for usage."
|
22
|
+
@warned = true
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module Parallel
|
27
|
+
def test_in_parallel
|
28
|
+
resp1, resp2 = nil, nil
|
29
|
+
|
30
|
+
connection = create_connection
|
31
|
+
connection.in_parallel do
|
32
|
+
resp1 = connection.get('echo?a=1')
|
33
|
+
resp2 = connection.get('echo?b=2')
|
34
|
+
assert connection.in_parallel?
|
35
|
+
assert_nil resp1.body
|
36
|
+
assert_nil resp2.body
|
37
|
+
end
|
38
|
+
assert !connection.in_parallel?
|
39
|
+
assert_equal 'get ?{"a"=>"1"}', resp1.body
|
40
|
+
assert_equal 'get ?{"b"=>"2"}', resp2.body
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
module NonParallel
|
45
|
+
def test_no_parallel_support
|
46
|
+
connection = create_connection
|
47
|
+
response = nil
|
48
|
+
|
49
|
+
err = capture_warnings do
|
50
|
+
connection.in_parallel do
|
51
|
+
response = connection.get('echo').body
|
52
|
+
end
|
53
|
+
end
|
54
|
+
assert response
|
55
|
+
assert_match "no parallel-capable adapter on Faraday stack", err
|
56
|
+
assert_match __FILE__, err
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
module Compression
|
61
|
+
def test_GET_handles_compression
|
62
|
+
res = get('echo_header', :name => 'accept-encoding')
|
63
|
+
assert_match(/\bgzip\b/, res.body)
|
64
|
+
assert_match(/\bdeflate\b/, res.body)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
module SSL
|
69
|
+
def test_GET_ssl_fails_with_bad_cert
|
70
|
+
ca_file = 'tmp/faraday-different-ca-cert.crt'
|
71
|
+
conn = create_connection(:ssl => {:ca_file => ca_file})
|
72
|
+
err = assert_raises Faraday::SSLError do
|
73
|
+
conn.get('/ssl')
|
74
|
+
end
|
75
|
+
assert_includes err.message, "certificate"
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
module Common
|
80
|
+
extend Forwardable
|
81
|
+
def_delegators :create_connection, :get, :head, :put, :post, :patch, :delete, :run_request
|
82
|
+
|
83
|
+
def test_GET_retrieves_the_response_body
|
84
|
+
assert_equal 'get', get('echo').body
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_GET_send_url_encoded_params
|
88
|
+
assert_equal %(get ?{"name"=>"zack"}), get('echo', :name => 'zack').body
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_GET_retrieves_the_response_headers
|
92
|
+
response = get('echo')
|
93
|
+
assert_match(/text\/plain/, response.headers['Content-Type'], 'original case fail')
|
94
|
+
assert_match(/text\/plain/, response.headers['content-type'], 'lowercase fail')
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_GET_handles_headers_with_multiple_values
|
98
|
+
assert_equal 'one, two', get('multi').headers['set-cookie']
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_GET_with_body
|
102
|
+
response = get('echo') do |req|
|
103
|
+
req.body = {'bodyrock' => true}
|
104
|
+
end
|
105
|
+
assert_equal %(get {"bodyrock"=>"true"}), response.body
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_GET_sends_user_agent
|
109
|
+
response = get('echo_header', {:name => 'user-agent'}, :user_agent => 'Agent Faraday')
|
110
|
+
assert_equal 'Agent Faraday', response.body
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_GET_ssl
|
114
|
+
expected = self.class.ssl_mode?.to_s
|
115
|
+
assert_equal expected, get('ssl').body
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_GET_reason_phrase
|
119
|
+
response = get('echo')
|
120
|
+
assert_equal "OK", response.reason_phrase
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_POST_send_url_encoded_params
|
124
|
+
assert_equal %(post {"name"=>"zack"}), post('echo', :name => 'zack').body
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_POST_send_url_encoded_nested_params
|
128
|
+
resp = post('echo', 'name' => {'first' => 'zack'})
|
129
|
+
assert_equal %(post {"name"=>{"first"=>"zack"}}), resp.body
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_POST_retrieves_the_response_headers
|
133
|
+
assert_match(/text\/plain/, post('echo').headers['content-type'])
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_POST_sends_files
|
137
|
+
resp = post('file') do |req|
|
138
|
+
req.body = {'uploaded_file' => Faraday::UploadIO.new(__FILE__, 'text/x-ruby')}
|
139
|
+
end
|
140
|
+
assert_equal "file integration.rb text/x-ruby #{File.size(__FILE__)}", resp.body
|
141
|
+
end
|
142
|
+
|
143
|
+
def test_PUT_send_url_encoded_params
|
144
|
+
assert_equal %(put {"name"=>"zack"}), put('echo', :name => 'zack').body
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_PUT_send_url_encoded_nested_params
|
148
|
+
resp = put('echo', 'name' => {'first' => 'zack'})
|
149
|
+
assert_equal %(put {"name"=>{"first"=>"zack"}}), resp.body
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_PUT_retrieves_the_response_headers
|
153
|
+
assert_match(/text\/plain/, put('echo').headers['content-type'])
|
154
|
+
end
|
155
|
+
|
156
|
+
def test_PATCH_send_url_encoded_params
|
157
|
+
assert_equal %(patch {"name"=>"zack"}), patch('echo', :name => 'zack').body
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_OPTIONS
|
161
|
+
resp = run_request(:options, 'echo', nil, {})
|
162
|
+
assert_equal 'options', resp.body
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_HEAD_retrieves_no_response_body
|
166
|
+
assert_equal '', head('echo').body
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_HEAD_retrieves_the_response_headers
|
170
|
+
assert_match(/text\/plain/, head('echo').headers['content-type'])
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_DELETE_retrieves_the_response_headers
|
174
|
+
assert_match(/text\/plain/, delete('echo').headers['content-type'])
|
175
|
+
end
|
176
|
+
|
177
|
+
def test_DELETE_retrieves_the_body
|
178
|
+
assert_equal %(delete), delete('echo').body
|
179
|
+
end
|
180
|
+
|
181
|
+
def test_timeout
|
182
|
+
conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
|
183
|
+
assert_raises Faraday::TimeoutError do
|
184
|
+
conn.get '/slow'
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_connection_error
|
189
|
+
assert_raises Faraday::ConnectionFailed do
|
190
|
+
get 'http://localhost:4'
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_proxy
|
195
|
+
proxy_uri = URI(ENV['LIVE_PROXY'])
|
196
|
+
conn = create_connection(:proxy => proxy_uri)
|
197
|
+
|
198
|
+
res = conn.get '/echo'
|
199
|
+
assert_equal 'get', res.body
|
200
|
+
|
201
|
+
unless self.class.ssl_mode?
|
202
|
+
# proxy can't append "Via" header for HTTPS responses
|
203
|
+
assert_match(/:#{proxy_uri.port}$/, res['via'])
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_proxy_auth_fail
|
208
|
+
proxy_uri = URI(ENV['LIVE_PROXY'])
|
209
|
+
proxy_uri.password = 'WRONG'
|
210
|
+
conn = create_connection(:proxy => proxy_uri)
|
211
|
+
|
212
|
+
err = assert_raises Faraday::ConnectionFailed do
|
213
|
+
conn.get '/echo'
|
214
|
+
end
|
215
|
+
|
216
|
+
unless self.class.ssl_mode? && (self.class.jruby? ||
|
217
|
+
adapter == :em_http || adapter == :em_synchrony)
|
218
|
+
# JRuby raises "End of file reached" which cannot be distinguished from a 407
|
219
|
+
# EM raises "connection closed by server" due to https://github.com/igrigorik/em-socksify/pull/19
|
220
|
+
assert_equal %{407 "Proxy Authentication Required "}, err.message
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
def test_empty_body_response_represented_as_blank_string
|
225
|
+
response = get('204')
|
226
|
+
assert_equal '', response.body
|
227
|
+
end
|
228
|
+
|
229
|
+
def adapter
|
230
|
+
raise NotImplementedError.new("Need to override #adapter")
|
231
|
+
end
|
232
|
+
|
233
|
+
# extra options to pass when building the adapter
|
234
|
+
def adapter_options
|
235
|
+
[]
|
236
|
+
end
|
237
|
+
|
238
|
+
def create_connection(options = {}, &optional_connection_config_blk)
|
239
|
+
if adapter == :default
|
240
|
+
builder_block = nil
|
241
|
+
else
|
242
|
+
builder_block = Proc.new do |b|
|
243
|
+
b.request :multipart
|
244
|
+
b.request :url_encoded
|
245
|
+
b.adapter adapter, *adapter_options, &optional_connection_config_blk
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
server = self.class.live_server
|
250
|
+
url = '%s://%s:%d' % [server.scheme, server.host, server.port]
|
251
|
+
|
252
|
+
options[:ssl] ||= {}
|
253
|
+
options[:ssl][:ca_file] ||= ENV['SSL_FILE']
|
254
|
+
|
255
|
+
Faraday::Connection.new(url, options, &builder_block).tap do |conn|
|
256
|
+
conn.headers['X-Faraday-Adapter'] = adapter.to_s
|
257
|
+
adapter_handler = conn.builder.handlers.last
|
258
|
+
conn.builder.insert_before adapter_handler, Faraday::Response::RaiseError
|
259
|
+
end
|
260
|
+
end
|
261
|
+
end
|
262
|
+
end
|
263
|
+
end
|
@@ -0,0 +1,136 @@
|
|
1
|
+
require File.expand_path('../../helper', __FILE__)
|
2
|
+
require 'stringio'
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module Adapters
|
6
|
+
class LoggerTest < Faraday::TestCase
|
7
|
+
def conn(logger, logger_options={})
|
8
|
+
rubbles = ['Barney', 'Betty', 'Bam Bam']
|
9
|
+
|
10
|
+
Faraday.new do |b|
|
11
|
+
b.response :logger, @logger, logger_options do | logger |
|
12
|
+
logger.filter(/(soylent green is) (.+)/,'\1 tasty')
|
13
|
+
logger.filter(/(api_key:).*"(.+)."/,'\1[API_KEY]')
|
14
|
+
logger.filter(/(password)=(.+)/,'\1=[HIDDEN]')
|
15
|
+
end
|
16
|
+
b.adapter :test do |stubs|
|
17
|
+
stubs.get('/hello') { [200, {'Content-Type' => 'text/html'}, 'hello'] }
|
18
|
+
stubs.post('/ohai') { [200, {'Content-Type' => 'text/html'}, 'fred'] }
|
19
|
+
stubs.post('/ohyes') { [200, {'Content-Type' => 'text/html'}, 'pebbles'] }
|
20
|
+
stubs.get('/rubbles') { [200, {'Content-Type' => 'application/json'}, rubbles] }
|
21
|
+
stubs.get('/filtered_body') { [200, {'Content-Type' => 'text/html'}, 'soylent green is people'] }
|
22
|
+
stubs.get('/filtered_headers') { [200, {'Content-Type' => 'text/html'}, 'headers response'] }
|
23
|
+
stubs.get('/filtered_params') { [200, {'Content-Type' => 'text/html'}, 'params response'] }
|
24
|
+
stubs.get('/filtered_url') { [200, {'Content-Type' => 'text/html'}, 'url response'] }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def setup
|
30
|
+
@io = StringIO.new
|
31
|
+
@logger = Logger.new(@io)
|
32
|
+
@logger.level = Logger::DEBUG
|
33
|
+
|
34
|
+
@conn = conn(@logger)
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_still_returns_output
|
38
|
+
resp = @conn.get '/hello', nil, :accept => 'text/html'
|
39
|
+
assert_equal 'hello', resp.body
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_logs_method_and_url
|
43
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
44
|
+
assert_match 'request: GET http:/hello', @io.string
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_logs_status_code
|
48
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
49
|
+
assert_match 'response: Status 200', @io.string
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_logs_request_headers_by_default
|
53
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
54
|
+
assert_match %(Accept: "text/html), @io.string
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_logs_response_headers_by_default
|
58
|
+
@conn.get '/hello', nil, :accept => 'text/html'
|
59
|
+
assert_match %(Content-Type: "text/html), @io.string
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_does_not_log_request_headers_if_option_is_false
|
63
|
+
app = conn(@logger, :headers => { :request => false })
|
64
|
+
app.get '/hello', nil, :accept => 'text/html'
|
65
|
+
refute_match %(Accept: "text/html), @io.string
|
66
|
+
end
|
67
|
+
|
68
|
+
def test_does_log_response_headers_if_option_is_false
|
69
|
+
app = conn(@logger, :headers => { :response => false })
|
70
|
+
app.get '/hello', nil, :accept => 'text/html'
|
71
|
+
refute_match %(Content-Type: "text/html), @io.string
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_does_not_log_request_body_by_default
|
75
|
+
@conn.post '/ohai', 'name=Unagi', :accept => 'text/html'
|
76
|
+
refute_match %(name=Unagi), @io.string
|
77
|
+
end
|
78
|
+
|
79
|
+
def test_does_not_log_response_body_by_default
|
80
|
+
@conn.post '/ohai', 'name=Toro', :accept => 'text/html'
|
81
|
+
refute_match %(fred), @io.string
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_log_only_request_body
|
85
|
+
app = conn(@logger, :bodies => { :request => true })
|
86
|
+
app.post '/ohyes', 'name=Tamago', :accept => 'text/html'
|
87
|
+
assert_match %(name=Tamago), @io.string
|
88
|
+
refute_match %(pebbles), @io.string
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_log_only_response_body
|
92
|
+
app = conn(@logger, :bodies => { :response => true })
|
93
|
+
app.post '/ohyes', 'name=Hamachi', :accept => 'text/html'
|
94
|
+
assert_match %(pebbles), @io.string
|
95
|
+
refute_match %(name=Hamachi), @io.string
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_log_request_and_response_body
|
99
|
+
app = conn(@logger, :bodies => true)
|
100
|
+
app.post '/ohyes', 'name=Ebi', :accept => 'text/html'
|
101
|
+
assert_match %(name=Ebi), @io.string
|
102
|
+
assert_match %(pebbles), @io.string
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_log_response_body_object
|
106
|
+
app = conn(@logger, :bodies => true)
|
107
|
+
app.get '/rubbles', nil, :accept => 'text/html'
|
108
|
+
assert_match %([\"Barney\", \"Betty\", \"Bam Bam\"]\n), @io.string
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_logs_filter_body
|
112
|
+
app = conn(@logger, :bodies => true)
|
113
|
+
app.get '/filtered_body', nil, :accept => 'text/html'
|
114
|
+
assert_match %(soylent green is), @io.string
|
115
|
+
assert_match %(tasty), @io.string
|
116
|
+
refute_match %(people), @io.string
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_logs_filter_headers
|
120
|
+
app = conn(@logger)
|
121
|
+
app.headers = {'api_key' => 'ABC123'}
|
122
|
+
app.get '/filtered_headers', nil, :accept => 'text/html'
|
123
|
+
assert_match %(api_key:), @io.string
|
124
|
+
assert_match %([API_KEY]), @io.string
|
125
|
+
refute_match %(ABC123), @io.string
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_logs_filter_url
|
129
|
+
app = conn(@logger)
|
130
|
+
app.get '/filtered_url?password=hunter2', nil, :accept => 'text/html'
|
131
|
+
assert_match %(password=[HIDDEN]), @io.string
|
132
|
+
refute_match %(hunter2), @io.string
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
require File.expand_path('../integration', __FILE__)
|
2
|
+
|
3
|
+
module Adapters
|
4
|
+
class NetHttpPersistentTest < Faraday::TestCase
|
5
|
+
|
6
|
+
def adapter() :net_http_persistent end
|
7
|
+
|
8
|
+
Integration.apply(self, :NonParallel) do
|
9
|
+
def setup
|
10
|
+
if defined?(Net::HTTP::Persistent)
|
11
|
+
# work around problems with mixed SSL certificates
|
12
|
+
# https://github.com/drbrain/net-http-persistent/issues/45
|
13
|
+
if Net::HTTP::Persistent.instance_method(:initialize).parameters.first == [:key, :name]
|
14
|
+
Net::HTTP::Persistent.new(name: 'Faraday').reconnect_ssl
|
15
|
+
else
|
16
|
+
Net::HTTP::Persistent.new('Faraday').ssl_cleanup(4)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end if ssl_mode?
|
20
|
+
|
21
|
+
def test_reuses_tcp_sockets
|
22
|
+
# Ensure that requests are not reused from previous tests
|
23
|
+
Thread.current.keys
|
24
|
+
.select { |key| key.to_s =~ /\Anet_http_persistent_Faraday_/ }
|
25
|
+
.each { |key| Thread.current[key] = nil }
|
26
|
+
|
27
|
+
sockets = []
|
28
|
+
tcp_socket_open_wrapped = Proc.new do |*args, &block|
|
29
|
+
socket = TCPSocket.__minitest_stub__open(*args, &block)
|
30
|
+
sockets << socket
|
31
|
+
socket
|
32
|
+
end
|
33
|
+
|
34
|
+
TCPSocket.stub :open, tcp_socket_open_wrapped do
|
35
|
+
conn = create_connection
|
36
|
+
conn.post("/echo", :foo => "bar")
|
37
|
+
conn.post("/echo", :foo => "baz")
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal 1, sockets.count
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_does_not_reuse_tcp_sockets_when_proxy_changes
|
44
|
+
# Ensure that requests are not reused from previous tests
|
45
|
+
Thread.current.keys
|
46
|
+
.select { |key| key.to_s =~ /\Anet_http_persistent_Faraday_/ }
|
47
|
+
.each { |key| Thread.current[key] = nil }
|
48
|
+
|
49
|
+
sockets = []
|
50
|
+
tcp_socket_open_wrapped = Proc.new do |*args, &block|
|
51
|
+
socket = TCPSocket.__minitest_stub__open(*args, &block)
|
52
|
+
sockets << socket
|
53
|
+
socket
|
54
|
+
end
|
55
|
+
|
56
|
+
TCPSocket.stub :open, tcp_socket_open_wrapped do
|
57
|
+
conn = create_connection
|
58
|
+
conn.post("/echo", :foo => "bar")
|
59
|
+
conn.proxy = URI(ENV["LIVE_PROXY"])
|
60
|
+
conn.post("/echo", :foo => "bar")
|
61
|
+
end
|
62
|
+
|
63
|
+
assert_equal 2, sockets.count
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_without_custom_connection_config
|
68
|
+
url = URI('https://example.com:1234')
|
69
|
+
|
70
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new
|
71
|
+
|
72
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
73
|
+
|
74
|
+
# `pool` is only present in net_http_persistent >= 3.0
|
75
|
+
assert http.pool.size != nil if http.respond_to?(:pool)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_custom_connection_config
|
79
|
+
url = URI('https://example.com:1234')
|
80
|
+
|
81
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new(nil, {pool_size: 5})
|
82
|
+
|
83
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
84
|
+
|
85
|
+
# `pool` is only present in net_http_persistent >= 3.0
|
86
|
+
assert_equal 5, http.pool.size if http.respond_to?(:pool)
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_custom_adapter_config
|
90
|
+
url = URI('https://example.com:1234')
|
91
|
+
|
92
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new do |http|
|
93
|
+
http.idle_timeout = 123
|
94
|
+
end
|
95
|
+
|
96
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
97
|
+
adapter.send(:configure_request, http, {})
|
98
|
+
|
99
|
+
assert_equal 123, http.idle_timeout
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_max_retries
|
103
|
+
url = URI('http://example.com')
|
104
|
+
|
105
|
+
adapter = Faraday::Adapter::NetHttpPersistent.new
|
106
|
+
|
107
|
+
http = adapter.send(:net_http_connection, :url => url, :request => {})
|
108
|
+
adapter.send(:configure_request, http, {})
|
109
|
+
|
110
|
+
# `max_retries=` is only present in Ruby 2.5
|
111
|
+
assert_equal 0, http.max_retries if http.respond_to?(:max_retries=)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|