From d4218f7b3433be71e82f5670bed55e19cae7024e Mon Sep 17 00:00:00 2001 From: ykrods <890082+ykrods@users.noreply.github.com> Date: Tue, 18 Jun 2024 13:24:36 +0900 Subject: [PATCH] Merge relative url without escaping (#1567) --- lib/faraday/connection.rb | 3 ++- spec/faraday/connection_spec.rb | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/faraday/connection.rb b/lib/faraday/connection.rb index 6316fc937..e979e8282 100644 --- a/lib/faraday/connection.rb +++ b/lib/faraday/connection.rb @@ -473,7 +473,8 @@ def build_exclusive_url(url = nil, params = nil, params_encoder = nil) if url && !base.path.end_with?('/') base.path = "#{base.path}/" # ensure trailing slash end - url = url.to_s.gsub(':', '%3A') if URI.parse(url.to_s).opaque + # Ensure relative url will be parsed correctly (such as `service:search` ) + url = "./#{url}" if url.respond_to?(:start_with?) && !url.start_with?('http://', 'https://', '/', './', '../') uri = url ? base + url : base if params uri.query = params.to_query(params_encoder || options.params_encoder) diff --git a/spec/faraday/connection_spec.rb b/spec/faraday/connection_spec.rb index d4ccb23d8..7fa726a4c 100644 --- a/spec/faraday/connection_spec.rb +++ b/spec/faraday/connection_spec.rb @@ -300,14 +300,14 @@ def decode(params) 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%3Asearch?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%3Asearch?limit=400') + expect(uri.to_s).to eq('http://service.com/api/service:search?limit=400') end end