Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

test script for LWP::UA based code

by danmcb (Monk)
on Jul 25, 2008 at 15:17 UTC ( [id://700157]=perlquestion: print w/replies, xml ) Need Help??

danmcb has asked for the wisdom of the Perl Monks concerning the following question:

I have a test script where I run an HTTP server in the background, send some requests to it with LWP::UserAgent, and then try to check the results. The server works and IS getting called (have checked with a telnet, also inserting brutal 'die' statements into it), the UA gets the response, but my attempts to get the server to share info about the last request (using an "our" variable) are failing. The code is below. Any ideas?

Another odd thing here is that although the UA timeout is set to 1 second, doing a netstat after running the script shows there is something still waiting for a response on that port. It takes maybe 10 or 15 secs to stop.

#!/usr/local/bin/perl -w use strict; use LWP::UserAgent; my $status = 'SOME_STATUS'; my $path = '/verify'; our $params; # fake server to verify against package MyServer; use HTTP::Server::Simple::CGI; use base qw(HTTP::Server::Simple::CGI); sub handle_request { my $self = shift; my $cgi = shift; my $p = $cgi->path_info(); $params = $cgi->Vars; #hardcoding this value doesn't help print "HTTP/1.0 200 OK\r\n"; if ( $p eq $path ) { print "Content-Type: text/plain\r\n"; print "Content-Length: ".length($status)."\r\n\r\n"; print "status\r\n"; } } package main; my $pid = MyServer->new(8000)->background(); my $ua = LWP::UserAgent->new; $ua->timeout( 1 ); my $ures = $ua->post('http://localhost:8000/verify', { v1 => 'abc', v2 + => 'def' }); kill( 3, $pid); print "params were : $params\n"; # expect to see { v1 => 'abc' ... }

Here's what we see on the command line:</p<

daniel@kinkyboots:~/work/engoi/trunk/Engoi$ ./test_server.pl HTTP::Server::Simple: You can connect to your server at http://localho +st:8000/ Use of uninitialized value $params in concatenation (.) or string at . +/test_server.pl line 40, <DATA> line 16. params were : daniel@kinkyboots:~/work/engoi/trunk/Engoi$ netstat -ap|grep 8000 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 localhost:8000 localhost:39238 TI +ME_WAIT -

Replies are listed 'Best First'.
Re: test script for LWP::UA based code
by Corion (Patriarch) on Jul 25, 2008 at 16:20 UTC

    In the test suite for WWW::Mechanize::Shell and WWW::Mechanize, I wrote a small webserver for testing. That webserver logs all output into a file, and the test launches the webserver, makes one or more requests and then visits a special URL to stop the webserver. Then it reads the file to additionally check that everything is as it should. See Test::HTTP::LocalServer.

Re: test script for LWP::UA based code
by kyle (Abbot) on Jul 25, 2008 at 15:49 UTC

    Your "HTTP server in the background" (ephasis added) is actually running in a separate process with its own memory.

    my $pid = MyServer->new(8000)->background();

    Here you've gotten the process ID of that separate process when it's created. I'd guess there's a call to fork under there somewhere. Any info you want to share between processes will have to be done at arm's length with the usual IPC mechanisms.

Re: test script for LWP::UA based code
by Anonymous Monk on Jul 25, 2008 at 15:39 UTC
    You're assuming $params is somehow shared between forks, it isnt
Re: test script for LWP::UA based code
by Anonymous Monk on Jul 25, 2008 at 15:31 UTC
    Get/set the timeout value in seconds. The default timeout() value is 180 seconds, i.e. 3 minutes.

    The requests is aborted if no activity on the connection to the server is observed for timeout seconds. This means that the time it takes for the complete transaction and the request() method to actually return might be longer.

Re: test script for LWP::UA based code
by jimX11 (Friar) on Jul 26, 2008 at 16:41 UTC
    I see the trees you mention, can you tell me about the forest?

    What are you trying to do? Are you developing a test or a web appliation?

    Apache::Test may interest you, depending on what you're developing.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://700157]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (6)
As of 2024-03-28 16:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found