File Coverage

blib/lib/WWW/CheckGzip.pm
Criterion Covered Total %
statement 15 71 21.1
branch 0 8 0.0
condition n/a
subroutine 5 11 45.4
pod 2 6 33.3
total 22 96 22.9


line stmt bran cond sub pod time code
1             package WWW::CheckGzip;
2 1     1   555 use warnings;
  1         2  
  1         28  
3 1     1   4 use strict;
  1         1  
  1         26  
4             our $VERSION = '0.05';
5 1     1   4 use Carp;
  1         2  
  1         46  
6 1     1   235 use Gzip::Faster;
  1         997  
  1         53  
7 1     1   416 use HTTP::Tiny;
  1         51020  
  1         720  
8              
9             sub builtin_test
10             {
11 0     0 0   my ($ok, $message) = @_;
12 0 0         if ($ok) {
13 0           print "OK - $message.\n";
14             }
15             else {
16 0           print "Not OK - $message.\n";
17             }
18             }
19              
20             sub new
21             {
22 0     0 1   my ($class, $test) = @_;
23 0           my $o = bless {};
24 0           $o->{ua} = HTTP::Tiny->new (agent => __PACKAGE__);
25 0 0         if (! $test) {
26 0           $test = \& builtin_test;
27             }
28 0           $o->{test} = $test;
29 0           return $o;
30             }
31              
32             sub get_compressed
33             {
34 0     0 0   my ($o, $url) = @_;
35 0           my $ua = $o->{ua};
36 0           my $r = $ua->get ($url, {headers => {'Accept-Encoding' => 'gzip'}});
37 0           return $r;
38             }
39              
40             sub get_uncompressed
41             {
42 0     0 0   my ($o, $url) = @_;
43 0           my $ua = $o->{ua};
44 0           my $r = $ua->get ($url, {headers => {'Accept-Encoding' => ''}});
45 0           return $r;
46             }
47              
48             # Private
49              
50             sub getce
51             {
52 0     0 0   my ($r) = @_;
53 0           return $r->{headers}{'content-encoding'};
54             }
55              
56             sub check
57             {
58 0     0 1   my ($o, $url) = @_;
59              
60 0 0         if (! $url) {
61 0           carp "No URL supplied";
62 0           return;
63             }
64              
65             # Test with compressed.
66              
67 0           my $r = $o->get_compressed ($url);
68 0           my $get_ok = $r->{success};
69 0           & {$o->{test}} (!! $get_ok, "successfully got compressed $url");
  0            
70 0 0         if (! $get_ok) {
71 0           return;
72             }
73              
74 0           my $content_encoding = getce ($r);
75 0           & {$o->{test}} (!! $content_encoding, "got content encoding");
  0            
76 0           & {$o->{test}} ($content_encoding eq 'gzip', "content encoding is gzip");
  0            
77 0           my $text = $r->{content};
78 0           my $unc;
79 0           eval {
80 0           $unc = gunzip ($text);
81             };
82 0           & {$o->{test}} (! $@, "$url correctly gzipped");
  0            
83 0           & {$o->{test}} (length ($unc) > length ($text),
  0            
84             "compression made it smaller");
85              
86             # Test with uncompressed.
87              
88 0           my $runc = $o->get_uncompressed ($url);
89 0           my $get_ok_unc = $runc->{success};
90 0           & {$o->{test}} ($get_ok_unc, "successfully got uncompressed $url");
  0            
91 0           my $content_encoding_unc = getce ($runc);
92 0           & {$o->{test}} (! $content_encoding_unc, "Did not get content encoding");
  0            
93 0           my $unctext = $runc->{content};
94 0           eval {
95 0           gunzip ($unctext);
96             };
97 0           & {$o->{test}} ($@, "$url not gzipped when requesting ungzipped");
  0            
98             }
99              
100             1;