From ae627fc16a261a45208e0566efb39d04a9dbae6b Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Tue, 12 Jan 2021 23:00:48 +0300 Subject: [PATCH 1/3] escape colon in path segment --- lib/faraday/connection.rb | 2 ++ spec/faraday/connection_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 3776253ee..f167fb402 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -522,7 +522,9 @@ def build_exclusive_url(url = nil, params = nil, params_encoder = nil) base = base.dup base.path = "#{base.path}/" # ensure trailing slash end + url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url uri = url ? base + url : base + uri = URI.parse(CGI.unescape(uri.to_s)) if params uri.query = params.to_query(params_encoder || options.params_encoder) end diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index 4f51f5682..ae28191c8 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -270,6 +270,29 @@ expect(uri.to_s).to eq('http://sushi.com/sake/') end end + + context 'with colon in path' do + let(:url) { 'http://service.com' } + + it 'joins url to base when used absolute path' do + conn = Faraday.new(url: url) + uri = conn.build_exclusive_url('/service:search?limit=400') + expect(uri.to_s).to eq('http://service.com/service:search?limit=400') + end + + it 'joins url to base when used relative path' do + conn = Faraday.new(url: url) + uri = conn.build_exclusive_url('service:search?limit=400') + expect(uri.to_s).to eq('http://service.com/service:search?limit=400') + end + + it 'joins url to base when used with path prefix' do + conn = Faraday.new(url: url) + conn.path_prefix = '/api' + uri = conn.build_exclusive_url('service:search?limit=400') + expect(uri.to_s).to eq('http://service.com/api/service:search?limit=400') + end + end end describe '#build_url' do From d29c56371a04c71dce54cca3e3d694011c0bf39d Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Wed, 13 Jan 2021 14:13:46 +0300 Subject: [PATCH 2/3] fix rubocop --- .rubocop_todo.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6bf427c24..5acc7c7cd 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -13,7 +13,7 @@ Metrics/AbcSize: # Offense count: 4 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 234 + Max: 236 # Offense count: 17 Metrics/CyclomaticComplexity: From abc7309aea4ec4f8a41d5f5bdc400bd7affb1434 Mon Sep 17 00:00:00 2001 From: Yaroslav Date: Wed, 13 Jan 2021 14:22:32 +0300 Subject: [PATCH 3/3] leave uri escaped --- .rubocop_todo.yml | 2 +- lib/faraday/connection.rb | 1 - spec/faraday/connection_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 5acc7c7cd..a6c69a6b9 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -13,7 +13,7 @@ Metrics/AbcSize: # Offense count: 4 # Configuration parameters: CountComments. Metrics/ClassLength: - Max: 236 + Max: 235 # Offense count: 17 Metrics/CyclomaticComplexity: diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index f167fb402..6318de585 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -524,7 +524,6 @@ def build_exclusive_url(url = nil, params = nil, params_encoder = nil) end url = url && URI.parse(url.to_s).opaque ? url.to_s.gsub(':', '%3A') : url uri = url ? base + url : base - uri = URI.parse(CGI.unescape(uri.to_s)) if params uri.query = params.to_query(params_encoder || options.params_encoder) end diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index ae28191c8..5c7e3b60c 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -283,14 +283,14 @@ it 'joins url to base when used relative path' do conn = Faraday.new(url: url) uri = conn.build_exclusive_url('service:search?limit=400') - expect(uri.to_s).to eq('http://service.com/service:search?limit=400') + expect(uri.to_s).to eq('http://service.com/service%3Asearch?limit=400') end it 'joins url to base when used with path prefix' do conn = Faraday.new(url: url) conn.path_prefix = '/api' uri = conn.build_exclusive_url('service:search?limit=400') - expect(uri.to_s).to eq('http://service.com/api/service:search?limit=400') + expect(uri.to_s).to eq('http://service.com/api/service%3Asearch?limit=400') end end end