line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::HTTPStatus; |
2
|
3
|
|
|
3
|
|
209509
|
use strict; |
|
3
|
|
|
|
|
20
|
|
|
3
|
|
|
|
|
130
|
|
3
|
|
|
|
|
|
|
|
4
|
3
|
|
|
3
|
|
21
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
106
|
|
5
|
3
|
|
|
3
|
|
18
|
no warnings; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
185
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=encoding utf8 |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
Test::HTTPStatus - check an HTTP status |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 SYNOPSIS |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
use Test::HTTPStatus tests => 2; |
16
|
|
|
|
|
|
|
use Apache::Constants qw(:http); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
http_ok( 'https://www.perl.org', HTTP_OK ); |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
http_ok( $url, $status ); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 DESCRIPTION |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
Check the HTTP status for a resource. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=cut |
27
|
|
|
|
|
|
|
|
28
|
3
|
|
|
3
|
|
48
|
use v5.10.1; # Mojolicious is v5.10.1 and later |
|
3
|
|
|
|
|
25
|
|
29
|
|
|
|
|
|
|
our $VERSION = '2.003'; |
30
|
|
|
|
|
|
|
|
31
|
3
|
|
|
3
|
|
1873
|
use parent 'Test::Builder::Module'; |
|
3
|
|
|
|
|
1187
|
|
|
3
|
|
|
|
|
21
|
|
32
|
|
|
|
|
|
|
|
33
|
3
|
|
|
3
|
|
229
|
use Carp qw(carp); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
156
|
|
34
|
3
|
|
|
3
|
|
1575
|
use HTTP::SimpleLinkChecker; |
|
3
|
|
|
|
|
1577775
|
|
|
3
|
|
|
|
|
224
|
|
35
|
3
|
|
|
3
|
|
40
|
use Test::Builder::Module; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
37
|
|
36
|
3
|
|
|
3
|
|
140
|
use Mojo::URL; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
26
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
my $Test = __PACKAGE__->builder; |
39
|
|
|
|
|
|
|
|
40
|
3
|
|
|
3
|
|
217
|
use constant NO_URL => -1; |
|
3
|
|
|
|
|
11
|
|
|
3
|
|
|
|
|
264
|
|
41
|
3
|
|
|
3
|
|
28
|
use constant INVALID_URL => -2; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
193
|
|
42
|
3
|
|
|
3
|
|
28
|
use constant HTTP_OK => 200; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
226
|
|
43
|
3
|
|
|
3
|
|
27
|
use constant HTTP_NOT_FOUND => 404; |
|
3
|
|
|
|
|
34
|
|
|
3
|
|
|
|
|
347
|
|
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub import { |
46
|
3
|
|
|
3
|
|
31
|
my $self = shift; |
47
|
3
|
|
|
|
|
11
|
my $caller = caller; |
48
|
3
|
|
|
3
|
|
30
|
no strict 'refs'; |
|
3
|
|
|
|
|
19
|
|
|
3
|
|
|
|
|
1787
|
|
49
|
3
|
|
|
|
|
12
|
*{$caller.'::http_ok'} = \&http_ok; |
|
3
|
|
|
|
|
31
|
|
50
|
3
|
|
|
|
|
14
|
*{$caller.'::NO_URL'} = \&NO_URL; |
|
3
|
|
|
|
|
32
|
|
51
|
3
|
|
|
|
|
11
|
*{$caller.'::INVALID_URL'} = \&INVALID_URL; |
|
3
|
|
|
|
|
19
|
|
52
|
3
|
|
|
|
|
10
|
*{$caller.'::HTTP_OK'} = \&HTTP_OK; |
|
3
|
|
|
|
|
25
|
|
53
|
3
|
|
|
|
|
10
|
*{$caller.'::HTTP_NOT_FOUND'} = \&HTTP_NOT_FOUND; |
|
3
|
|
|
|
|
21
|
|
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
26
|
$Test->exported_to($caller); |
56
|
3
|
|
|
|
|
57
|
$Test->plan(@_); |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
=head1 FUNCTIONS |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
=over 4 |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item http_ok( URL [, HTTP_STATUS] ) |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
Print the ok message if the URL's HTTP status matches the specified |
66
|
|
|
|
|
|
|
HTTP_STATUS. If you don't specify a status, it assumes you mean |
67
|
|
|
|
|
|
|
HTTP_OK (from Apache::Constants). |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=cut |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
sub http_ok { |
72
|
1
|
|
|
1
|
1
|
129
|
my $url = shift; |
73
|
1
|
|
50
|
|
|
6
|
my $expected = shift || HTTP_OK; |
74
|
|
|
|
|
|
|
|
75
|
1
|
|
|
|
|
5
|
my $hash = _get_status( $url ); |
76
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
6
|
my $status = $hash->{status}; |
78
|
|
|
|
|
|
|
|
79
|
1
|
50
|
33
|
|
|
13
|
if( defined $expected and $expected eq $status ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
80
|
1
|
|
|
|
|
16
|
$Test->ok( 1, "Expected [$expected], got [$status] for [$url]"); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
elsif( $status == NO_URL ) { |
83
|
0
|
|
|
|
|
0
|
$Test->ok( 0, "[$url] does not appear to be anything"); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
elsif( $status == INVALID_URL ) { |
86
|
0
|
|
|
|
|
0
|
$Test->ok( 0, "[$url] does not appear to be a valid URL"); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
else { |
89
|
0
|
|
|
|
|
0
|
$Test->ok( 0, "Mysterious failure for [$url] with status [$status]" ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub _get_status { |
94
|
5
|
|
|
5
|
|
61354
|
my $string = shift; |
95
|
|
|
|
|
|
|
|
96
|
5
|
100
|
|
|
|
30
|
return { status => NO_URL } unless defined $string; |
97
|
|
|
|
|
|
|
|
98
|
4
|
|
|
|
|
47
|
my $url = Mojo::URL->new( $string ); |
99
|
4
|
100
|
|
|
|
896
|
return { status => undef } unless $url->host; |
100
|
|
|
|
|
|
|
|
101
|
3
|
|
|
|
|
42
|
my $status = HTTP::SimpleLinkChecker::check_link( $url ); |
102
|
|
|
|
|
|
|
|
103
|
3
|
|
|
|
|
1356638
|
return { url => $url, status => $status }; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=back |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SEE ALSO |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Apache::Constants, HTTP::SimpleLinkChecker |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
=head1 SOURCE AVAILABILITY |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
This project is in GitHub: |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
https://github.com/briandfoy/test-httpstatus |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=head1 AUTHOR |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
brian d foy, C<< >> |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Copyright © 2002-2018, brian d foy . All rights reserved. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify |
127
|
|
|
|
|
|
|
it under the terms of the Artistic License 2.0. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=cut |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
1; |