Commit 0ddb3c76 authored by Ash McKenzie's avatar Ash McKenzie

Support HTTP headers and sending JSON

parent e865251e
...@@ -91,7 +91,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength ...@@ -91,7 +91,7 @@ class GitlabNet # rubocop:disable Metrics/ClassLength
end end
def check def check
get("#{internal_api_endpoint}/check", read_timeout: CHECK_TIMEOUT) get("#{internal_api_endpoint}/check", options: { read_timeout: CHECK_TIMEOUT })
end end
def authorized_key(key) def authorized_key(key)
......
...@@ -33,15 +33,19 @@ module HTTPHelper ...@@ -33,15 +33,19 @@ module HTTPHelper
http http
end end
def http_request_for(method, uri, params = {}) def http_request_for(method, uri, params: {}, headers: {}, options: {})
request_klass = method == :get ? Net::HTTP::Get : Net::HTTP::Post request_klass = method == :get ? Net::HTTP::Get : Net::HTTP::Post
request = request_klass.new(uri.request_uri) request = request_klass.new(uri.request_uri, headers)
user = config.http_settings['user'] user = config.http_settings['user']
password = config.http_settings['password'] password = config.http_settings['password']
request.basic_auth(user, password) if user && password request.basic_auth(user, password) if user && password
if options[:json]
request.body = options[:json].merge(secret_token: secret_token).to_json
else
request.set_form_data(params.merge(secret_token: secret_token)) request.set_form_data(params.merge(secret_token: secret_token))
end
if uri.is_a?(URI::HTTPUNIX) if uri.is_a?(URI::HTTPUNIX)
# The HTTPUNIX HTTP client does not set a correct Host header. This can # The HTTPUNIX HTTP client does not set a correct Host header. This can
...@@ -52,13 +56,15 @@ module HTTPHelper ...@@ -52,13 +56,15 @@ module HTTPHelper
request request
end end
def request(method, url, params = {}, options = {}) def request(method, url, params: {}, headers: {}, options: {})
$logger.debug('Performing request', method: method.to_s.upcase, url: url) $logger.debug('Performing request', method: method.to_s.upcase, url: url)
uri = URI.parse(url) uri = URI.parse(url)
http = http_client_for(uri, options) http = http_client_for(uri, options)
request = http_request_for(method, uri, params) request = http_request_for(method, uri,
params: params,
headers: headers,
options: options)
begin begin
start_time = Time.new start_time = Time.new
...@@ -79,12 +85,12 @@ module HTTPHelper ...@@ -79,12 +85,12 @@ module HTTPHelper
response response
end end
def get(url, options = {}) def get(url, headers: {}, options: {})
request(:get, url, {}, options) request(:get, url, headers: headers, options: options)
end end
def post(url, params) def post(url, params, headers: {}, options: {})
request(:post, url, params) request(:post, url, params: params, headers: headers, options: options)
end end
def cert_store def cert_store
......
...@@ -5,6 +5,7 @@ require_relative '../lib/gitlab_access_status' ...@@ -5,6 +5,7 @@ require_relative '../lib/gitlab_access_status'
describe GitlabNet, vcr: true do describe GitlabNet, vcr: true do
let(:gitlab_net) { GitlabNet.new } let(:gitlab_net) { GitlabNet.new }
let(:changes) { ['0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4'] } let(:changes) { ['0000000000000000000000000000000000000000 92d0970eefd7acb6d548878925ce2208cfe2d2ec refs/heads/branch4'] }
let(:base_api_endpoint) { 'http://localhost:3000/api/v4' }
let(:internal_api_endpoint) { 'http://localhost:3000/api/v4/internal' } let(:internal_api_endpoint) { 'http://localhost:3000/api/v4/internal' }
let(:project) { 'gitlab-org/gitlab-test.git' } let(:project) { 'gitlab-org/gitlab-test.git' }
let(:key) { 'key-1' } let(:key) { 'key-1' }
...@@ -12,7 +13,7 @@ describe GitlabNet, vcr: true do ...@@ -12,7 +13,7 @@ describe GitlabNet, vcr: true do
let(:secret) { "0a3938d9d95d807e94d937af3a4fbbea\n" } let(:secret) { "0a3938d9d95d807e94d937af3a4fbbea\n" }
before do before do
gitlab_net.stub(:internal_api_endpoint).and_return(internal_api_endpoint) gitlab_net.stub(:base_api_endpoint).and_return(base_api_endpoint)
gitlab_net.stub(:secret_token).and_return(secret) gitlab_net.stub(:secret_token).and_return(secret)
end end
...@@ -381,19 +382,49 @@ describe GitlabNet, vcr: true do ...@@ -381,19 +382,49 @@ describe GitlabNet, vcr: true do
describe :http_request_for do describe :http_request_for do
context 'with stub' do context 'with stub' do
let(:get) do let(:get) { double(Net::HTTP::Get) }
double(Net::HTTP::Get).tap do |get|
Net::HTTP::Get.stub(:new) { get }
end
end
let(:user) { 'user' } let(:user) { 'user' }
let(:password) { 'password' } let(:password) { 'password' }
let(:url) { URI 'http://localhost/' } let(:url) { URI 'http://localhost/' }
let(:params) { { 'key1' => 'value1' } }
let(:headers) { { 'Content-Type' => 'application/json'} }
let(:options) { { json: { 'key2' => 'value2' } } }
context 'with no params, options or headers' do
subject { gitlab_net.send :http_request_for, :get, url } subject { gitlab_net.send :http_request_for, :get, url }
before do before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user } gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password } gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
Net::HTTP::Get.should_receive(:new).with('/', {}).and_return(get)
get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:set_form_data).with(hash_including(secret_token: secret)).once
end
it { should_not be_nil }
end
context 'with params' do
subject { gitlab_net.send :http_request_for, :get, url, params: params, headers: headers }
before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
Net::HTTP::Get.should_receive(:new).with('/', headers).and_return(get)
get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:set_form_data).with({ 'key1' => 'value1', secret_token: secret }).once
end
it { should_not be_nil }
end
context 'with headers' do
subject { gitlab_net.send :http_request_for, :get, url, headers: headers }
before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
Net::HTTP::Get.should_receive(:new).with('/', headers).and_return(get)
get.should_receive(:basic_auth).with(user, password).once get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:set_form_data).with(hash_including(secret_token: secret)).once get.should_receive(:set_form_data).with(hash_including(secret_token: secret)).once
end end
...@@ -401,6 +432,24 @@ describe GitlabNet, vcr: true do ...@@ -401,6 +432,24 @@ describe GitlabNet, vcr: true do
it { should_not be_nil } it { should_not be_nil }
end end
context 'with options' do
context 'with json' do
subject { gitlab_net.send :http_request_for, :get, url, options: options }
before do
gitlab_net.send(:config).http_settings.stub(:[]).with('user') { user }
gitlab_net.send(:config).http_settings.stub(:[]).with('password') { password }
Net::HTTP::Get.should_receive(:new).with('/', {}).and_return(get)
get.should_receive(:basic_auth).with(user, password).once
get.should_receive(:body=).with({ 'key2' => 'value2', secret_token: secret }.to_json).once
get.should_not_receive(:set_form_data)
end
it { should_not be_nil }
end
end
end
context 'Unix socket' do context 'Unix socket' do
it 'sets the Host header to "localhost"' do it 'sets the Host header to "localhost"' do
gitlab_net = described_class.new gitlab_net = described_class.new
......
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