File Coverage

blib/lib/HTTP/WebTest/Request.pm
Criterion Covered Total %
statement 40 41 97.5
branch 9 10 90.0
condition 7 9 77.7
subroutine 8 8 100.0
pod 3 3 100.0
total 67 71 94.3


line stmt bran cond sub pod time code
1             # $Id: Request.pm,v 1.6 2003/07/14 08:21:08 m_ilya Exp $
2              
3             package HTTP::WebTest::Request;
4              
5             =head1 NAME
6              
7             HTTP::WebTest::Request - HTTP request objects
8              
9             =head1 SYNOPSIS
10              
11             use HTTP::WebTest::Request;
12             $request = HTTP::WebTest::Request->new;
13              
14             my $uri = $request->uri;
15              
16             $request->base_uri($base_uri);
17             my $base_uri = $request->base_uri;
18              
19             my @params = @{$request->params};
20             $request->params([@params]);
21              
22             =head1 DESCRIPTION
23              
24             This class is a subclass of L class. It
25             extends it with continence methods that allow to set or get CGI query
26             params for HTTP request in uniform way independent of HTTP request
27             method.
28              
29             Each URI in GET requests may consist of two portions: URI of
30             document/resource/etc and CGI query string. In
31             L method C doesn't separate them and
32             operates on them as on single entity. In C
33             method C is not allowed to modify HTTP request URI. Instead of
34             it methods C and C should be used to change or get
35             these parts independently.
36              
37             For POST requests method C acts simular to C. On the
38             other hand C set content of HTTP request in case of POST
39             requests.
40              
41             CGI request parameters are defined in the way similar to CGI request
42             parameters defenition in
43             L. It is an array of
44             pairs
45              
46             ( name1 => value1, name2 => value2, ..., nameN => valueN )
47              
48             If any value is passed as an array reference it is treated as file
49             upload. See L for more
50             details.
51              
52             By default GET type of HTTP request is assumed. But if CGI request
53             parameters have data for file upload then POST type of HTTP request is
54             assumed.
55              
56             =head1 CLASS METHODS
57              
58             =cut
59              
60 21     21   34044 use strict;
  21         42  
  21         1146  
61              
62 21     21   118 use base qw(HTTP::Request);
  21         35  
  21         3297  
63              
64 21     21   65956 use HTTP::Request::Common;
  21         53988  
  21         2019  
65 21     21   270 use URI;
  21         33  
  21         727  
66              
67 21     21   735 use HTTP::WebTest::Utils qw(make_access_method);
  21         38  
  21         8352  
68              
69             =head2 base_uri($optional_uri)
70              
71             Can set non CGI query portion of request URI if C<$optional_uri> is
72             passed.
73              
74             =head3 Returns
75              
76             Non CGI query portion of request URI.
77              
78             =cut
79              
80 2441     2441 1 36832 sub base_uri { shift->SUPER::uri(@_) }
81              
82             =head2 uri($optional_uri)
83              
84             Method C is redefined. It is same as C for non-GET
85             request. For GET requests it returns URI with query parameters.
86              
87             =head3 Returns
88              
89             Whole URI.
90              
91             =cut
92              
93             sub uri {
94 1636     1636 1 653913 my $self = shift;
95              
96 1636 100       4826 if(@_) {
97 390         1285 $self->base_uri(@_);
98 390         62645 my $new_uri = $self->base_uri;
99 390         4025 $self->params([]);
100             }
101              
102 1636         4470 my $uri = $self->base_uri;
103              
104 1636 100 66     12733 if(@{$self->params} and $self->method and $self->method eq 'GET') {
  1636   100     7244  
105 61         1442 $uri->query_form(@{$self->params});
  61         174  
106             }
107              
108 1636         12791 return $uri;
109             }
110              
111             *url = \&uri;
112              
113             =head2 content_ref
114              
115             Method C is redefined. For POST requests it returns POST
116             query content corresponding to query parameters.
117              
118             =cut
119              
120             sub content_ref {
121 199     199 1 1829556 my $self = shift;
122              
123 199 100 66     1085 return $self->SUPER::content_ref
124             unless defined($self->method) and $self->method eq 'POST';
125              
126 12         391 my $has_filepart = grep ref($_), @{$self->params};
  12         88  
127              
128 12         29 my @post;
129 12 100       610 if($has_filepart) {
130 4         18 @post = ($self->uri,
131             Content_Type => 'form-data',
132             Content => $self->params);
133             } else {
134 8         32 @post = ($self->uri, $self->params);
135             }
136              
137 12         102 my $req = POST @post;
138              
139             # DANGER: EVIL HACK
140 12         10640 for my $header (qw(Content-Type Content-Length)) {
141 24 50       924 if(defined $header) {
142 24         103 $self->header($header => $req->header($header));
143             } else {
144 0         0 $self->remove_header($header);
145             }
146             }
147              
148 12         869 return $req->content_ref;
149             }
150              
151             =head2 params($optional_params)
152              
153             Can set CGI request parameters for this HTTP request object if an
154             array reference C<$optional_params> is passed.
155              
156             =head3 Returns
157              
158             An reference to an array that contains CGI request parameters.
159              
160             =cut
161              
162             *params = make_access_method('PARAMS', sub { [] });
163              
164             =head1 COPYRIGHT
165              
166             Copyright (c) 2001-2003 Ilya Martynov. All rights reserved.
167              
168             This program is free software; you can redistribute it and/or modify
169             it under the same terms as Perl itself.
170              
171             =head1 SEE ALSO
172              
173             L
174              
175             L
176              
177             L
178              
179             L
180              
181             =cut
182              
183             1;