2019-10-12 21:52:04 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe API::Helpers::RelatedResourcesHelpers do
|
2018-03-27 19:54:05 +05:30
|
|
|
subject(:helpers) do
|
|
|
|
Class.new.include(described_class).new
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
describe '#expose_path' do
|
|
|
|
let(:path) { '/api/v4/awesome_endpoint' }
|
|
|
|
|
|
|
|
context 'empty relative URL root' do
|
|
|
|
before do
|
|
|
|
stub_config_setting(relative_url_root: '')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the existing path' do
|
|
|
|
expect(helpers.expose_path(path)).to eq(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'slash relative URL root' do
|
|
|
|
before do
|
|
|
|
stub_config_setting(relative_url_root: '/')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the existing path' do
|
|
|
|
expect(helpers.expose_path(path)).to eq(path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with relative URL root' do
|
|
|
|
before do
|
|
|
|
stub_config_setting(relative_url_root: '/gitlab/root')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns the existing path' do
|
|
|
|
expect(helpers.expose_path(path)).to eq("/gitlab/root" + path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
describe '#expose_url' do
|
|
|
|
let(:path) { '/api/v4/awesome_endpoint' }
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
subject(:url) { helpers.expose_url(path) }
|
|
|
|
|
2018-11-08 19:23:39 +05:30
|
|
|
def stub_default_url_options(protocol: 'http', host: 'example.com', port: nil, script_name: '')
|
2018-03-27 19:54:05 +05:30
|
|
|
expect(Gitlab::Application.routes).to receive(:default_url_options)
|
2018-11-08 19:23:39 +05:30
|
|
|
.and_return(protocol: protocol, host: host, port: port, script_name: script_name)
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
it 'accepts the host if it contains an underscore' do
|
|
|
|
stub_default_url_options(host: 'w_ww.example.com')
|
|
|
|
|
|
|
|
is_expected.to start_with('http://w_ww.example.com/')
|
|
|
|
end
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
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
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
it 'includes the relative_url before the path if it is set' do
|
|
|
|
stub_default_url_options(script_name: '/gitlab')
|
|
|
|
|
|
|
|
is_expected.to start_with('http://example.com/gitlab/api/v4')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes the path after the host' do
|
|
|
|
stub_default_url_options
|
|
|
|
|
|
|
|
is_expected.to start_with('http://example.com/api/v4')
|
|
|
|
end
|
2018-03-27 19:54:05 +05:30
|
|
|
end
|
|
|
|
end
|