summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorToon Claes <toon@iotcl.com>2017-08-17 12:11:40 +0200
committerToon Claes <toon@iotcl.com>2017-08-17 21:47:01 +0200
commita98d17a83877cd885a92aac29a9cb13d13a53a86 (patch)
tree28714cf97d1130e66a38b6447629d9c1b1509378
parentfdf4f0fc0884c0346b16ec107a1ea1084dd4a32f (diff)
downloadgitlab-ce-a98d17a83877cd885a92aac29a9cb13d13a53a86.tar.gz
Add specs for pagination Link header
Add specs that check the 'Link' header for the inclusion of: - rel="first" - rel="last" - rel="prev" - rel="next" Fixes gitlab-org/gitlab-ce#36618 Related to gitlab-com/infrastructure#2532
-rw-r--r--changelogs/unreleased/tc-git-tower-pagination-links.yml5
-rw-r--r--spec/lib/api/helpers/pagination_spec.rb51
2 files changed, 52 insertions, 4 deletions
diff --git a/changelogs/unreleased/tc-git-tower-pagination-links.yml b/changelogs/unreleased/tc-git-tower-pagination-links.yml
new file mode 100644
index 00000000000..b99ef8c3c4c
--- /dev/null
+++ b/changelogs/unreleased/tc-git-tower-pagination-links.yml
@@ -0,0 +1,5 @@
+---
+title: Improve API pagination headers when no record found
+merge_request: 13629
+author: Jordan Patterson
+type: fixed
diff --git a/spec/lib/api/helpers/pagination_spec.rb b/spec/lib/api/helpers/pagination_spec.rb
index fb3ef04b860..041c40d9490 100644
--- a/spec/lib/api/helpers/pagination_spec.rb
+++ b/spec/lib/api/helpers/pagination_spec.rb
@@ -52,7 +52,13 @@ describe API::Helpers::Pagination do
expect_header('X-Page', '1')
expect_header('X-Next-Page', '2')
expect_header('X-Prev-Page', '')
- expect_header('Link', any_args)
+
+ expect_header('Link', anything) do |_key, val|
+ expect(val).to include('rel="first"')
+ expect(val).to include('rel="last"')
+ expect(val).to include('rel="next"')
+ expect(val).not_to include('rel="prev"')
+ end
subject.paginate(resource)
end
@@ -75,15 +81,52 @@ describe API::Helpers::Pagination do
expect_header('X-Page', '2')
expect_header('X-Next-Page', '')
expect_header('X-Prev-Page', '1')
- expect_header('Link', any_args)
+
+ expect_header('Link', anything) do |_key, val|
+ expect(val).to include('rel="first"')
+ expect(val).to include('rel="last"')
+ expect(val).to include('rel="prev"')
+ expect(val).not_to include('rel="next"')
+ end
+
+ subject.paginate(resource)
+ end
+ end
+ end
+
+ context 'when resource empty' do
+ describe 'first page' do
+ before do
+ allow(subject).to receive(:params)
+ .and_return({ page: 1, per_page: 2 })
+ end
+
+ it 'returns appropriate amount of resources' do
+ expect(subject.paginate(resource).count).to eq 0
+ end
+
+ it 'adds appropriate headers' do
+ expect_header('X-Total', '0')
+ expect_header('X-Total-Pages', '0')
+ expect_header('X-Per-Page', '2')
+ expect_header('X-Page', '1')
+ expect_header('X-Next-Page', '')
+ expect_header('X-Prev-Page', '')
+
+ expect_header('Link', anything) do |_key, val|
+ expect(val).to include('rel="first"')
+ expect(val).to include('rel="last"')
+ expect(val).not_to include('rel="prev"')
+ expect(val).not_to include('rel="next"')
+ end
subject.paginate(resource)
end
end
end
- def expect_header(name, value)
- expect(subject).to receive(:header).with(name, value)
+ def expect_header(*args, &block)
+ expect(subject).to receive(:header).with(*args, &block)
end
def expect_message(method)