related_resources_helpers_spec.rb 2.25 KB
Newer Older
1 2 3 4 5 6 7
require 'spec_helper'

describe API::Helpers::RelatedResourcesHelpers do
  subject(:helpers) do
    Class.new.include(described_class).new
  end

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
  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

42 43 44 45
  describe '#expose_url' do
    let(:path) { '/api/v4/awesome_endpoint' }
    subject(:url) { helpers.expose_url(path) }

46
    def stub_default_url_options(protocol: 'http', host: 'example.com', port: nil, script_name: '')
47
      expect(Gitlab::Application.routes).to receive(:default_url_options)
48
        .and_return(protocol: protocol, host: host, port: port, script_name: script_name)
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
    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
74 75 76 77 78 79 80 81 82 83 84 85

    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
86 87
  end
end