Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I mock a web server in Perl?

Our web application works together with several web-services which we can't influence. After each workflow (tested with Selenium) a hook call to a web-service occurs. I would like to mock that server. Ideally, I want a HTTP server object which I can start and kill at will, and an URL dispatcher which would call certain subroutines in my test when called.

So far, I have found HTTP::Server::Simple and HTTP::Server::Brick and I've found the latter more appealing. Do you have any other insider tip?

like image 803
Nikolai Prokoschenko Avatar asked Mar 01 '23 13:03

Nikolai Prokoschenko


1 Answers

I used a combination of HTTP::Daemon and Template::Toolkit to do this.

package Test::WebService;

use HTTP::Daemon;
use HTTP::Response;
use IO::File;
use Template;

our $PID  = $$;

END { __PACKAGE__->StopWeb(); }

sub StartWeb : method {

    my $self    = shift;
    my $port    = shift;
    my %actions = $_[0] && ref($_[0]) eq 'HASH' ? %{ $_[0] } : @_ %2 ? () : @_;

    # Ignore CHLD
    local $SIG{CHLD} = 'IGNORE';

    # Fork
    my $pid = fork();

    if ( $pid == 0 )
    {
        # Create pid file
        _createPid( "/tmp/httpd.pid" );

        # Create server
        eval
        {
            # Create socket
            my $d = HTTP::Daemon->new
            (
                Listen      => 1, 
                LocalPort   => $port,
                Reuse       => 1,
            ) || die "Failed to bind socket";

            # Listen for connections
            while ( my $c = $d->accept )
            {
                # Process requests
                while ( my $r = $c->get_request() )
                {               
                    if ( defined( my $tmpl = $actions{ $r->uri()->path() } ) )
                    {
                        eval
                        {
                            # Create template object
                            my $tt = Template->new( {ABSOLUTE => 1 } );

                            # Create response
                            my $rs = HTTP::Response->new('200');

                            # Process template
                            $tt->process
                            (
                                $tmpl,
                                $r->uri()->query_form_hash(),
                                sub { $rs->content( shift ) }
                            );

                            # Send response
                            $c->send_response( $rs );
                        };

                        if ($@)
                        {
                            $c->send_error('500', $@ );
                        }

                    }
                    else
                    {
                        $c->send_error('404', 'No Template Found');
                    }
                }
            }
        };

        if ($@)
        {
            # Remove pid file
            unlink "/tmp/httpd.pid";

            # die
            die $@;
        }

        # Exit nicely
        exit(0);
    }

    # Wait up to 5 seconds for server to start;
    die "Failed to start http server" unless _waitpid( 5, "/tmp/httpd.pid" );   

}

sub StopWeb {

    # Only cleanup parent process.
    if ( $PID && $PID == $$ )
    {
        if ( my $fh = IO::File->new( "/tmp/httpd.pid", 'r') )
        {
            # Get pid.
            my $pid;
            $fh->read( $pid, 16384 ); 
            $pid =~ s/\D//g;

            # Kill server
            kill 4, $pid if $pid;
        }
    }
}

sub _createPid {

    my $fh = IO::File->new( shift, 'w') || die "Couldn't create pid";
    $fh->print("$$");
    $fh->close(); 

    return;
}

sub _waitpid {

    my $secs = shift || 5;
    my $file = shift || die "Missing pid file";

    for( my $i=0; $i < $secs; $i++ )
    {
        return 1 if -e $file;
        sleep 1;
    }

    return 0;
}

The Test code could then be written like:

#!/usr/bin/perl

use Test::More tests => 1;
use Test::WebService;

use MyApp;

Test::WebService->StartWeb( '8088', '/webservice/method' => 'my.tmpl' );

ok ( MyApp->methodThatCallsWebService(), 'yay!' );

1;
like image 171
David Avatar answered Mar 05 '23 14:03

David