File Coverage

blib/lib/Test/Mojo/Role/HTTPstatus.pm
Criterion Covered Total %
statement 28 28 100.0
branch n/a
condition 2 3 66.6
subroutine 15 15 100.0
pod 9 9 100.0
total 54 55 98.1


line stmt bran cond sub pod time code
1             package Test::Mojo::Role::HTTPstatus;
2              
3 1     1   1346 use strict;
  1         3  
  1         31  
4 1     1   5 use warnings;
  1         2  
  1         23  
5 1     1   27 use 5.016; # inherited from Mojolicious
  1         7  
6              
7 1     1   5 use Mojo::Util qw(encode);
  1         3  
  1         54  
8              
9 1     1   6 use Role::Tiny;
  1         2  
  1         8  
10              
11             # Yuck, semver. I give in, the stupid cult that doesn't understand
12             # what the *number* bit of *version number* means has won.
13             our $VERSION='1.0.0';
14              
15             sub status_like {
16 10     10 1 3557 my ($self, $status, $desc) = @_;
17 10         31 $desc = _desc($desc, $self->tx->res->code . " like $status");
18 10         114 return $self->test('like', $self->tx->res->code, $status, $desc);
19             }
20              
21             sub status_unlike {
22 2     2 1 3645 my ($self, $status, $desc) = @_;
23 2         10 $desc = _desc($desc, $self->tx->res->code . " unlike $status");
24 2         25 return $self->test('unlike', $self->tx->res->code, $status, $desc);
25             }
26              
27             # copied from Test::Mojo. If no $desc is provided to status_(un)like
28             # then use the default that those methods define
29 12   66 12   167 sub _desc { encode 'UTF-8', shift || shift }
30              
31             sub status_is_client_error {
32 1     1 1 2736 $_[0]->status_like(qr/^4\d\d$/a, $_[0]->tx->res->code.' is client error');
33             }
34              
35             sub status_is_empty {
36 1     1 1 2734 $_[0]->status_like(qr/^(1\d\d|[23]04)$/a, $_[0]->tx->res->code.' is empty');
37             }
38              
39             sub status_is_error {
40 1     1 1 2711 $_[0]->status_like(qr/^[45]\d\d$/a, $_[0]->tx->res->code.' is error');
41             }
42              
43             sub status_is_info {
44 1     1 1 2661 $_[0]->status_like(qr/^1\d\d$/a, $_[0]->tx->res->code.' is info');
45             }
46              
47             sub status_is_redirect {
48 1     1 1 2734 $_[0]->status_like(qr/^3\d\d$/a, $_[0]->tx->res->code.' is redirect');
49             }
50              
51             sub status_is_server_error {
52 1     1 1 2665 $_[0]->status_like(qr/^5\d\d$/a, $_[0]->tx->res->code.' is server error');
53             }
54              
55             sub status_is_success {
56 2     2 1 37843 $_[0]->status_like(qr/^2\d\d$/a, $_[0]->tx->res->code.' is success');
57             }
58              
59             =encoding utf8
60              
61             =head1 NAME
62              
63             Test::Mojo::Role::HTTPstatus
64              
65             =head1 DESCRIPTION
66              
67             Role to add some extra methods to Test::Mojo for testing routes' HTTP status codes
68              
69             =head1 SYNOPSIS
70              
71             use Test::More;
72             use Test::Mojo::WithRoles qw(HTTPstatus);
73              
74             my $t = Test::Mojo::WithRoles->new('MyApp');
75              
76             $t->get_ok('/success')->status_like(qr/^2/);
77             $t->get_ok('/failure')->status_is_error();
78              
79             done_testing();
80              
81             =head1 METHODS
82              
83             C does most of the work behind the scenes. For completeness, its
84             opposite C is also provided.
85              
86             =over
87              
88             =item status_like
89              
90             $t = $t->status_like(qr/^4/);
91             $t = $t->status_like(qr/^4/, 'some kind of error');
92              
93             Pass if the status matches a given regex.
94              
95             =item status_unlike
96              
97             $t = $t->status_unlike(qr/^3/);
98             $t = $t->status_unlike(qr/^3/, 'not any kind of redirect');
99              
100             Pass if the status does B match the given regex.
101              
102             =back
103              
104             The various C methods match the C methods in
105             L.
106              
107             =over
108              
109             =item status_is_client_error
110              
111             Matches a C<4xx> status code.
112              
113             =item status_is_empty
114              
115             Matches a C<1xx>, C<204>, or C<304> status code.
116              
117             =item status_is_error
118              
119             Matches a C<4xx> or C<5xx> status code.
120              
121             =item status_is_info
122              
123             Matches a C<1xx> status code.
124              
125             =item status_is_redirect
126              
127             Matches a C<3xx> status code.
128              
129             =item status_is_server_error
130              
131             Matches a C<5xx> status code.
132              
133             =item status_is_success
134              
135             Matches a C<2xx> status code.
136              
137             =back
138              
139             =head1 BUGS
140              
141             None known, plenty lurking no doubt. Please report any bugs that you find on
142             Github:
143              
144             L
145              
146             or even better, raise a pull request that fixes it. The best pull requests
147             include at least two commits, the first being a test that fails, and then in the
148             following commits a code change that makes that test pass, and documentation. I
149             realise that it is difficult to work in that manner, but C will
150             let you edit history to pretend that's what you did.
151              
152             =head1 SEE ALSO
153              
154             L
155              
156             =head1 AUTHOR, COPYRIGHT and LICENCE
157              
158             Copyright 2020 David Cantrell Edavid@cantrell.org.ukE
159              
160             This software is free-as-in-speech software, and may be used,
161             distributed, and modified under the terms of either the GNU
162             General Public Licence version 2 or the Artistic Licence. It's
163             up to you which one you use. The full text of the licences can
164             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
165              
166             =head1 CONSPIRACY
167              
168             This module is also free-as-in-mason software.
169              
170             =cut
171              
172             1;