|XP is just a number|
How to unit test code that used LWPby chrestomanci (Priest)
|on Dec 17, 2012 at 10:56 UTC||Need Help??|
chrestomanci has asked for the
wisdom of the Perl Monks concerning the following question:
Greetings wise brothers.
In the project I am working on, I have some code that attempts to fetch files over http via LWP. I am confused at how I can write unit tests for that functionality.
The overall design is that I have a global database that lists all the files available, along with the size and checksum, and a list of http servers that may or may not have a cached copy of any particular file, so when a client needs a file it might have to try several servers before it finds one that has the one it is looking for. In simplified form my code looks like this:
For the unit testing, I would like to cover the case where a remote host does not respond, does not have a file, and where the fetched file arrives truncated or corrupt. The unit tests need to fit nicely into the codebase and not require much in the way of external dependencies. The other developers on the project won't mind installing another Ubuntu package, but they will complain more if I ask them to install something from CPAN. There would be no point asking them to run a local web server on their machines, or suchlike just to run unit tests.
So far I looked into Test::LWP::UserAgent which looks promising, but is not available as an Ubuntu package, and Test::Mock::LWP which is packaged for Ubuntu, but does not have much helpful documentation. With both those mocking modules there appears to be a problem in that they don't pay attention to the hostname portion of the URL, so I can't easily simulate the case where one host has a file and another does not.
Can you offer any suggestions? I can't believe that this has not been extensively covered before, but a search here did not turn up anything recent or useful.