3 # start a fake webserver, fork, and connect to ourselves
12 use Carp qw(carp croak);
14 =head2 C<< Test::HTTP::LocalServer->spawn %ARGS >>
16 This spawns a new HTTP server. The server will stay running until
17 C<< $server->stop >> is called.
25 scalar containing the page to be served
29 filename containing the page to be served
33 Set to true to make the spawned server output debug information
37 All served HTML will have the first %s replaced by the current location.
42 my ($class,%args) = @_;
46 local $ENV{TEST_HTTP_VERBOSE};
47 $ENV{TEST_HTTP_VERBOSE} = 1 if delete $args{debug};
50 if (my $html = delete $args{html}) {
51 # write the html to a temp file
52 my ($fh,$tempfile) = File::Temp::tempfile();
55 or die "Couldn't write tempfile $tempfile : $!";
57 push @{$self->{delete}},$tempfile;
58 $args{file} = $tempfile;
60 my ($fh,$logfile) = File::Temp::tempfile();
62 push @{$self->{delete}},$logfile;
63 $self->{logfile} = $logfile;
64 my $web_page = delete $args{file};
65 if (defined $web_page) {
66 $web_page = qq{"$web_page"}
71 my $server_file = File::Spec->catfile( $FindBin::Bin,'log-server' );
73 open my $server, qq'$^X "$server_file" "$web_page" "$logfile" |'
74 or die "Couldn't spawn fake server $server_file : $!";
77 die "Couldn't find fake server url" unless $url;
79 $self->{_fh} = $server;
81 my $lhurl = URI::URL->new( $url );
82 $lhurl->host( 'localhost' );
83 $self->{_server_url} = $lhurl;
85 diag "Started $lhurl";
90 =head2 C<< $server->port >>
92 This returns the port of the current server. As new instances
93 will most likely run under a different port, this is convenient
94 if you need to compare results from two runs.
99 carp __PACKAGE__ . '::port called without a server' unless $_[0]->{_server_url};
100 $_[0]->{_server_url}->port
103 =head2 C<< $server->url >>
105 This returns the url where you can contact the server. This url
106 is valid until you call
109 C<< $server->get_output >>
114 my $url = $_[0]->{_server_url}->abs;
116 return $url->as_string;
119 =head2 C<< $server->creds_required >>
121 This returns a URL for a page that requires HTTP Basic-Auth. The
122 content returned is invariant and irrelevant; this method is for
123 testing credential-passing code. The username is 'luser' and the
124 password is 'fnord'. When these credentials are passed, the returned
125 status will be 200, otherwise it will be 401.
130 return $_[0]->{_server_url} . 'creds_required';
133 =head2 C<< $server->stop >>
135 This stops the server process by requesting a special
141 get( $_[0]->{_server_url} . 'quit_server' );
142 undef $_[0]->{_server_url}
145 =head2 C<< $server->get_output >>
147 This stops the server by calling C<stop> and then returns the
148 output of the server process. This output will be a list of
149 all requests made to the server concatenated together
159 open LOG, '<', $self->{logfile}
160 or die "Couldn't retrieve logfile";
161 return join "", <LOG>;
166 $self->stop if $self->{_server_url};
167 if ( $self->{_fh} ) {
171 for my $file ( @{$self->{delete}} ) {
172 unlink $file or warn "Couldn't remove tempfile $file : $!\n";
180 =head1 COPYRIGHT AND LICENSE
182 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
184 Copyright (C) 2003 Max Maischein
188 Max Maischein, E<lt>corion@cpan.orgE<gt>
190 Please contact me if you find bugs or otherwise improve the module. More tests are also very welcome !
194 L<WWW::Mechanize>,L<WWW::Mechanize::Shell>