|
1 | 1 | require 'spec_helper' |
| 2 | +require 'support/shared_examples/existing_headers' |
| 3 | +require 'support/shared_examples/first_page' |
| 4 | +require 'support/shared_examples/middle_page' |
| 5 | +require 'support/shared_examples/last_page' |
2 | 6 |
|
3 | 7 | describe NumbersController, :type => :controller do |
| 8 | + before { request.host = 'example.org' } |
4 | 9 | describe 'GET #index' do |
| 10 | + let(:links) { response.headers['Link'].split(', ') } |
| 11 | + |
5 | 12 | context 'without enough items to give more than one page' do |
6 | 13 | it 'should not paginate' do |
7 | | - get :index, count: 20 |
| 14 | + get :index, :count => 20 |
8 | 15 | expect(response.headers.keys).not_to include('Link') |
9 | 16 | end |
10 | 17 | end |
11 | 18 |
|
12 | 19 | context 'with existing Link headers' do |
13 | | - before do |
14 | | - get :index, count: 30, with_headers: true |
15 | | - |
16 | | - @links = response.headers['Link'].split(', ') |
17 | | - end |
18 | | - |
19 | | - it 'should keep existing Links' do |
20 | | - expect(@links).to include('<http://test.host/numbers?count=30>; rel="without"') |
21 | | - end |
| 20 | + before { get :index, :count => 30, :with_headers => true } |
22 | 21 |
|
23 | | - it 'should contain pagination Links' do |
24 | | - expect(@links).to include('<http://test.host/numbers?count=30&page=2>; rel="next"') |
25 | | - expect(@links).to include('<http://test.host/numbers?count=30&page=2>; rel="last"') |
26 | | - end |
| 22 | + it_behaves_like 'an endpoint with existing Link headers' |
27 | 23 | end |
28 | 24 |
|
29 | 25 | context 'with enough items to paginate' do |
30 | 26 | context 'when on the first page' do |
31 | | - before do |
32 | | - get :index, count: 100 |
33 | | - |
34 | | - @links = response.headers['Link'].split(', ') |
35 | | - end |
36 | | - |
37 | | - it 'should not give a link with rel "first"' do |
38 | | - expect(@links).not_to include('rel="first"') |
39 | | - end |
| 27 | + before { get :index, :count => 100 } |
40 | 28 |
|
41 | | - it 'should not give a link with rel "prev"' do |
42 | | - expect(@links).not_to include('rel="prev"') |
43 | | - end |
44 | | - |
45 | | - it 'should give a link with rel "last"' do |
46 | | - expect(@links).to include('<http://test.host/numbers?count=100&page=4>; rel="last"') |
47 | | - end |
48 | | - |
49 | | - it 'should give a link with rel "next"' do |
50 | | - expect(@links).to include('<http://test.host/numbers?count=100&page=2>; rel="next"') |
51 | | - end |
| 29 | + it_behaves_like 'an endpoint with a first page' |
52 | 30 | end |
53 | 31 |
|
54 | 32 | context 'when on the last page' do |
55 | | - before do |
56 | | - get :index, count: 100, page: 4 |
57 | | - |
58 | | - @links = response.headers['Link'].split(', ') |
59 | | - end |
60 | | - |
61 | | - it 'should not give a link with rel "last"' do |
62 | | - expect(@links).not_to include('rel="last"') |
63 | | - end |
| 33 | + before { get :index, :count => 100, :page => 4 } |
64 | 34 |
|
65 | | - it 'should not give a link with rel "next"' do |
66 | | - expect(@links).not_to include('rel="next"') |
67 | | - end |
68 | | - |
69 | | - it 'should give a link with rel "first"' do |
70 | | - expect(@links).to include('<http://test.host/numbers?count=100&page=1>; rel="first"') |
71 | | - end |
72 | | - |
73 | | - it 'should give a link with rel "prev"' do |
74 | | - expect(@links).to include('<http://test.host/numbers?count=100&page=3>; rel="prev"') |
75 | | - end |
| 35 | + it_behaves_like 'an endpoint with a last page' |
76 | 36 | end |
77 | 37 |
|
78 | 38 | context 'when somewhere comfortably in the middle' do |
79 | | - it 'should give all pagination links' do |
80 | | - get :index, count: 100, page: 2 |
81 | | - |
82 | | - links = response.headers['Link'].split(', ') |
| 39 | + before { get :index, :count => 100, :page => 2 } |
83 | 40 |
|
84 | | - expect(links).to include('<http://test.host/numbers?count=100&page=1>; rel="first"') |
85 | | - expect(links).to include('<http://test.host/numbers?count=100&page=4>; rel="last"') |
86 | | - expect(links).to include('<http://test.host/numbers?count=100&page=3>; rel="next"') |
87 | | - expect(links).to include('<http://test.host/numbers?count=100&page=1>; rel="prev"') |
88 | | - end |
| 41 | + it_behaves_like 'an endpoint with a middle page' |
89 | 42 | end |
90 | 43 | end |
91 | 44 | end |
|
0 commit comments