From d717b3b53483b2e504e4a477ea0041d40687c887 Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Fri, 31 Jul 2020 14:07:23 -0700 Subject: [PATCH] fix: Correctly handle absolute paths for simple commands (#895) --- lib/google/apis/core/base_service.rb | 8 +++++++- spec/google/apis/core/service_spec.rb | 15 +++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/google/apis/core/base_service.rb b/lib/google/apis/core/base_service.rb index 40eb5f930..38496f2c6 100644 --- a/lib/google/apis/core/base_service.rb +++ b/lib/google/apis/core/base_service.rb @@ -334,7 +334,13 @@ module Google # Request-specific options # @return [Google::Apis::Core::DownloadCommand] def make_simple_command(method, path, options) - template = Addressable::Template.new(root_url + base_path + path) + full_path = + if path.start_with? "/" + path[1..-1] + else + base_path + path + end + template = Addressable::Template.new(root_url + full_path) command = ApiCommand.new(method, template) command.options = request_options.merge(options) apply_command_defaults(command) diff --git a/spec/google/apis/core/service_spec.rb b/spec/google/apis/core/service_spec.rb index ba8967ea2..be994e1b0 100644 --- a/spec/google/apis/core/service_spec.rb +++ b/spec/google/apis/core/service_spec.rb @@ -21,6 +21,7 @@ RSpec.describe Google::Apis::Core::BaseService do include TestHelpers let(:service) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', '') } + let(:service_with_base_path) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', 'my_service/v1/') } let(:x_goog_api_client_value) { "gl-ruby/#{RUBY_VERSION} gdcl/#{Google::Apis::VERSION}" } before do @@ -133,6 +134,20 @@ RSpec.describe Google::Apis::Core::BaseService do include_examples 'with options' end + context 'when making simple commands with a base path' do + it 'should return the correct URL for a relative path' do + command = service_with_base_path.send(:make_simple_command, :get, 'zoo/animals', authorization: 'foo') + url = command.url.expand({}).to_s + expect(url).to eql 'https://www.googleapis.com/my_service/v1/zoo/animals' + end + + it 'should return the correct URL for an absolute path' do + command = service_with_base_path.send(:make_simple_command, :get, '/zoo/animals', authorization: 'foo') + url = command.url.expand({}).to_s + expect(url).to eql 'https://www.googleapis.com/zoo/animals' + end + end + context 'when making download commands' do let(:command) { service.send(:make_download_command, :get, 'zoo/animals', authorization: 'foo') }