File Coverage

blib/lib/WWW/Webrobot/SelftestRunner.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package WWW::Webrobot::SelftestRunner;
2              
3             # Author: Stefan Trcek
4             # Copyright(c) 2004 ABAS Software AG
5              
6 27     27   55037 use Exporter;
  27         53  
  27         3507  
7             @EXPORT_OK = qw/ RunTestplan RunTestplan2 HttpdEcho Config /;
8              
9 27     27   136 use strict;
  27         41  
  27         729  
10 27     27   126 use warnings;
  27         45  
  27         634  
11              
12 27     27   30660 use HTTP::Response;
  27         1061754  
  27         1216  
13 27     27   306 use HTTP::Headers;
  27         54  
  27         658  
14 27     27   154 use URI;
  27         43  
  27         646  
15              
16 27     27   17629 use WWW::Webrobot;
  0            
  0            
17             use WWW::Webrobot::StupidHTTPD;
18              
19              
20             =head1 NAME
21              
22             WWW::Webrobot::SelftestRunner - Start httpd and run a test plan
23              
24             =head1 SYNOPSIS
25              
26             use WWW::Webrobot::SelftestRunner qw/RunTestplan HttpdEcho Config/;
27             exit RunTestplan(HttpdEcho, Config(qw/Test/, $test_plan);
28              
29             exit RunTestplan(HttpdEcho, Config(qw/Test Html/, $test_plan);
30              
31             see also t/get.t
32              
33              
34             =head1 DESCRIPTION
35              
36             This package serves some functions to start a http daemon
37             and run a test plan.
38             It is only used for the test in the C directory.
39              
40              
41             =head1 FUNCTIONS
42              
43             =over
44              
45             =item RunTestplan($serverfunc, $config, $testplan)
46              
47             Run a C http daemon implementing C<$serverfunc>.
48             Then run a C http client using the configuration C<$config>
49             and the testplan C<$testplan>.
50              
51             =cut
52              
53             sub RunTestplan {
54             my ($exit, $webrobot) = RunTestplan2(@_);
55             return $exit;
56             }
57              
58             sub RunTestplan2 {
59             my ($server_func, $config, $test_plan) = @_;
60              
61             my $daemon = WWW::Webrobot::StupidHTTPD -> new();
62             $daemon -> start($server_func, fork_daemon => 1);
63              
64             $config .= "names=application=" . $daemon -> server_url() . "\n";
65             my $webrobot = WWW::Webrobot -> new($config);
66             my $exit = $webrobot -> run($test_plan);
67              
68             $daemon -> stop();
69              
70             return ($exit, $webrobot);
71             }
72              
73              
74             my $simple_html_text_0 = <<'EOF';
75            
76            
77             A_Static_Html_Page
78            
79            
80             A simple text.
81            
82            
83             EOF
84              
85             my $simple_html_text_1 = <<'EOF';
86            
87            
88             Confuse perl regular expressions: [a-z]
89             HTMLish text
90            
91            
92             EOF
93              
94             my $frame_0 = <<'EOF';
95            
96            
97            
98            
99             Your browser does not support frames.
100            
101            
102             EOF
103              
104             my $ACTION = {
105             # NOTE: depending on the key the HTTP response will be
106             # text/html or text/plain
107             url => sub {
108             my ($connection, $request) = @_;
109             $request -> uri();
110             },
111             content => sub {
112             my ($connection, $request) = @_;
113             $request -> content();
114             },
115             method => sub {
116             my ($connection, $request) = @_;
117             $request -> method();
118             },
119             headers => sub {
120             my ($connection, $request) = @_;
121             $request -> headers() -> as_string();
122             },
123             constant_html_0 => sub {
124             my ($connection, $request) = @_;
125             return $simple_html_text_0;
126             },
127             constant_html_1 => sub {
128             my ($connection, $request) = @_;
129             return $simple_html_text_1;
130             },
131             html_frame_0 => sub {
132             my ($connection, $request) = @_;
133             return $frame_0;
134             },
135             html_as_utf8 => sub {
136             my ($connection, $request) = @_;
137             my $path = $request->uri();
138             my $file = $path || "";
139             $file =~ s{^/*html_as_utf8/(.*)$}{$1};
140             local *F;
141             open F, "<$file" or die "Can't open '$file': $!";
142             my $html = do { local $/; };
143             close F;
144             return $html;
145             },
146             # 500 => sub {},
147             # Don't use '500' as a key: some tests rely that this key doesn't exist!
148             };
149              
150             =item HttpdEcho
151              
152             A simple server function for the C http daemon.
153             It read the requested url C
154             as an action where the action is denoted by the C part within the url.
155             The result of the action is returned as content in the response.
156             Actions are
157              
158             url echo the url as response content
159             content echo the content in the request as response content
160             method echo the method in the request as response content
161             headers echo a stringified form of the request headers as response content
162              
163             =cut
164              
165             sub HttpdEcho {
166             my %parm = (@_);
167             my $charset = $parm{charset} ? "; charset=" . $parm{charset} : "";
168             my $plain_header = HTTP::Headers -> new(Content_Type => "text/plain$charset");
169             my $html_header = HTTP::Headers -> new(Content_Type => "text/html$charset");
170             return sub {
171             my ($connection, $request) = @_;
172              
173             my $path = $request->uri();
174             my $action = $path || "";
175             $action =~ s{^/*([^/]+).*$}{$1};
176             $action = "" if ! exists $ACTION->{$action};
177              
178             my $response = (exists $ACTION->{$action}) ?
179             HTTP::Response -> new(
180             200,
181             undef,
182             ($action =~ /html/) ? $html_header : $plain_header,
183             $ACTION->{$action}->($connection, $request)
184             ) : HTTP::Response -> new(500, undef, $plain_header, undef);
185             $connection -> send_response($response);
186             };
187             };
188              
189              
190             =item Config(Modulenames...)
191              
192             Simple config string for C output.
193             Defaults to "Test" if no parameter is given.
194              
195             Example:
196              
197             my $cfg = Config(qw/Html Test/);
198             my $cfg = Config;
199              
200             =cut
201              
202             sub Config {
203             push @_, "Test" if ! @_;
204             return join "", map {"output=WWW::Webrobot::Print::$_\n"} @_;
205             }
206              
207              
208             =pod
209              
210             =back
211              
212             =cut
213              
214             1;