<p>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?</p>

[{'answer_id': 68605, 'author': 'Mike Stone', 'author_id': 122, 'author_profile': 'https://Stackoverflow.com/users/122', 'pm_score': 0, 'selected': False, 'text': "<p>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).</p>\n\n<p>Databases is one of the few external resources I often won't mock... I use DBUnit instead.</p>\n"}, {'answer_id': 68618, 'author': 'Josh Smeaton', 'author_id': 10583, 'author_profile': 'https://Stackoverflow.com/users/10583', 'pm_score': 0, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 68623, 'author': 'David Thibault', 'author_id': 5903, 'author_profile': 'https://Stackoverflow.com/users/5903', 'pm_score': 4, 'selected': True, 'text': '<p>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.</p>\n\n<p>You can test the HTTP access as an integration test.</p>\n'}, {'answer_id': 90712, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 0, 'selected': False, 'text': "<p>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.</p>\n"}]
['https://Stackoverflow.com/questions/68592', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2399475/']