Commit d2fd6d23 authored by Stan Hu's avatar Stan Hu

Deal with subpaths and trailing slashes properly

parent 77044043
...@@ -5,6 +5,7 @@ module BitbucketServer ...@@ -5,6 +5,7 @@ module BitbucketServer
include ActionView::Helpers::SanitizeHelper include ActionView::Helpers::SanitizeHelper
DEFAULT_API_VERSION = '1.0' DEFAULT_API_VERSION = '1.0'
SEPARATOR = '/'
attr_reader :api_version, :base_uri, :username, :token attr_reader :api_version, :base_uri, :username, :token
...@@ -87,19 +88,35 @@ module BitbucketServer ...@@ -87,19 +88,35 @@ module BitbucketServer
def build_url(path) def build_url(path)
return path if path.starts_with?(root_url) return path if path.starts_with?(root_url)
URI.join(root_url, path).to_s url_join_paths(root_url, path)
end end
def root_url def root_url
URI.join(base_uri, "/rest/api/#{api_version}").to_s url_join_paths(base_uri, "/rest/api/#{api_version}")
end end
def delete_url(resource, path) def delete_url(resource, path)
if resource == :branches if resource == :branches
URI.join(base_uri, "/rest/branch-utils/#{api_version}#{path}").to_s url_join_paths(base_uri, "/rest/branch-utils/#{api_version}#{path}")
else else
build_url(path) build_url(path)
end end
end end
# URI.join is stupid in that slashes are important:
#
# # URI.join('http://example.com/subpath', 'hello')
# => http://example.com/hello
#
# We really want http://example.com/subpath/hello
#
def url_join_paths(*paths)
paths.map { |path| strip_slashes(path) }.join(SEPARATOR)
end
def strip_slashes(path)
path = path[1..-1] if path.starts_with?(SEPARATOR)
path.chomp(SEPARATOR)
end
end end
end end
require 'spec_helper' require 'spec_helper'
describe BitbucketServer::Client do describe BitbucketServer::Client do
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } } let(:base_uri) { 'https://test:7990/stash/' }
let(:options) { { base_uri: base_uri, user: 'bitbucket', password: 'mypassword' } }
let(:project) { 'SOME-PROJECT' } let(:project) { 'SOME-PROJECT' }
let(:repo_slug) { 'my-repo' } let(:repo_slug) { 'my-repo' }
let(:headers) { { "Content-Type" => "application/json" } } let(:headers) { { "Content-Type" => "application/json" } }
...@@ -36,7 +37,7 @@ describe BitbucketServer::Client do ...@@ -36,7 +37,7 @@ describe BitbucketServer::Client do
describe '#repo' do describe '#repo' do
let(:path) { "/projects/#{project}/repos/#{repo_slug}" } let(:path) { "/projects/#{project}/repos/#{repo_slug}" }
let(:url) { "https://test:7990/rest/api/1.0/projects/SOME-PROJECT/repos/my-repo" } let(:url) { "#{base_uri}rest/api/1.0/projects/SOME-PROJECT/repos/my-repo" }
it 'requests a specific repository' do it 'requests a specific repository' do
stub_request(:get, url).to_return(status: 200, headers: headers, body: '{}') stub_request(:get, url).to_return(status: 200, headers: headers, body: '{}')
...@@ -60,7 +61,7 @@ describe BitbucketServer::Client do ...@@ -60,7 +61,7 @@ describe BitbucketServer::Client do
describe '#create_branch' do describe '#create_branch' do
let(:branch) { 'test-branch' } let(:branch) { 'test-branch' }
let(:sha) { '12345678' } let(:sha) { '12345678' }
let(:url) { 'https://test:7990/rest/api/1.0/projects/SOME-PROJECT/repos/my-repo/branches' } let(:url) { "#{base_uri}rest/api/1.0/projects/SOME-PROJECT/repos/my-repo/branches" }
it 'requests Bitbucket to create a branch' do it 'requests Bitbucket to create a branch' do
stub_request(:post, url).to_return(status: 204, headers: headers, body: '{}') stub_request(:post, url).to_return(status: 204, headers: headers, body: '{}')
...@@ -74,7 +75,7 @@ describe BitbucketServer::Client do ...@@ -74,7 +75,7 @@ describe BitbucketServer::Client do
describe '#delete_branch' do describe '#delete_branch' do
let(:branch) { 'test-branch' } let(:branch) { 'test-branch' }
let(:sha) { '12345678' } let(:sha) { '12345678' }
let(:url) { 'https://test:7990/rest/branch-utils/1.0/projects/SOME-PROJECT/repos/my-repo/branches' } let(:url) { "#{base_uri}rest/branch-utils/1.0/projects/SOME-PROJECT/repos/my-repo/branches" }
it 'requests Bitbucket to create a branch' do it 'requests Bitbucket to create a branch' do
stub_request(:delete, url).to_return(status: 204, headers: headers, body: '{}') stub_request(:delete, url).to_return(status: 204, headers: headers, body: '{}')
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment