summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/chef/http.rb5
-rw-r--r--spec/unit/http_spec.rb23
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/chef/http.rb b/lib/chef/http.rb
index 42b5decd6b..44b418735f 100644
--- a/lib/chef/http.rb
+++ b/lib/chef/http.rb
@@ -204,7 +204,10 @@ class Chef
elsif path.nil? or path.empty?
URI.parse(@url)
else
- URI.parse("#{@url}/#{path}")
+ # The regular expressions used here are to make sure '@url' does not have
+ # any trailing slashes and 'path' does not have any leading slashes. This
+ # way they are always joined correctly using just one slash.
+ URI.parse(@url.gsub(%r{/+$}, '') + '/' + path.gsub(%r{^/+}, ''))
end
end
diff --git a/spec/unit/http_spec.rb b/spec/unit/http_spec.rb
index d747482260..1cd226b4ee 100644
--- a/spec/unit/http_spec.rb
+++ b/spec/unit/http_spec.rb
@@ -21,8 +21,31 @@ require 'spec_helper'
require 'chef/http'
require 'chef/http/basic_client'
+class Chef::HTTP
+ public :create_url
+end
+
describe Chef::HTTP do
+ describe "create_url" do
+
+ it 'should return a correctly formatted url 1/3 CHEF-5261' do
+ http = Chef::HTTP.new('http://www.getchef.com')
+ http.create_url('api/endpoint').should eql(URI.parse('http://www.getchef.com/api/endpoint'))
+ end
+
+ it 'should return a correctly formatted url 2/3 CHEF-5261' do
+ http = Chef::HTTP.new('http://www.getchef.com/')
+ http.create_url('/organization/org/api/endpoint/').should eql(URI.parse('http://www.getchef.com/organization/org/api/endpoint/'))
+ end
+
+ it 'should return a correctly formatted url 3/3 CHEF-5261' do
+ http = Chef::HTTP.new('http://www.getchef.com/organization/org///')
+ http.create_url('///api/endpoint?url=http://foo.bar').should eql(URI.parse('http://www.getchef.com/organization/org/api/endpoint?url=http://foo.bar'))
+ end
+
+ end # create_url
+
describe "head" do
it 'should return nil for a "200 Success" response (CHEF-4762)' do