[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Testing CGI::Application with HTTP::Server::Simple(::CGI)


On 6/29/05, Shawn Sorichetti <suppressed> wrote:
>      HTTP::Server::Simple is a module by Jesse Vincent. It uses only
> modules that are included with Perl to provide a basic webserver,
> that works really well for testing.

This looks very useful for development work.  Perrin mentioned in his
MVC talk that Catalyst and OpenInteract both have a way to run this
simple server for doing development and testing.  It would be very
useful to have some simple scripts that do the same for
CGI::Application.

>      Currently I'm trying to get Devel::Cover to work with the
> HTTP::Server::Simple::CGI server in order to test coverage fo the
> webapp from a users point of view.

If you can't get Devel::Cover to work under this server, I have had
success doing testing by simulating a POST or GET request, and just
forking to handle the request (although I haven't used Devel::Cover in
this situation, but it may work).  Here is a quick example:

use CGI;
use Config;

die "fork not supported on this platform" unless $Config{d_fork};

my $req = &HTTP::Request::Common::POST(
    '/dummy_location',
    Content_Type => 'form-data',
    Content      => [
        name   => 'name1',
        test   => 'name2',
        image => ["t/image.jpg"],  # file upload
    ]
);
$ENV{REQUEST_METHOD} = 'POST';
$ENV{CONTENT_TYPE}   = 'multipart/form-data';
$ENV{CONTENT_LENGTH} = $req->content_length;
if ( open( CHILD, "|-" ) ) {    # cparent
    # Send the request info to the child
    print CHILD $req->content;
    close CHILD;
    exit 0;
}

# At this point we are in the child process, and it looks like a regular
# CGI request, so CGI.pm can read the POST params as if you were
# running under a real webserver
my $app = MY::App->new;
$app->run;


I am not sure how Devel::Cover will react in this situation, but I
have found this technique useful for testing.  You can see
Data::FormValidator::Filters::Image for a complete test example that
uses this technique (I have now figured out how to use Test::More in a
forked environment, so I will be replacing the custom 'test' function
in that testfile which was a workaround).


> I've included an example of a test below (I can add this to the Wiki
> if there is interest):

Yes, definately add it to the Wiki!  Thanks for the code...

Cheers,

Cees

> 
> use Test::More tests => 2;
> use Test::WWW::Mechanize;
> 
> use strict;
> use warnings;
> 
> # Setup a server to run the CGI::App Application
> my $server=CGIAppServer->new;
> my $pid=$server->background;
> ok($pid,'HTTP Server Started');
> 
> # If something goes wrong, make sure to stop the HTTP server.
> sub cleanup { kill(9,$pid) }
> $SIG{__DIE__}=\&cleanup;
> 
> # Begin Testing.
> my $mech=Test::WWW::Mechanize->new;
> $mech->get_ok('http://localhost:8080/','HTTP Get Index');
> 
> cleanup();
> 
> {
>      package CGIAppServer;
> 
>      use base 'HTTP::Server::Simple::CGI';
>      use MyCGIApp;
> 
>      sub handle_request {
>          my ($self, $cgi) = @_;
> 
>          return MyCGIApp->new->run;
>      }
>      1;
> }
> 
> ---
> Shawn Sorichetti
> 
> 
> ---------------------------------------------------------------------
> Web Archive:  http://www.mail-archive.com/suppressed/
>               http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
> To unsubscribe, e-mail: suppressed
> For additional commands, e-mail: suppressed
> 
>

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.