File Coverage

blib/lib/HTTP/WebTest/Plugin/SetRequest.pm
Criterion Covered Total %
statement 56 56 100.0
branch 29 30 96.6
condition n/a
subroutine 5 5 100.0
pod 1 2 50.0
total 91 93 97.8


line stmt bran cond sub pod time code
1             # $Id: SetRequest.pm,v 1.20 2003/03/02 11:52:09 m_ilya Exp $
2              
3             package HTTP::WebTest::Plugin::SetRequest;
4              
5             =head1 NAME
6              
7             HTTP::WebTest::Plugin::SetRequest - Initializes HTTP request for web test
8              
9             =head1 SYNOPSIS
10              
11             Not Applicable
12              
13             =head1 DESCRIPTION
14              
15             This plugin initializes the HTTP request for a web test.
16              
17             =cut
18              
19 10     10   69 use strict;
  10         21  
  10         343  
20 10     10   55 use URI;
  10         23  
  10         386  
21              
22 10     10   52 use base qw(HTTP::WebTest::Plugin);
  10         19  
  10         14275  
23              
24             =head1 TEST PARAMETERS
25              
26             =for pod_merge copy params
27              
28             =head2 relative_urls
29              
30             If set to C than C supports relative URLs. See
31             test parameter C for more information.
32              
33             =head3 Allowed values
34              
35             C, C
36              
37             =head3 Default value
38              
39             C
40              
41             =head2 url
42              
43             URL to test.
44              
45             If test parameter C is set to C than URL for each
46             test is treated as relative to the URL in the previous test. URL in
47             the first test is treated as relative to C.
48              
49             If test parameter C is set to C than each URL is
50             treated as absolute. In this case if schema part of URL is omitted
51             (i.e. URL doesn't start with C, C, etc) then
52             C is implied.
53              
54             =head2 method
55              
56             HTTP request method.
57              
58             See RFC 2616 (HTTP/1.1 protocol).
59              
60             =head3 Allowed values
61              
62             C, C
63              
64             =head3 Default value
65              
66             C
67              
68             =head2 http_headers
69              
70             A list of HTTP header/value pairs. Can be used to override default
71             HTTP headers or to add additional HTTP headers.
72              
73             =head3 Example
74              
75             http_headers = ( Accept => text/plain, text/html )
76              
77             =head2 params
78              
79             A list of name/value pairs to be passed as parameters to the URL.
80             (This element is used to test pages that process input from forms.)
81              
82             If the method key is set to C, these pairs are URI-escaped and
83             appended to the requested URL.
84              
85             Example (wtscript file):
86              
87             url = http://www.hotmail.com/cgi-bin/hmhome
88             params = ( curmbox
89             F001 A005
90             from
91             HotMail )
92              
93             generates the HTTP request with URI:
94              
95             http://www.hotmail.com/cgi-bin/hmhome?curmbox=F001%20A005&from=HotMail
96              
97             If the method key is set to C, as long as all values are scalars
98             they are URI-escaped and put into content of the HTTP request.
99             C content type is set for such HTTP
100             request.
101              
102             If the method key is set to C, some values may be defined as
103             lists. In this case L uses
104             C content type used for C
105             as specified in RFC 1867. Each parameter with list value is treated
106             as file part specification with the following interpretation:
107              
108             ( FILE, FILENAME, HEADER => VALUE... )
109              
110             where
111              
112             =over 4
113              
114             =item * FILE
115              
116             The name of a file to open. This file will be read and its content
117             placed in the request.
118              
119             =item * FILENAME
120              
121             The optional filename to be reported in the request. If it is not
122             specified than basename of C is used.
123              
124             =item * HEADER => VALUE
125              
126             Additional optional headers for file part.
127              
128             =back
129              
130             Example (wtscript file):
131              
132             url = http://www.server.com/upload.pl
133             method = post
134             params = ( submit => ok
135             file => ( '/home/ilya/file.txt', 'myfile.txt' ) )
136              
137             It generates HTTP request with C file included
138             and reported under name C.
139              
140             =head2 auth
141              
142             A list which contains two elements: userid/password pair to be used
143             for web page access authorization.
144              
145             =head2 proxies
146              
147             A list of service name/proxy URL pairs that specify proxy servers to
148             use for requests.
149              
150             =head3 Example
151              
152             proxies = ( http => http://http_proxy.mycompany.com
153             ftp => http://ftp_proxy.mycompany.com )
154              
155             =head2 pauth
156              
157             A list which contains two elements: userid/password pair to be used
158             for proxy server access authorization.
159              
160             =head2 user_agent
161              
162             Set the product token that is used to identify the user agent on
163             the network.
164              
165             =head3 Default value
166              
167             C
168              
169             where C is version number of HTTP-WebTest.
170              
171             =head2 handle_redirects
172              
173             If set to C then HTTP-WebTest automatically follows redirects.
174             It means that you never see HTTP responses with status codes 301 and
175             302. This feature is disabled if this test parameter is set to C.
176              
177             =head3 Allowed values
178              
179             C, C
180              
181             =head3 Default value
182              
183             C
184              
185             =head2 timeout
186              
187             Set the timeout value in seconds.
188              
189             =head3 Default value
190              
191             C<180>
192              
193             =cut
194              
195             sub param_types {
196 194     194 1 7299 return q(url uri
197             relative_urls yesno
198             method scalar('^(?:GET|POST)$')
199             params hashlist
200             auth list('scalar','scalar')
201             proxies hashlist
202             pauth list('scalar','scalar')
203             http_headers hashlist
204             user_agent scalar
205             handle_redirects yesno
206             timeout scalar);
207             }
208              
209             sub prepare_request {
210 194     194 0 370 my $self = shift;
211              
212             # get user agent object
213 194         849 my $user_agent = $self->webtest->user_agent;
214              
215             # get request object
216 194         895 my $request = $self->webtest->current_request;
217              
218 194         1226 $self->validate_params(qw(url relative_urls method params
219             auth proxies pauth
220             http_headers user_agent
221             handle_redirects timeout));
222              
223             # get various params we handle
224 194         1419 my $url = $self->test_param('url');
225 194         937 my $relative_urls = $self->yesno_test_param('relative_urls');
226 194         675 my $method = $self->test_param('method');
227 194         805 my $params = $self->test_param('params');
228 194         629 my $auth = $self->test_param('auth');
229 194         1448 my $proxies = $self->test_param('proxies');
230 194         648 my $pauth = $self->test_param('pauth');
231 194         677 my $headers = $self->test_param('http_headers');
232 194         670 my $ua_name = $self->test_param('user_agent');
233 194         702 my $handle_redirects = $self->yesno_test_param('handle_redirects', 1);
234 194         698 my $timeout = $self->test_param('timeout', 180);
235              
236             # set LWP's timeout
237 194         661 $self->webtest->user_agent->timeout($timeout);
238              
239             # normalize uri
240 194 100       5626 if(defined $url) {
241 177 100       2546 if($relative_urls) {
242 7         30 my $current_test_num = $self->webtest->current_test_num;
243              
244 7 100       41 my $prev_url = $current_test_num > 0 ?
245             $self->webtest->tests->[$current_test_num - 1]->request->uri :
246             'http://localhost/';
247              
248 7         61 $url = URI->new_abs($url, $prev_url);
249             } else {
250             # add shema part if it is missing
251 170 100       1751 $url = "http://" . $url unless $url =~ m|^\w+://|;
252             }
253             }
254              
255             # set request uri
256 194 100       5352 $request->uri($url) if defined $url;
257              
258             # set request method (with default GET)
259 194 100       1814994 if(defined $method) {
260 15 100       102 if($method =~ /^POST$/i) {
261 10         47 $request->method('POST');
262             # ensure correct default value for content-type header
263 10         153 $request->header(Content_Type =>
264             'application/x-www-form-urlencoded');
265             } else {
266 5         25 $request->method('GET');
267             }
268             } else {
269 179         769 $request->method('GET');
270             }
271              
272             # set request params
273 194 100       5156 if(defined $params) {
274 15 50       95 my @params = ref($params) eq 'ARRAY' ? @$params : %$params;
275 15         65 $request->params(\@params);
276             }
277              
278             # pass authorization data
279 194 100       546 if(defined $auth) {
280 3         38 $request->authorization_basic(@$auth);
281             }
282              
283             # set proxies
284 194 100       2000 if(defined $proxies) {
285 6         31 for my $i (0 .. @$proxies / 2 - 1) {
286 8         396 $user_agent->proxy(@$proxies[2 * $i, 2 * $i + 1]);
287             }
288             }
289              
290             # pass proxy authorization data
291 194 100       1354 if(defined $pauth) {
292 3         34 $request->proxy_authorization_basic(@$pauth);
293             }
294              
295             # set http headers
296 194 100       1119 if(defined $headers) {
297 2         21 $request->header(@$headers);
298             }
299              
300             # set user agent name
301 194 100       5393 $ua_name = 'HTTP-WebTest/' . HTTP::WebTest->VERSION
302             unless defined $ua_name;
303 194         1428 $user_agent->agent($ua_name);
304              
305             # define if requests are redirectable
306 194 100       22732 if($handle_redirects) {
307 192         1921 $user_agent->requests_redirectable([qw(GET POST)]);
308             } else {
309 2         16 $user_agent->requests_redirectable([]);
310             }
311             }
312              
313             =head1 COPYRIGHT
314              
315             Copyright (c) 2000-2001 Richard Anderson. All rights reserved.
316              
317             Copyright (c) 2001-2003 Ilya Martynov. All rights reserved.
318              
319             This program is free software; you can redistribute it and/or modify
320             it under the same terms as Perl itself.
321              
322             =head1 SEE ALSO
323              
324             L
325              
326             L
327              
328             L
329              
330             L
331              
332             =cut
333              
334             1;