Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: How to unit test code that used LWP

by chrestomanci (Priest)
on Dec 18, 2012 at 12:27 UTC ( #1009351=note: print w/ replies, xml ) Need Help??


in reply to Re: How to unit test code that used LWP
in thread How to unit test code that used LWP

Thanks. Test::HTTP::Server certainly looks useful, though there is the issue of which server is contacted. And you are right, Test::LWP::UserAgent is pure perl, so there is nothing stopping me checking it into the source tree, other than an irrational worry that doing so could be the start of a slippery slope that will end with half of CPAN checked in, and the tree growing to many gigabytes in size.

In the end I decided that the simplest way to solve the problem was to directly mock the get and head methods in LWP::UserAgent. My mocked verson of head looks like this:

sub mocked_http_head { my($self, $url) = @_; ( $url =~ m!http://(.*)/(.*)! ) or die "malformed URL"; my $hostname = $1; my $file = $2; my $test_path = '/tmp/fake_server/'.$hostname.'/'.$file; my $response = Test::MockObject::Extends->new('HTTP::Response'); if( -f $test_path ) { my $fakeHeaders = HTTP::Headers->new('content-length' => -s $t +est_path ); $response->set_true('is_success'); $response->mock('headers', sub{ return $fakeHeaders } ); } else { $response->set_false('is_success'); } return $response; }

Using this approach I can easily simulate as many servers as I like, each with different content. I can simulate wrong content or corrupt downloads by putting a file in the /tmp/fake_server directory that differs from the database. The only thing I can't easily simulate is a server that is not listening (so LWP times out) or an aborted download so that the size from the content-length header differs from the file actualy delevered.

The other thing this does not simulate is any server side scripting, but that is not a requirement for what I am doing.


Comment on Re^2: How to unit test code that used LWP
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009351]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2014-08-20 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (107 votes), past polls