diff options
author | Toon Claes <toon@gitlab.com> | 2018-03-11 22:38:29 +0100 |
---|---|---|
committer | Toon Claes <toon@gitlab.com> | 2018-03-12 15:34:11 +0100 |
commit | 3802006436d6834ee0a8052b22773cb65c4447b5 (patch) | |
tree | d169e4fcf0112bbe312fd5fd7e0084d08833dd60 | |
parent | 4bc58ca210d8c984c20e88c19e74b53e9505905b (diff) | |
download | gitlab-ce-3802006436d6834ee0a8052b22773cb65c4447b5.tar.gz |
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
-rw-r--r-- | changelogs/unreleased/tc-api-fix-expose_url.yml | 5 | ||||
-rw-r--r-- | lib/api/helpers/related_resources_helpers.rb | 2 | ||||
-rw-r--r-- | spec/lib/api/helpers/related_resources_helpers_spec.rb | 41 |
3 files changed, 47 insertions, 1 deletions
diff --git a/changelogs/unreleased/tc-api-fix-expose_url.yml b/changelogs/unreleased/tc-api-fix-expose_url.yml new file mode 100644 index 00000000000..c701f64d6bf --- /dev/null +++ b/changelogs/unreleased/tc-api-fix-expose_url.yml @@ -0,0 +1,5 @@ +--- +title: Ensure the API returns https links when https is configured +merge_request: 17681 +author: +type: fixed diff --git a/lib/api/helpers/related_resources_helpers.rb b/lib/api/helpers/related_resources_helpers.rb index 1f677529b07..7f4d6e58b34 100644 --- a/lib/api/helpers/related_resources_helpers.rb +++ b/lib/api/helpers/related_resources_helpers.rb @@ -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 diff --git a/spec/lib/api/helpers/related_resources_helpers_spec.rb b/spec/lib/api/helpers/related_resources_helpers_spec.rb new file mode 100644 index 00000000000..b918301f1cb --- /dev/null +++ b/spec/lib/api/helpers/related_resources_helpers_spec.rb @@ -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 |