Commit 15220291 authored by Stan Hu's avatar Stan Hu

Add spec for BitbucketServer::Connection

parent f85712fb
...@@ -21,7 +21,7 @@ class Import::BitbucketServerController < Import::BaseController ...@@ -21,7 +21,7 @@ class Import::BitbucketServerController < Import::BaseController
Net::OpenTimeout, Net::OpenTimeout,
Net::ReadTimeout, Net::ReadTimeout,
Gitlab::HTTP::BlockedUrlError, Gitlab::HTTP::BlockedUrlError,
BitbucketServer::Error::Unauthorized].freeze BitbucketServer::Connection::ConnectionError].freeze
def new def new
end end
......
...@@ -18,7 +18,6 @@ module BitbucketServer ...@@ -18,7 +18,6 @@ module BitbucketServer
def repo(project, repo_name) def repo(project, repo_name)
parsed_response = connection.get("/projects/#{project}/repos/#{repo_name}") parsed_response = connection.get("/projects/#{project}/repos/#{repo_name}")
# XXXX TODO Handle failure
BitbucketServer::Representation::Repo.new(parsed_response) BitbucketServer::Representation::Repo.new(parsed_response)
end end
......
...@@ -6,6 +6,8 @@ module BitbucketServer ...@@ -6,6 +6,8 @@ module BitbucketServer
attr_reader :api_version, :base_uri, :username, :token attr_reader :api_version, :base_uri, :username, :token
ConnectionError = Class.new(StandardError)
def initialize(options = {}) def initialize(options = {})
@api_version = options.fetch(:api_version, DEFAULT_API_VERSION) @api_version = options.fetch(:api_version, DEFAULT_API_VERSION)
@base_uri = options[:base_uri] @base_uri = options[:base_uri]
...@@ -19,6 +21,7 @@ module BitbucketServer ...@@ -19,6 +21,7 @@ module BitbucketServer
query: extra_query) query: extra_query)
check_errors!(response) check_errors!(response)
response.parsed_response response.parsed_response
end end
...@@ -29,6 +32,7 @@ module BitbucketServer ...@@ -29,6 +32,7 @@ module BitbucketServer
body: body) body: body)
check_errors!(response) check_errors!(response)
response response
end end
...@@ -37,13 +41,13 @@ module BitbucketServer ...@@ -37,13 +41,13 @@ module BitbucketServer
def check_errors!(response) def check_errors!(response)
if response.code != 200 if response.code != 200
error = error =
if response.parsed_response if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message')) sanitize(response.parsed_response.dig('errors', 0, 'message'))
end end
message = "Error #{response.code}" message = "Error #{response.code}"
message += ": #{error}" if error message += ": #{error}" if error
raise ::BitbucketServer::Error::Unauthorized, message raise ConnectionError, message
end end
end end
......
module BitbucketServer
module Error
Unauthorized = Class.new(StandardError)
end
end
require 'spec_helper'
describe BitbucketServer::Connection do
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } }
let(:payload) { { 'test' => 1 } }
let(:headers) { { "Content-Type" => "application/json" } }
subject { described_class.new(options) }
describe '#get' do
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
it 'returns JSON body' do
WebMock.stub_request(:get, url).to_return(body: payload.to_json, status: 200, headers: headers)
expect(subject.get(url, { something: 1 })).to eq(payload)
end
it 'throws an exception if the response is not 200' do
WebMock.stub_request(:get, url).to_return(body: payload.to_json, status: 500, headers: headers)
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
end
end
describe '#post' do
end
end
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