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
Net::OpenTimeout,
Net::ReadTimeout,
Gitlab::HTTP::BlockedUrlError,
BitbucketServer::Error::Unauthorized].freeze
BitbucketServer::Connection::ConnectionError].freeze
def new
end
......
......@@ -18,7 +18,6 @@ module BitbucketServer
def repo(project, repo_name)
parsed_response = connection.get("/projects/#{project}/repos/#{repo_name}")
# XXXX TODO Handle failure
BitbucketServer::Representation::Repo.new(parsed_response)
end
......
......@@ -6,6 +6,8 @@ module BitbucketServer
attr_reader :api_version, :base_uri, :username, :token
ConnectionError = Class.new(StandardError)
def initialize(options = {})
@api_version = options.fetch(:api_version, DEFAULT_API_VERSION)
@base_uri = options[:base_uri]
......@@ -19,6 +21,7 @@ module BitbucketServer
query: extra_query)
check_errors!(response)
response.parsed_response
end
......@@ -29,6 +32,7 @@ module BitbucketServer
body: body)
check_errors!(response)
response
end
......@@ -37,13 +41,13 @@ module BitbucketServer
def check_errors!(response)
if response.code != 200
error =
if response.parsed_response
if response.parsed_response && response.parsed_response.is_a?(Hash)
sanitize(response.parsed_response.dig('errors', 0, 'message'))
end
message = "Error #{response.code}"
message += ": #{error}" if error
raise ::BitbucketServer::Error::Unauthorized, message
raise ConnectionError, message
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