diff options
author | Stan Hu <stanhu@gmail.com> | 2018-07-13 08:33:36 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2018-07-13 08:33:36 +0300 |
commit | 15220291ae1752b9e36ad0b2320180614ec37c59 (patch) | |
tree | 4365ad78af4a81b524781719379acd9284c61114 | |
parent | f85712fb9814170aac631c7c24ca287fe5a0eb04 (diff) |
Add spec for BitbucketServer::Connection
-rw-r--r-- | app/controllers/import/bitbucket_server_controller.rb | 2 | ||||
-rw-r--r-- | lib/bitbucket_server/client.rb | 1 | ||||
-rw-r--r-- | lib/bitbucket_server/connection.rb | 8 | ||||
-rw-r--r-- | lib/bitbucket_server/error/unauthorized.rb | 5 | ||||
-rw-r--r-- | spec/lib/bitbucket_server/connection_spec.rb | 28 |
5 files changed, 35 insertions, 9 deletions
diff --git a/app/controllers/import/bitbucket_server_controller.rb b/app/controllers/import/bitbucket_server_controller.rb index 3e5e45bd527..83949c8e840 100644 --- a/app/controllers/import/bitbucket_server_controller.rb +++ b/app/controllers/import/bitbucket_server_controller.rb @@ -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 diff --git a/lib/bitbucket_server/client.rb b/lib/bitbucket_server/client.rb index 49654e45226..802fe7953ce 100644 --- a/lib/bitbucket_server/client.rb +++ b/lib/bitbucket_server/client.rb @@ -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 diff --git a/lib/bitbucket_server/connection.rb b/lib/bitbucket_server/connection.rb index c5e40a3964c..0e7817fc82e 100644 --- a/lib/bitbucket_server/connection.rb +++ b/lib/bitbucket_server/connection.rb @@ -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 diff --git a/lib/bitbucket_server/error/unauthorized.rb b/lib/bitbucket_server/error/unauthorized.rb deleted file mode 100644 index 96ed469e913..00000000000 --- a/lib/bitbucket_server/error/unauthorized.rb +++ /dev/null @@ -1,5 +0,0 @@ -module BitbucketServer - module Error - Unauthorized = Class.new(StandardError) - end -end diff --git a/spec/lib/bitbucket_server/connection_spec.rb b/spec/lib/bitbucket_server/connection_spec.rb new file mode 100644 index 00000000000..911f6eea8bf --- /dev/null +++ b/spec/lib/bitbucket_server/connection_spec.rb @@ -0,0 +1,28 @@ +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 |