View | Details | Raw Unified | Return to bug 19196
Collapse All | Expand All

(-)a/t/Koha/REST/Plugin/Pagination.t (-1 / +91 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
# Dummy app for testing the plugin
21
use Mojolicious::Lite;
22
23
app->log->level('error');
24
25
plugin 'Koha::REST::Plugin::Pagination';
26
27
28
get '/empty' => sub {
29
    my $c = shift;
30
    $c->render( json => { ok => 1 }, status => 200 );
31
};
32
33
get '/pagination_headers' => sub {
34
    my $c = shift;
35
    $c->add_pagination_headers({ total => 10, page => 2, per_page => 3 });
36
    $c->render( json => { ok => 1 }, status => 200 );
37
};
38
39
get '/pagination_headers_first_page' => sub {
40
    my $c = shift;
41
    $c->add_pagination_headers({ total => 10, page => 1, per_page => 3 });
42
    $c->render( json => { ok => 1 }, status => 200 );
43
};
44
45
get '/pagination_headers_last_page' => sub {
46
    my $c = shift;
47
    $c->add_pagination_headers({ total => 10, page => 4, per_page => 3 });
48
    $c->render( json => { ok => 1 }, status => 200 );
49
};
50
51
# The tests
52
53
use Test::More tests => 1;
54
use Test::Mojo;
55
56
subtest 'add_pagination_headers() tests' => sub {
57
58
    plan tests => 25;
59
60
    my $t = Test::Mojo->new;
61
62
    $t->get_ok('/empty')
63
      ->status_is( 200 )
64
      ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' )
65
      ->header_is( 'Link'          => undef, 'Link is undefined' );
66
67
    $t->get_ok('/pagination_headers')
68
      ->status_is( 200 )
69
      ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
70
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="prev",/ )
71
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=3>; rel="next",/ )
72
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
73
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
74
75
    $t->get_ok('/pagination_headers_first_page')
76
      ->status_is( 200 )
77
      ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
78
      ->header_unlike( 'Link' => qr/<http:\/\/.*\?per_page=3&page=.*>; rel="prev",/ )
79
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=2>; rel="next",/ )
80
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
81
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
82
83
    $t->get_ok('/pagination_headers_last_page')
84
      ->status_is( 200 )
85
      ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' )
86
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=3>; rel="prev",/ )
87
      ->header_unlike( 'Link' => qr/<http:\/\/.*\?per_page=3&page=.*>; rel="next",/ )
88
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
89
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
90
};
91

Return to bug 19196