summaryrefslogtreecommitdiff
path: root/spec/support_specs/helpers/keyset_pagination_helpers_spec.rb
blob: ec63f33776c708ea9ec459e38529afd86bfd7e6b (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
76
77
78
79
80
81
82
83
84
85
86
87
88
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe KeysetPaginationHelpers, feature_category: :api do
  include described_class

  let(:headers) { { 'LINK' => %(<#{url}>; rel="#{rel}") } }
  let(:response) { instance_double('HTTParty::Response', headers: headers) }
  let(:rel) { 'next' }
  let(:url) do
    'http://127.0.0.1:3000/api/v4/projects/7/audit_eve' \
      'nts?cursor=eyJpZCI6IjYyMjAiLCJfa2QiOiJuIn0%3D&id=7&o' \
      'rder_by=id&page=1&pagination=keyset&per_page=2'
  end

  describe '#pagination_links' do
    subject { pagination_links(response) }

    let(:expected_result) { [{ url: url, rel: rel }] }

    it { is_expected.to eq expected_result }

    context 'with a partially malformed LINK header' do
      # malformed as the regxe is expecting the url to be surrounded by `<>`
      let(:headers) do
        { 'LINK' => %(<#{url}>; rel="next", GARBAGE, #{url}; rel="prev") }
      end

      it { is_expected.to eq expected_result }
    end

    context 'with a malformed LINK header' do
      # malformed as the regxe is expecting the url to be surrounded by `<>`
      let(:headers) { { 'LINK' => %(rel="next", GARBAGE, #{url}; rel="prev") } }
      let(:expected_result) { [] }

      it { is_expected.to eq expected_result }
    end
  end

  describe '#pagination_params_from_next_url' do
    subject { pagination_params_from_next_url(response) }

    let(:expected_result) do
      {
        'cursor' => 'eyJpZCI6IjYyMjAiLCJfa2QiOiJuIn0=',
        'id' => '7',
        'order_by' => 'id',
        'page' => '1',
        'pagination' => 'keyset',
        'per_page' => '2'
      }
    end

    it { is_expected.to eq expected_result }

    context 'with both prev and next rel links' do
      let(:prev_url) do
        'http://127.0.0.1:3000/api/v4/projects/7/audit_eve' \
          'nts?cursor=foocursor&id=8&o' \
          'rder_by=id&page=0&pagination=keyset&per_page=2'
      end

      let(:headers) do
        { 'LINK' => %(<#{url}>; rel="next", <#{prev_url}>; rel="prev") }
      end

      it { is_expected.to eq expected_result }
    end

    context 'with a partially malformed LINK header' do
      # malformed as the regxe is expecting the url to be surrounded by `<>`
      let(:headers) do
        { 'LINK' => %(<#{url}>; rel="next", GARBAGE, #{url}; rel="prev") }
      end

      it { is_expected.to eq expected_result }
    end

    context 'with a malformed LINK header' do
      # malformed as the regxe is expecting the url to be surrounded by `<>`
      let(:headers) { { 'LINK' => %(rel="next", GARBAGE, #{url}; rel="prev") } }

      it { is_expected.to be nil }
    end
  end
end