views:

137

answers:

4

Often, I find myself wanting to write a unit test for a portion of code that accesses HTTP resources as part of its normal function. Have you found any good ways to write these kinds of tests?

A: 

This is typically a function I would mock out for the tests... I don't like my tests depending on anything external... even worse if it is an external resource I have no control over (such as a 3rd party website).

Databases is one of the few external resources I often won't mock... I use DBUnit instead.

Mike Stone
A: 

I recently had to write a component that accessed a wiki and did some basic text scraping. The majority of tests I wrote validated the correct HTTP response code. As far as validating the actual resource goes, I would save an offline version of a known resource and check that the algorithm is gathering/processing the correct data.

Josh Smeaton
+5  A: 

Extract the part that accesses the HTTP resources out of your main code. Create an interface for that new component, In your test, mock the interface and return data that you can control reliably.

You can test the HTTP access as an integration test.

David Thibault
Another thing - if you are using an HTTP library or framework, there's a good chance that it is already working. You don't really need to test the library. You only need to test the bits of your code that sends/receives the data.
jop
A: 

Depending on which language or framework you're using, it may be straightforward to start up a locally-running HTTP server which serves up the resources you want.