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.04';
4             ## use critic (RequireUseStrict)
5 11     11   486195 use strict;
  11         16  
  11         280  
6 11     11   34 use warnings;
  11         11  
  11         233  
7              
8 11     11   36 use Carp qw(croak);
  11         12  
  11         395  
9 11     11   894 use HTTP::Request;
  11         42238  
  11         247  
10 11     11   4715 use Sereal qw(decode_sereal);
  11         4337  
  11         429  
11 11     11   4773 use IO::File;
  11         8072  
  11         1022  
12 11     11   3889 use Plack::VCR::Interaction;
  11         16  
  11         271  
13 11     11   5567 use UNIVERSAL;
  11         121  
  11         37  
14              
15 11     11   4777 use namespace::clean;
  11         127745  
  11         57  
16              
17             sub new {
18 16     16 1 315694 my ( $class, %opts ) = @_;
19              
20 16 100       70 my $filename = $opts{'filename'} or croak "filename parameter required";
21              
22 15 100       67 my $file = IO::File->new($filename, 'r') or croak $!;
23              
24 14         1035 return bless {
25             file => $file,
26             }, $class;
27             }
28              
29             sub next {
30 34     34 1 19282 my ( $self ) = @_;
31              
32 34         70 my $file = $self->{'file'};
33              
34 34         41 my $size = '';
35 34         91 my $bytes = $file->read($size, 4);
36 34 100       380 return if $bytes == 0;
37 27 50       50 croak "Unexpected end of file" unless $bytes == 4;
38              
39 27         116 $size = unpack('N', $size);
40 27 100       186 if($size > -s $file) {
41 1         12 croak "Invalid file contents";
42             }
43 26         29 my $request = '';
44 26         50 $bytes = $file->read($request, $size);
45 26 50       130 croak "Unexpected end of file" unless $bytes == $size;
46 26         607 $request = decode_sereal($request);
47              
48 26 50       97 croak "Invalid file contents"
49             unless UNIVERSAL::isa($request, 'HTTP::Request');
50              
51 26         111 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__