Test/Page/Real.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 75.0% 6 / 8 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 87.5% 7 / 8 Podpod 50.0% 1 / 2 totaltotal 88.5% 54 / 61 CC 6 SCAR 12.3
CC
6
Cov
92%
_read_raw_content
6.1
new
2.0
BEGIN
1.0
CRAP 3.4
« Test/Input/Multipart.pm ↑ Summary Test/Input.pm »
1 package CGI::Test::Page::Real;
2 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ####################################################################
5 # $Id: Real.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6 # $Name: cgi-test_0-104_t1 $
7 ####################################################################
8 #
9 # Copyright (c) 2001, Raphael Manfredi
10 #
11 # You may redistribute only under the terms of the Artistic License,
12 # as specified in the README file that comes with the distribution.
13  
14 #
15 # An abstract interface to a real page, which is the result of a valid output
16 # and not an HTTP error. The concrete representation is defined by heirs,
17 # depending on the Content-Type.
18 #
19  
20 16 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 16 ▶use base qw(CGI::Test::Page);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 #
25 # ->new
26 #
27 # Creation routine
28 #
29 sub new
30 0 ▶{
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
31 0 confess "deferred";
32 }
33  
34 #
35 # Attribute access
36 #
37  
38 sub uri
39 19 ▶{
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
40 19 my $this = shift;
41 19 return $this->{uri};
42 }
43  
44 #
45 # ->_init
46 #
47 # Initialize common attributes
48 #
49 sub _init
50 28 ▶{
Subroutine _init: called CC=1 SCAR=0.0
51 28 my $this = shift;
52  
53 28 my %params = @_;
54  
55 28 my $file = $params{-file};
56 28 $this->{server} = $params{-server};
57 28 $this->{content_type} = $params{-content_type};
58 28 $this->{user} = $params{-user};
59 28 $this->{uri} = $params{-uri};
60  
61 28 $this->_read_raw_content($file);
62  
63 28 return;
64 }
65  
66 #
67 # ->_read_raw_content
68 #
69 # Read file content verbatim into `raw_content', skipping header.
70 #
71 # Even in the case of an HTML content, reading the whole thing into memory
72 # as a big happy string means we can issue regexp queries.
73 #
74 sub _read_raw_content
75 28 ▶{
Subroutine _read_raw_content: called CC=6 SCAR=18.0
76 28 my ($self, $file_name) = @_;
77  
78 28 ▶ open my $fh, $file_name || die "Can't open $file_name: $!";
Condition50%1/2
$file_name || die("Can't open ${file_name}: $!")
l!l
280
MC/DC0%0/1
$file_name || die("Can't open ${file_name}: $!")
$file_name
Truth table50%1/2
A
A= $file_name
Aresult
11
00
79  
80 28 my %headers;
81 my $content_length;
82  
83 28 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 72 0 72
84 72 ▶ last if $line =~ /^\r?$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\r?$/ 28 44 72
85  
86 44 $line =~ s/\r\n$//;
87  
88 44 my ($h, $v) = $line =~ /^(.*?):\s+(.*)$/;
89 44 ▶ $headers{ $h } = $v if defined $h;
Branch: 1 branch
BranchTrueFalseTotal
if defined $h 44 0 44
90  
91 44 ▶ $content_length = $v if $h =~ /content[-_]length/i;
Branch: 1 branch
BranchTrueFalseTotal
if $h =~ /content[-_]length/i 16 28 44
92 }
93  
94 28 $self->{headers} = \%headers;
95 28 $self->{content_length} = $content_length;
96  
97 28 local $/ = undef; # Will slurp remaining
98 28 $self->{raw_content} = <$fh>;
99 28 close $fh;
100  
101 28 return;
102 }
103  
104 16 1;
105  
106 =head1 NAME
107  
108 CGI::Test::Page::Real - Abstract representation of a real page
109  
110 =head1 SYNOPSIS
111  
112 # Inherits from CGI::Test::Page
113 # $page holds a CGI::Test::Page::Real object
114  
115 use CGI::Test;
116  
117 ok 1, $page->raw_content =~ /test is ok/;
118 ok 2, $page->uri->scheme eq "http";
119 ok 3, $page->content_type !~ /html/;
120  
121 =head1 DESCRIPTION
122  
123 This class is the representation of a real page, i.e. something physically
124 returned by the server and which is not an error.
125  
126 =head1 INTERFACE
127  
128 The interface is the same as the one described in L<CGI::Test::Page>, with
129 the following additions:
130  
131 =over 4
132  
133 =item C<raw_content>
134  
135 Returns the raw content of the page, as a string.
136  
137 =item C<raw_content_ref>
138  
139 Returns a reference to the raw content of the page, to avoid making yet
140 another copy.
141  
142 =item C<uri>
143  
144 The URI object, identifying the page we requested.
145  
146 =back
147  
148 =head1 AUTHORS
149  
150 The original author is Raphael Manfredi.
151  
152 Steven Hilton was long time maintainer of this module.
153  
154 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
155  
156 =head1 SEE ALSO
157  
158 CGI::Test::Page(3), CGI::Test::Page::HTML(3), CGI::Test::Page::Other(3),
159 CGI::Test::Page::Text(3), URI(3).
160  
161 =cut
162  
« Test/Input/Multipart.pm ↑ Summary Test/Input.pm »