Respect the protocol in `expose_url`

When https is configured in the URL, also use that, and do not set it
to http.

Closes gitlab-org/gitlab-ee#5217
This commit is contained in:
Toon Claes 2018-03-11 22:38:29 +01:00
parent 4bc58ca210
commit 3802006436
3 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,5 @@
---
title: Ensure the API returns https links when https is configured
merge_request: 17681
author:
type: fixed

View File

@ -15,7 +15,7 @@ module API
url_options = Gitlab::Application.routes.default_url_options
protocol, host, port = url_options.slice(:protocol, :host, :port).values
URI::HTTP.build(scheme: protocol, host: host, port: port, path: path).to_s
URI::Generic.build(scheme: protocol, host: host, port: port, path: path).to_s
end
private

View File

@ -0,0 +1,41 @@
require 'spec_helper'
describe API::Helpers::RelatedResourcesHelpers do
subject(:helpers) do
Class.new.include(described_class).new
end
describe '#expose_url' do
let(:path) { '/api/v4/awesome_endpoint' }
subject(:url) { helpers.expose_url(path) }
def stub_default_url_options(protocol: 'http', host: 'example.com', port: nil)
expect(Gitlab::Application.routes).to receive(:default_url_options)
.and_return(protocol: protocol, host: host, port: port)
end
it 'respects the protocol if it is HTTP' do
stub_default_url_options(protocol: 'http')
is_expected.to start_with('http://')
end
it 'respects the protocol if it is HTTPS' do
stub_default_url_options(protocol: 'https')
is_expected.to start_with('https://')
end
it 'accepts port to be nil' do
stub_default_url_options(port: nil)
is_expected.to start_with('http://example.com/')
end
it 'includes port if provided' do
stub_default_url_options(port: 8080)
is_expected.to start_with('http://example.com:8080/')
end
end
end