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

(-)a/t/Koha/REST/Plugin/Pagination.t (-1 / +108 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 '/total_count_header' => sub {
34
    my $c = shift;
35
    $c->total_count_header({ total_count => 100 });
36
    $c->render( json => { ok => 1 }, status => 200 );
37
};
38
39
get '/links_header' => sub {
40
    my $c = shift;
41
    $c->links_header({ total => 10, page => 2, per_page => 3 });
42
    $c->render( json => { ok => 1 }, status => 200 );
43
};
44
45
get '/links_header_first_page' => sub {
46
    my $c = shift;
47
    $c->links_header({ total => 10, page => 1, per_page => 3 });
48
    $c->render( json => { ok => 1 }, status => 200 );
49
};
50
51
get '/links_header_last_page' => sub {
52
    my $c = shift;
53
    $c->links_header({ total => 10, page => 4, per_page => 3 });
54
    $c->render( json => { ok => 1 }, status => 200 );
55
};
56
57
# The tests
58
59
use Test::More tests => 2;
60
use Test::Mojo;
61
62
subtest 'total_count_header() tests' => sub {
63
64
    plan tests => 6;
65
66
    my $t = Test::Mojo->new;
67
68
    $t->get_ok('/empty')
69
      ->status_is( 200 )
70
      ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' );
71
72
    $t->get_ok('/total_count_header')
73
      ->status_is( 200 )
74
      ->header_is( 'X-Total-Count' => 100, 'X-Total-Count contains the hardcoded value' );
75
76
};
77
78
subtest 'links_header() tests' => sub {
79
80
    plan tests => 21;
81
82
    my $t = Test::Mojo->new;
83
84
    $t->get_ok('/empty')
85
      ->status_is( 200 )
86
      ->header_is( 'Link' => undef, 'Link is undefined' );
87
88
    $t->get_ok('/links_header')
89
      ->status_is( 200 )
90
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="prev",/ )
91
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=3>; rel="next",/ )
92
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
93
      ->header_like( 'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
94
95
    $t->get_ok('/links_header_first_page')
96
      ->status_is( 200 )
97
      ->header_unlike( 'Link' => qr/<http:\/\/.*\?per_page=3&page=.*>; rel="prev",/ )
98
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=2>; rel="next",/ )
99
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
100
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
101
102
    $t->get_ok('/links_header_last_page')
103
      ->status_is( 200 )
104
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=3>; rel="prev",/ )
105
      ->header_unlike( 'Link' => qr/<http:\/\/.*\?per_page=3&page=.*>; rel="next",/ )
106
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=1>; rel="first",/ )
107
      ->header_like(   'Link' => qr/<http:\/\/.*\?per_page=3&page=4>; rel="last"/ );
108
};

Return to bug 19196