Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

How to test a module which needs a local HTTP server?

( #427740=categorized question: print w/ replies, xml ) Need Help??
Contributed by Realbot on Feb 03, 2005 at 17:54 UTC
Q&A  > testing


Answer: How to test a module which needs a local HTTP server?
contributed by merlyn

See how WWW::Mechanize tests using a local server with HTTP::Daemon.

Answer: How to test a module which needs a local HTTP server?
contributed by Realbot

I had this problem recently and after having taken a look at Gisle Aas HTTP::Daemon tests, I came up with this structure.
The two main files (runWebTest.pl, localHttpServer.pl) need not to be touched.
runWebTest.pl defines a subroutine (runWebTest) which takes two parameters.
The first parameter is a reference to a test routine to execute (defined in sampletest.t). This test sub will receive the server URL as parameter.
The second one is a file which should contains a subroutine named "handler" which will receive two parameters (HTTP::Request, HTTP::Daemon::ClientConn) and must supply the HTTP server logic for our test.

Here is the code with a sample test:
File: sampletest.t

use strict; use Test::More tests => 2; sub test { my $baseUrl = shift; use LWP::UserAgent; my $ua = new LWP::UserAgent; my $response = $ua->get($baseUrl."sample"); ok($response->is_success, "get ok"); like ($response->content, qr/expected text/, "sample test"); } require 'runWebTest.pl'; runWebTest(\&test, 'sampleHandler.pl');
File: sampleHandler.pl
sub handler { my ($r, $c) = @_; if ($r->method eq 'GET' and $r->url->path eq "/sample") { $c->send_basic_header(200); print $c "Content-Type: text/plain\015\012"; $c->send_crlf; print $c "this is the expected text"; } else { $c->send_error(404); } } 1;
File: runWebTest.pl
use strict; use Config; sub runWebTest { my ($testSub, $handlerScript) = @_; my $perl = $Config{'perlpath'}; $perl = $^X if $^O eq 'VMS' or -x $^X and $^X =~ m,^([a-z]:)?/,i; open(DAEMON, "$perl localHttpServer.pl $handlerScript |") or die " +Can't exec daemon: $!"; my $serverUrl = <DAEMON>; ($serverUrl) = $serverUrl =~ /<([^>]+)>/; sleep(2); $testSub->($serverUrl); quitWebServer($serverUrl); } sub quitWebServer { my $baseUrl = shift; use LWP::UserAgent; my $ua = new LWP::UserAgent; return $ua->get($baseUrl."quit"); } 1;
File: localHttpServer.pl
use strict; $| = 1; # autoflush require IO::Socket; require HTTP::Daemon; my $handlerScript = shift || die "no handler given"; my $d = HTTP::Daemon->new() || die; print "HTTP Server started at: <", $d->url, ">\n"; open(STDOUT, $^O eq 'VMS'? ">nl: " : ">/dev/null"); print STDERR "HTTP Server started\n"; while (my $c = $d->accept) { while (my $r = $c->get_request) { if ($r) { if ($r->method eq 'GET' and $r->url->path eq "/quit") { $c->send_error(503, "Bye, bye"); print STDERR "HTTP Server terminated\n"; exit; # terminate HTTP server } else { require $handlerScript; handler($r, $c); } } } $c->close; $c = undef; # close connection }
Answer: How to test a module which needs a local HTTP server?
contributed by jplindstrom

Depending on what you really need to test, it may work to... not use a web server. Check out WWW::Mechanize::Cached.

First you run your tests live on your machine while having the caching function save the pages to files (or some other caching mechanism). Then you can just re-run the test again, essentially in off-line mode since the results will come from disk.

I actually started doing this today when the thing to test wasn't "get web page", but to combine that with some processing.

/J

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others lurking in the Monastery: (10)
    As of 2015-07-08 07:59 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (96 votes), past polls