Skip to content

Commit

Permalink
Add proxy setting when url_prefix is changed (#1276)
Browse files Browse the repository at this point in the history
* Add proxy setting when url_prefix is changed

Without this setting, setting the url_prefix after
a Faraday::Connection is initialized results in no_proxy
settings ignored when using relative paths.

* Increase Rubocop Metrics/ClassLength
  • Loading branch information
ci committed Apr 28, 2021
1 parent 67c5f7b commit 53e4bae
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop_todo.yml
Expand Up @@ -14,7 +14,7 @@ Metrics/AbcSize:
# Offense count: 5
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 235
Max: 250

# Offense count: 15
# Configuration parameters: IgnoredMethods.
Expand Down
8 changes: 7 additions & 1 deletion lib/faraday/connection.rb
Expand Up @@ -419,6 +419,8 @@ def url_prefix=(url, encoder = nil)
basic_auth user, password
uri.user = uri.password = nil
end

@proxy = proxy_from_env(url) unless @manual_proxy
end

# Sets the path prefix and ensures that it always has a leading
Expand Down Expand Up @@ -577,7 +579,11 @@ def proxy_from_env(url)
case url
when String
uri = Utils.URI(url)
uri = URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
uri = if uri.host.nil?
find_default_proxy
else
URI.parse("#{uri.scheme}://#{uri.host}").find_proxy
end
when URI
uri = url.find_proxy
when nil
Expand Down
8 changes: 8 additions & 0 deletions spec/faraday/connection_spec.rb
Expand Up @@ -442,6 +442,14 @@
end
end

it 'allows when url in no proxy list with url_prefix' do
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
conn = Faraday::Connection.new
conn.url_prefix = 'http://example.com'
expect(conn.proxy).to be_nil
end
end

it 'allows when prefixed url is not in no proxy list' do
with_env 'http_proxy' => 'http://proxy.com', 'no_proxy' => 'example.com' do
conn = Faraday::Connection.new('http://prefixedexample.com')
Expand Down

0 comments on commit 53e4bae

Please sign in to comment.