File Coverage

blib/lib/Plack/VCR.pm
Criterion Covered Total %
statement 46 46 100.0
branch 11 14 78.5
condition n/a
subroutine 11 11 100.0
pod 2 2 100.0
total 70 73 95.8


line stmt bran cond sub pod time code
1             ## no critic (RequireUseStrict)
2             package Plack::VCR;
3             $Plack::VCR::VERSION = '0.06';
4             ## use critic (RequireUseStrict)
5 11     11   932222 use strict;
  11         29  
  11         420  
6 11     11   62 use warnings;
  11         20  
  11         454  
7              
8 11     11   63 use Carp qw(croak);
  11         24  
  11         817  
9 11     11   1052 use HTTP::Request;
  11         43457  
  11         489  
10 11     11   7685 use Sereal qw(decode_sereal);
  11         7496  
  11         817  
11 11     11   8738 use IO::File;
  11         15214  
  11         2000  
12 11     11   7210 use Plack::VCR::Interaction;
  11         31  
  11         507  
13 11     11   8400 use UNIVERSAL;
  11         135  
  11         67  
14              
15 11     11   8175 use namespace::clean;
  11         208992  
  11         64  
16              
17             sub new {
18 16     16 1 483274 my ( $class, %opts ) = @_;
19              
20 16 100       112 my $filename = $opts{'filename'} or croak "filename parameter required";
21              
22 15 100       104 my $file = IO::File->new($filename, 'r') or croak $!;
23              
24 14         1428 return bless {
25             file => $file,
26             }, $class;
27             }
28              
29             sub next {
30 34     34 1 34806 my ( $self ) = @_;
31              
32 34         107 my $file = $self->{'file'};
33              
34 34         59 my $size = '';
35 34         138 my $bytes = $file->read($size, 4);
36 34 100       543 return if $bytes == 0;
37 27 50       77 croak "Unexpected end of file" unless $bytes == 4;
38              
39 27         104 $size = unpack('N', $size);
40 27 100       291 if($size > -s $file) {
41 1         26 croak "Invalid file contents";
42             }
43 26         42 my $request = '';
44 26         82 $bytes = $file->read($request, $size);
45 26 50       226 croak "Unexpected end of file" unless $bytes == $size;
46 26         973 $request = decode_sereal($request);
47              
48 26 50       133 croak "Invalid file contents"
49             unless UNIVERSAL::isa($request, 'HTTP::Request');
50              
51 26         152 return Plack::VCR::Interaction->new(
52             request => $request,
53             );
54             }
55              
56             1;
57              
58             # ABSTRACT: API for interacting with a frozen request file
59              
60             __END__