Welcome to mirror list, hosted at ThFree Co, Russian Federation.

link_header_parser_spec.rb « utils « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e15ef930271917680c895b171520acd147c8b9ec (plain)
1
2
3
4
5
6
7
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
42
43
44
45
46
47
48
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
74
75
# frozen_string_literal: true

require 'fast_spec_helper'

RSpec.describe Gitlab::Utils::LinkHeaderParser do
  let(:parser) { described_class.new(header) }

  describe '#parse' do
    subject { parser.parse }

    context 'with a valid header' do
      let(:header) { generate_header(next: 'http://sandbox.org/next') }
      let(:expected) { { next: { uri: URI('http://sandbox.org/next') } } }

      it { is_expected.to eq(expected) }

      context 'with multiple links' do
        let(:header) { generate_header(next: 'http://sandbox.org/next', previous: 'http://sandbox.org/previous') }
        let(:expected) do
          {
            next: { uri: URI('http://sandbox.org/next') },
            previous: { uri: URI('http://sandbox.org/previous') }
          }
        end

        it { is_expected.to eq(expected) }
      end

      context 'with an incomplete uri' do
        let(:header) { '<http://sandbox.org/next; rel="next"' }

        it { is_expected.to eq({}) }
      end

      context 'with no rel' do
        let(:header) { '<http://sandbox.org/next>; direction="next"' }

        it { is_expected.to eq({}) }
      end

      context 'with multiple rel elements' do
        # check https://datatracker.ietf.org/doc/html/rfc5988#section-5.3:
        # occurrences after the first MUST be ignored by parsers
        let(:header) { '<http://sandbox.org/next>; rel="next"; rel="dummy"' }

        it { is_expected.to eq(expected) }
      end

      context 'when the url is too long' do
        let(:header) { "<http://sandbox.org/#{'a' * 500}>; rel=\"next\"" }

        it { is_expected.to eq({}) }
      end
    end

    context 'with nil header' do
      let(:header) { nil }

      it { is_expected.to eq({}) }
    end

    context 'with empty header' do
      let(:header) { '' }

      it { is_expected.to eq({}) }
    end

    def generate_header(links)
      stringified_links = links.map do |rel, url|
        "<#{url}>; rel=\"#{rel}\""
      end
      stringified_links.join(', ')
    end
  end
end