2019-07-25 01:21:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-13 01:33:36 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 05:08:32 -04:00
|
|
|
RSpec.describe BitbucketServer::Connection do
|
2018-07-13 01:33:36 -04:00
|
|
|
let(:options) { { base_uri: 'https://test:7990', user: 'bitbucket', password: 'mypassword' } }
|
|
|
|
let(:payload) { { 'test' => 1 } }
|
|
|
|
let(:headers) { { "Content-Type" => "application/json" } }
|
2018-07-13 02:17:05 -04:00
|
|
|
let(:url) { 'https://test:7990/rest/api/1.0/test?something=1' }
|
2018-07-13 01:33:36 -04:00
|
|
|
|
|
|
|
subject { described_class.new(options) }
|
|
|
|
|
|
|
|
describe '#get' do
|
|
|
|
it 'returns JSON body' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:get, url).with(headers: { 'Accept' => 'application/json' }).to_return(body: payload.to_json, status: 200, headers: headers)
|
2018-07-13 01:33:36 -04:00
|
|
|
|
|
|
|
expect(subject.get(url, { something: 1 })).to eq(payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws an exception if the response is not 200' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:get, url).with(headers: { 'Accept' => 'application/json' }).to_return(body: payload.to_json, status: 500, headers: headers)
|
2018-07-13 01:33:36 -04:00
|
|
|
|
|
|
|
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2018-07-27 17:29:05 -04:00
|
|
|
|
|
|
|
it 'throws an exception if the response is not JSON' do
|
|
|
|
WebMock.stub_request(:get, url).with(headers: { 'Accept' => 'application/json' }).to_return(body: 'bad data', status: 200, headers: headers)
|
|
|
|
|
|
|
|
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2019-01-12 18:18:22 -05:00
|
|
|
|
|
|
|
it 'throws an exception upon a network error' do
|
|
|
|
WebMock.stub_request(:get, url).with(headers: { 'Accept' => 'application/json' }).to_raise(OpenSSL::SSL::SSLError)
|
|
|
|
|
|
|
|
expect { subject.get(url) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2018-07-13 01:33:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#post' do
|
2018-07-27 15:58:03 -04:00
|
|
|
let(:headers) { { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } }
|
|
|
|
|
2018-07-13 02:17:05 -04:00
|
|
|
it 'returns JSON body' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:post, url).with(headers: headers).to_return(body: payload.to_json, status: 200, headers: headers)
|
2018-07-13 02:17:05 -04:00
|
|
|
|
|
|
|
expect(subject.post(url, payload)).to eq(payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws an exception if the response is not 200' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:post, url).with(headers: headers).to_return(body: payload.to_json, status: 500, headers: headers)
|
2018-07-13 02:17:05 -04:00
|
|
|
|
|
|
|
expect { subject.post(url, payload) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2019-01-12 18:18:22 -05:00
|
|
|
|
|
|
|
it 'throws an exception upon a network error' do
|
|
|
|
WebMock.stub_request(:post, url).with(headers: { 'Accept' => 'application/json' }).to_raise(OpenSSL::SSL::SSLError)
|
|
|
|
|
|
|
|
expect { subject.post(url, payload) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2018-07-13 01:33:36 -04:00
|
|
|
end
|
2018-07-13 18:39:24 -04:00
|
|
|
|
|
|
|
describe '#delete' do
|
2018-07-27 15:58:03 -04:00
|
|
|
let(:headers) { { 'Accept' => 'application/json', 'Content-Type' => 'application/json' } }
|
|
|
|
|
2018-07-13 18:39:24 -04:00
|
|
|
context 'branch API' do
|
|
|
|
let(:branch_path) { '/projects/foo/repos/bar/branches' }
|
2018-07-19 02:15:24 -04:00
|
|
|
let(:branch_url) { 'https://test:7990/rest/branch-utils/1.0/projects/foo/repos/bar/branches' }
|
2022-08-01 23:09:33 -04:00
|
|
|
let(:path) {}
|
2018-07-13 18:39:24 -04:00
|
|
|
|
|
|
|
it 'returns JSON body' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:delete, branch_url).with(headers: headers).to_return(body: payload.to_json, status: 200, headers: headers)
|
2018-07-13 18:39:24 -04:00
|
|
|
|
|
|
|
expect(subject.delete(:branches, branch_path, payload)).to eq(payload)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'throws an exception if the response is not 200' do
|
2018-07-27 15:58:03 -04:00
|
|
|
WebMock.stub_request(:delete, branch_url).with(headers: headers).to_return(body: payload.to_json, status: 500, headers: headers)
|
2018-07-13 18:39:24 -04:00
|
|
|
|
|
|
|
expect { subject.delete(:branches, branch_path, payload) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2019-01-12 18:18:22 -05:00
|
|
|
|
|
|
|
it 'throws an exception upon a network error' do
|
|
|
|
WebMock.stub_request(:delete, branch_url).with(headers: headers).to_raise(OpenSSL::SSL::SSLError)
|
|
|
|
|
|
|
|
expect { subject.delete(:branches, branch_path, payload) }.to raise_error(described_class::ConnectionError)
|
|
|
|
end
|
2018-07-13 18:39:24 -04:00
|
|
|
end
|
|
|
|
end
|
2018-07-13 01:33:36 -04:00
|
|
|
end
|