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.05'; # TRIAL
4             ## use critic (RequireUseStrict)
5 11     11   522088 use strict;
  11         17  
  11         265  
6 11     11   37 use warnings;
  11         9  
  11         227  
7              
8 11     11   35 use Carp qw(croak);
  11         8  
  11         367  
9 11     11   931 use HTTP::Request;
  11         30194  
  11         213  
10 11     11   4279 use Sereal qw(decode_sereal);
  11         4276  
  11         475  
11 11     11   4388 use IO::File;
  11         7187  
  11         954  
12 11     11   3338 use Plack::VCR::Interaction;
  11         15  
  11         209  
13 11     11   4740 use UNIVERSAL;
  11         102  
  11         30  
14              
15 11     11   4315 use namespace::clean;
  11         111827  
  11         49  
16              
17             sub new {
18 16     16 1 434722 my ( $class, %opts ) = @_;
19              
20 16 100       74 my $filename = $opts{'filename'} or croak "filename parameter required";
21              
22 15 100       71 my $file = IO::File->new($filename, 'r') or croak $!;
23              
24 14         942 return bless {
25             file => $file,
26             }, $class;
27             }
28              
29             sub next {
30 34     34 1 23184 my ( $self ) = @_;
31              
32 34         73 my $file = $self->{'file'};
33              
34 34         39 my $size = '';
35 34         100 my $bytes = $file->read($size, 4);
36 34 100       370 return if $bytes == 0;
37 27 50       55 croak "Unexpected end of file" unless $bytes == 4;
38              
39 27         108 $size = unpack('N', $size);
40 27 100       184 if($size > -s $file) {
41 1         13 croak "Invalid file contents";
42             }
43 26         30 my $request = '';
44 26         53 $bytes = $file->read($request, $size);
45 26 50       133 croak "Unexpected end of file" unless $bytes == $size;
46 26         617 $request = decode_sereal($request);
47              
48 26 50       96 croak "Invalid file contents"
49             unless UNIVERSAL::isa($request, 'HTTP::Request');
50              
51 26         115 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__