File Coverage

inc/Test/HTTPTinyFile.pm
Criterion Covered Total %
statement 65 100 65.0
branch 11 28 39.2
condition n/a
subroutine 15 22 68.1
pod n/a
total 91 150 60.6


line stmt bran cond sub pod time code
1             package Test::HTTPTinyFile;
2              
3 2     2   21270 use strict;
  2         4  
  2         52  
4 2     2   8 use warnings;
  2         3  
  2         44  
5 2     2   10 use HTTP::Tiny;
  2         3  
  2         46  
6 2     2   824 use HTTP::Date qw( time2str );
  2         5795  
  2         111  
7 2     2   14 use URI;
  2         5  
  2         36  
8 2     2   8 use Test::More ();
  2         4  
  2         62  
9              
10             my $request_method = \&HTTP::Tiny::request;
11              
12 2     2   298 BEGIN { *note = \&Test::More::note }
13              
14             my $request_wrapper = sub
15             {
16             # TODO options to support 'If-Modified-Since' see PAUSE::Packages
17 12     12   11824 my($self, $method, $url, $args) = @_;
18 12         162 my $uri = URI->new($url);
19            
20 12         1523 note "HTTP::Tiny $method $url";
21            
22 12 50       12459 if($uri->scheme eq 'file')
23             {
24 12         6934 tie my %headers, 'Test::HTTPTinyFile::ResponseHeaderTie';
25 12         135 my $path = $uri->path;
26 12 50       380 $path =~ s{^/([A-Za-z]:)}{$1} if $^O eq 'MSWin32';
27 12         114 my $result = { url => $url, content => '', headers => \%headers }; # TODO include some headers
28 12         40 my $content = '';
29            
30 12 50       158 if($method =~ /(GET|HEAD)/)
    0          
    0          
    0          
31             {
32 12 50       705 if(-d $path)
    50          
    0          
33             {
34 0         0 die "TODO";
35             }
36             elsif(-r $path)
37             {
38 12 50       72 if($method eq 'GET')
39             {
40 12         28 eval {
41 2     2   13 use autodie;
  2         28  
  2         14  
42 12         128 open my $fh, '<', $path;
43 12         6341 binmode $fh;
44 12         2982 local $/;
45 12 50       411 $content = <$fh> if $method eq 'GET';
46 12         83 close $fh;
47             };
48 12 50       3096 if($@)
49             {
50 0         0 $result->{success} = 0;
51 0         0 $result->{status} = 599;
52 0         0 $result->{reason} = 'Internal Exception';
53             }
54             }
55 12 50       70 unless(defined $result->{success})
56             {
57 12         62 $result->{success} = 1;
58 12         120 $result->{status} = 200;
59 12         146 $result->{reason} = 'OK';
60 12         449 $headers{'last-modified'} = time2str((stat $path)[9]);
61             }
62             }
63             elsif(-e $path)
64             {
65 0         0 $result->{success} = 0;
66 0         0 $result->{status} = 403;
67 0         0 $result->{reason} = 'Forbidden';
68             }
69             else
70             {
71 0         0 $result->{success} = 0;
72 0         0 $result->{status} = 404;
73 0         0 $result->{reason} = 'Not Found';
74             }
75             }
76             elsif($method eq 'POST')
77             {
78 0         0 die "TODO";
79             }
80             elsif($method eq 'PUT')
81             {
82 0         0 die "TODO";
83             }
84             elsif($method eq 'DELETE')
85             {
86 0         0 die "TODO";
87             }
88             else
89             {
90 0         0 die "unknown HTTP method: $method";
91             }
92 12         284 note "HTTP::Tiny ", join(' ', $result->{success}, $result->{status}, $result->{reason});
93 12 100       6057 if($args->{data_callback})
94             {
95 2         17 $args->{data_callback}->($content, $result);
96             }
97             else
98             {
99 10         48 $result->{content} = $content;
100             }
101 12         188 return $result;
102             }
103             else
104             {
105 0         0 $request_method->(@_);
106             }
107             };
108              
109 2     2   15470 do { no warnings; *HTTP::Tiny::request = $request_wrapper };
  2         5  
  2         157  
110              
111             package
112             Test::HTTPTinyFile::ResponseHeaderTie;
113              
114 2     2   758 BEGIN { *note = \&Test::More::note }
115              
116             sub TIEHASH
117             {
118 12     12   73 my($class) = @_;
119 12         67 bless {}, $class;
120             }
121              
122             sub FETCH
123             {
124 2     2   300 my($self, $key) = @_;
125 2         16 note "header FETCH $key";
126 2         905 $self->{$key};
127             }
128              
129             sub STORE
130             {
131 12     12   682 my($self, $key, $value) = @_;
132 12         122 note "header STORE $key $value";
133 12         7798 $self->{$key} = $value;
134             }
135              
136             sub DELETE
137             {
138 0     0   0 my($self, $key) = @_;
139 0         0 note "header DELETE $key";
140 0         0 delete $self->{$key};
141             }
142              
143             sub CLEAR
144             {
145 0     0   0 my($self) = @_;
146 0         0 note "header CLEAR";
147 0         0 %$self = ();
148             }
149              
150             sub EXISTS
151             {
152 0     0   0 my($self, $key) = @_;
153 0         0 note "header EXISTS $key";
154 0         0 exists $self->{$key};
155             }
156              
157             sub FIRSTKEY
158             {
159 0     0   0 my($self) = @_;
160 0         0 note "header FIRSTKEY";
161 0         0 die "TODO";
162             }
163              
164             sub NEXTKEY
165             {
166 0     0   0 my($self, $lastkey) = @_;
167 0         0 note "header NEXTKEY $lastkey";
168 0         0 die "TODO";
169             }
170              
171             sub SCALAR
172             {
173 0     0   0 my($self) = @_;
174 0         0 note "header SCALAR";
175 0         0 scalar %$self;
176             }
177              
178             sub DESTROY
179             {
180 12     12   17941 my($self) = @_;
181 12         132 note "header DESTROY";
182             }
183              
184             sub UNTIE
185             {
186 0     0     my($self) = @_;
187 0           note "header UNTIE";
188             }
189              
190             1;