blib/lib/MYDan/Util/Check/Http.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 88 CC 11 SCAR 37.9
CC
11
Cov
0%
check
72.0
new
20.0
BEGIN
2.0
CRAP 44.1
1 package MYDan::Util::Check::Http;
2 use strict;
3 use warnings;
4 use LWP::UserAgent;
5  
6 $|++;
7  
8 sub new
9 {
10 my ( $class, $method, $url, $type, @condition ) = @_;
11  
12 die "not supported.\n" unless $method && ( $method eq 'get' || $method eq 'post' );
13 die "not supported.\n" unless $type && ( $type eq 'code' || $type eq 'grep' );
14  
15 die "format error.\n" unless @condition;
16  
17 bless +{ method => $method, url => $url, type => $type, condition => \@condition },
18 ref $class || $class;
19 }
20  
21 sub check
22 {
23 my ( $this, %run )= @_;
24  
25 my ( $method, $url, $type, $condition ) = @$this{qw( method url type condition )};
26  
27 my $ua = LWP::UserAgent->new();
28 $ua->agent('Mozilla/9 [en] (Centos; Linux)');
29 $ua->timeout( 10 );
30  
31 my $res = $method eq 'get' ? $ua->get( $url ) : $ua->post( $url );
32  
33 if( $type eq 'code' )
34 {
35 my $code = $res->code();
36 print "$method $url code:$code\n";
37 return ( grep{ $code eq $_ }@$condition ) ? 0 : 1;
38 }
39 else
40 {
41 unless( $res->is_success )
42 {
43 print "$method $url fail\n";
44 return 1;
45 }
46 my $content = $res->content;
47 $content = '' unless defined $content;
48 print "$method $url: $content\n" if $run{debug};
49  
50 return ( grep{ $content =~ /$_/ }@$condition ) ? 0 : 1;
51 }
52  
53 return 1;
54 }
55  
56 1;
57 __END__