From 3dc9f0e3b30b02b34c67db1ff9152b5ee08b232d Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 13 Sep 2021 14:42:21 +0100 Subject: [PATCH 1/4] add broken test --- spec/faraday/connection_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 4ff56335e..2d7821235 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -103,6 +103,12 @@ it { expect(subject.params).to eq('a' => 3, 'b' => '2') } end + context 'with basic_auth in url' do + let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' } + + it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') } + end + context 'with custom headers' do let(:options) { { headers: { user_agent: 'Faraday' } } } From 5a66c5def09e784a91b34b07564c92647dd34a70 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 13 Sep 2021 14:45:02 +0100 Subject: [PATCH 2/4] fix rubocop --- spec/faraday/connection_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 2d7821235..3b88035f4 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -105,7 +105,7 @@ context 'with basic_auth in url' do let(:url) { 'http://Aladdin:open%20sesame@sushi.com/fish' } - + it { expect(subject.headers['Authorization']).to eq('Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==') } end From fc230dcd1a6c4914c128ecb69b10ca090db56897 Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 13 Sep 2021 14:51:19 +0100 Subject: [PATCH 3/4] fix broken test --- lib/faraday/connection.rb | 7 ++++++- lib/faraday/request/authorization.rb | 10 +--------- lib/faraday/utils.rb | 7 +++++++ 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 8d8973eb1..d97479628 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -362,13 +362,18 @@ def url_prefix=(url, encoder = nil) uri.query = nil with_uri_credentials(uri) do |user, password| - basic_auth user, password + set_basic_auth(user, password) uri.user = uri.password = nil end @proxy = proxy_from_env(url) unless @manual_proxy end + def set_basic_auth(user, password) + header = Faraday::Utils.basic_header_from(user, password) + headers[Faraday::Request::Authorization::KEY] = header + end + # Sets the path prefix and ensures that it always has a leading # slash. # diff --git a/lib/faraday/request/authorization.rb b/lib/faraday/request/authorization.rb index 330b4eacc..92f064bad 100644 --- a/lib/faraday/request/authorization.rb +++ b/lib/faraday/request/authorization.rb @@ -1,7 +1,5 @@ # frozen_string_literal: true -require 'base64' - module Faraday class Request # Request middleware for the Authorization HTTP header @@ -34,7 +32,7 @@ def on_request(env) # @return [String] a header value def header_from(type, *params) if type.to_s.casecmp('basic').zero? && params.size == 2 - basic_header_from(*params) + Utils.basic_header_from(*params) elsif params.size != 1 raise ArgumentError, "Unexpected params received (got #{params.size} instead of 1)" else @@ -43,12 +41,6 @@ def header_from(type, *params) "#{type} #{value}" end end - - def basic_header_from(login, pass) - value = Base64.encode64("#{login}:#{pass}") - value.delete!("\n") - "Basic #{value}" - end end end end diff --git a/lib/faraday/utils.rb b/lib/faraday/utils.rb index db37d66d9..a2be8b3be 100644 --- a/lib/faraday/utils.rb +++ b/lib/faraday/utils.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +require 'base64' require 'uri' require 'faraday/utils/headers' require 'faraday/utils/params_hash' @@ -52,6 +53,12 @@ def default_params_encoder @default_params_encoder ||= NestedParamsEncoder end + def basic_header_from(login, pass) + value = Base64.encode64("#{login}:#{pass}") + value.delete!("\n") + "Basic #{value}" + end + class << self attr_writer :default_params_encoder end From 47029930e238604ee3649bbccbf2cca6f0b9e2bf Mon Sep 17 00:00:00 2001 From: Matt Date: Mon, 13 Sep 2021 14:55:28 +0100 Subject: [PATCH 4/4] fix rubocop --- .rubocop_todo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6f6c227de..e83aacf5d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -32,7 +32,7 @@ Metrics/AbcSize: # Offense count: 3 # Configuration parameters: CountComments, CountAsOne. Metrics/ClassLength: - Max: 226 + Max: 230 # Offense count: 12 # Configuration parameters: IgnoredMethods.