O'Reilly logo

Perl Testing: A Developer's Notebook by Chromatic, Ian Langworth

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Testing with Apache-Test

"Running Your Own Apache Server" demonstrated how to start and stop an Apache server manually. In real life, you’ll probably start and stop the test server automatically when you want to run your test suite. This lab shows how to test and create a simple Apache module, called Thumbnail, that uses the Imager module to resize images to a certain width and height. How do you know this module works? Use Apache-Test to test it!

How do I do that?

First, create a lib/ directory. You should already have the t/ and /t/conf/ directories from the previous lab. t/TEST will be the same, but t/conf/extra.conf.in needs some editing.

Note

Remember to add this new lib/ directory to Perl’s search path.

In custom configuration files such as extra.conf.in, the server substitutes special variables (in the form of @ NAME @) with useful values when it starts. The server uses those directives in the file as its configuration. Adding the lib/ directory to Perl’s module search path is easy; add it to the SERVERROOT variable.

Save the following as t/conf/extra.conf.in:

    <IfModule mod_perl.c>
    
        <Perl>
            use lib '@SERVERROOT@/../lib';
            use Thumbnail ();
        </Perl>
    
        <Location /images>
            SetHandler perl-script
            PerlHandler Thumbnail
        </Location>
    
    </IfModule>

Save the Thumbnail module as lib/Thumbnail.pm:

 package Thumbnail; use strict; use warnings; use Apache::Constants qw(:common); use Apache::File; use Imager; our $constraint = 150; sub handler { my ($r) = @_; return DECLINED unless $r->content_type() =~ ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required