Test/Page.pm

filter: Statementstmt 91.1% 41 / 45 Branchbran 50.0% 2 / 4 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 83.3% 15 / 18 Podpod 60.0% 9 / 15 totaltotal 80.0% 68 / 85 CC 4 SCAR 4.3
CC
4
Cov
86%
header
3.0
headers
2.1
new
2.0
CRAP 1.5
« Test/Page/Error.pm ↑ Summary Test/Form/Widget/Button.pm »
1 package CGI::Test::Page;
2 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ####################################################################
5 # $Id: Page.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 #
16 # An abstract representation of a page, returned by an HTTP request.
17 # The page can be an error, or a real page, each with its own class hierarchy.
18 #
19  
20 23 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 ######################################################################
23 #
24 # ->new
25 #
26 # Creation routine
27 #
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 # Common attribute access
36 #
37  
38 28 ▶sub raw_content {
Subroutine raw_content: called CC=1 SCAR=0.0
Pod undocumented
39 28 my ($self) = @_;
40  
41 28 return $self->{raw_content};
42 }
43  
44 3 ▶sub raw_content_ref {
Subroutine raw_content_ref: called CC=1 SCAR=0.0
Pod undocumented
45 3 my ($self) = @_;
46  
47 3 return \$self->{raw_content};
48 }
49  
50 3 ▶sub headers {
Subroutine headers: called CC=2 SCAR=7.2
Pod undocumented
51 3 my ($self) = @_;
52  
53 3 ▶ return $self->{headers} || {};
Condition50%1/2
$self->{'headers'} || {}
l!l
30
MC/DC0%0/1
$self->{'headers'} || {}
$self->{'headers'}
Truth table50%1/2
A
A= $self->{'headers'}
Aresult
11
00
54 }
55  
56 2 ▶sub header {
Subroutine header: called CC=3 SCAR=11.1
Pod undocumented
57 2 my ($self, $hdr) = @_;
58  
59 2 my %header = %{ $self->headers };
60  
61 2 my $value;
62  
63 2 $hdr = lc $hdr;
64  
65 # We're not concerned with performance here and would rather save
66 # the original headers as they were; hence searching instead of
67 # lowercasing header keys in _read_raw_content.
68 2 ▶ while ( my ($k, $v) = each %header ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %header) 2 0 2
69 2 ▶ if ( $hdr eq lc $k ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr eq lc $k) 2 0 2
70 2 $value = $v;
71 2 last;
72 }
73 }
74  
75 2 return $value;
76 }
77  
78 ######################################################################
79 sub content_length
80 1 ▶{
Subroutine content_length: called CC=1 SCAR=0.0
Pod undocumented
81 1 my $this = shift;
82 1 return $this->{content_length};
83 }
84  
85 ######################################################################
86 sub content_type
87 1 ▶{
Subroutine content_type: called CC=1 SCAR=0.0
Pod documented
88 1 my $this = shift;
89 1 $this->{content_type};
90 }
91  
92 ######################################################################
93 sub user
94 19 ▶{
Subroutine user: called CC=1 SCAR=0.0
Pod documented
95 19 my $this = shift;
96 19 $this->{user};
97 }
98  
99 ######################################################################
100 sub server
101 19 ▶{
Subroutine server: called CC=1 SCAR=0.0
Pod documented
102 19 my $this = shift;
103 19 return $this->{server};
104 }
105 ######################################################################
106  
107 #
108 # Queries
109 #
110  
111 ######################################################################
112 # Error code (0 = OK)
113 ######################################################################
114 sub error_code
115 9 ▶{
Subroutine error_code: called CC=1 SCAR=0.0
Pod documented
116 9 0
117 }
118  
119 ######################################################################
120 # True if page indicates HTTP error
121 ######################################################################
122 sub is_error
123 20 ▶{
Subroutine is_error: called CC=1 SCAR=0.0
Pod documented
124 20 0
125 }
126  
127 ######################################################################
128 sub form_count
129 0 ▶{
Subroutine form_count: not called CC=1 SCAR=6.9
Pod documented
130 0 0
131 }
132  
133 ######################################################################
134 sub is_ok
135 11 ▶{
Subroutine is_ok: called CC=1 SCAR=0.0
Pod documented
136 11 my $this = shift;
137 11 return !$this->is_error;
138 }
139  
140 ######################################################################
141 #
142 # ->forms
143 #
144 # Returns list ref of CGI::Test::Form objects, one per <FORM></FORM> in the
145 # document. The order is the same as the one in the raw document.
146 #
147 # Meant to be redefined in CGI::Test::Page::HTML.
148 #
149 ######################################################################
150 sub forms
151 0 ▶{
Subroutine forms: not called CC=1 SCAR=6.9
Pod documented
152 0 my $this = shift;
153 0 return [];
154 }
155  
156 ######################################################################
157 #
158 # ->delete
159 #
160 # Done with this page, cleanup by breaking circular refs.
161 #
162 ######################################################################
163 sub delete
164 3 ▶{
Subroutine delete: called CC=1 SCAR=0.0
Pod documented
165 3 my $this = shift;
166 3 $this->{server} = undef;
167 3 return;
168 }
169  
170 23 1;
171  
172 =head1 NAME
173  
174 CGI::Test::Page - Abstract represention of an HTTP reply content
175  
176 =head1 SYNOPSIS
177  
178 # Deferred class, only heirs can be created
179 # $page holds a CGI::Test::Page object
180  
181 use CGI::Test;
182  
183 ok 1, $page->is_ok;
184 ok 2, $page->user ne ''; # authenticated access
185  
186 my $ctype = $page->content_type;
187 ok 3, $ctype eq "text/plain";
188  
189 $page->delete;
190  
191 =head1 DESCRIPTION
192  
193 The C<CGI::Test::Page> class is deferred. It is an abstract representation
194 of an HTTP reply content, which would be displayed on a browser, as a page.
195 It does not necessarily hold HTML content.
196  
197 Here is an outline of the class hierarchy tree, with the leading C<CGI::Test::>
198 string stripped for readability, and a trailing C<*> indicating deferred
199 clases:
200  
201 Page*
202 Page::Error
203 Page::Real*
204 Page::HTML
205 Page::Other
206 Page::Text
207  
208 Those classes are constructed as needed by C<CGI::Test>. You must always
209 call I<delete> on them to break the circular references if you care about
210 reclaiming unused memory.
211  
212 =head1 INTERFACE
213  
214 This is the interface defined at the C<CGI::Test::Page> level.
215 Each subclass may add further specific features, but the following is
216 available to the whole hierarchy:
217  
218 =over 4
219  
220 =item C<content_type>
221  
222 The MIME content type, along with parameters, as it appeared in the headers.
223 For instance, it can be:
224  
225 text/html; charset=ISO-8859-1
226  
227 Don't assume it to be just C<text/html> though. Use something like:
228  
229 ok 1, $page->content_type =~ m|^text/html\b|;
230  
231 in your regression tests, which will match whether there are parameters
232 following the content type or not.
233  
234 =item C<delete>
235  
236 Breaks circular references to allow proper reclaiming of unused memory.
237 Must be the last thing to call on the object before forgetting about it.
238  
239 =item C<error_code>
240  
241 The error code. Will be 0 to mean OK, but otherwise HTTP error codes
242 are used, as described by L<HTTP::Status>.
243  
244 =item C<forms>
245  
246 Returns a list reference containing all the CGI forms on the page,
247 as C<CGI::Test::Form> objects. Will be an empty list for anything
248 but C<CGI::Test::Page::HTML>, naturally.
249  
250 =item C<form_count>
251  
252 The amount of forms held in the C<forms> list.
253  
254 =item C<is_error>
255  
256 Returns I<true> when the page indicates an HTTP error.
257  
258 =item C<is_ok>
259  
260 Returns I<true> when the page is not the result of an HTTP error.
261  
262 =item C<server>
263  
264 Returns the server object that returned the page. Currently, this is
265 the C<CGI::Test> object, but it might change one day. In any case, this
266 is the place where GET/POST requests may be addresed.
267  
268 =item C<user>
269  
270 The authenticated user that requested this page, or C<undef> if no
271 authentication was made.
272  
273 =back
274  
275 =head1 AUTHORS
276  
277 The original author is Raphael Manfredi.
278  
279 Steven Hilton was long time maintainer of this module.
280  
281 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
282  
283 =head1 SEE ALSO
284  
285 CGI::Test::Page::Error(3), CGI::Test::Page::Real(3).
286  
287 =cut
288  
« Test/Page/Error.pm ↑ Summary Test/Form/Widget/Button.pm »