Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: How to unit test code that used LWP

by tobyink (Canon)
on Dec 17, 2012 at 20:15 UTC ( #1009226=note: print w/replies, xml ) Need Help??

in reply to How to unit test code that used LWP

Test::LWP::UserAgent is pure Perl, right? So if it's not available as an Ubuntu package, you could just bundle a copy in your project's t/lib/ dir.

The other possibility is to do things the other way around, and bundle a test HTTP server; then you can use a real client. I often do this; e.g. the test suite for IO::Callback::HTTP.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^2: How to unit test code that used LWP
by chrestomanci (Priest) on Dec 18, 2012 at 12:27 UTC

    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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2020-01-23 22:40 GMT
Find Nodes?
    Voting Booth?