My python application makes many http requests to many urls using urllib2. I would like to build a unit test suite to test my data parsing and error handling code.
I have a directory full of test data, with a number of files, each file containing a single http response, with headers and response data. (using curl -i) In some cases, these files contain http error messages (needed to test the error handling)
Ideally, I would like to create a mock object to replace urllib2.urlopen and return a mock response object.
I'm wondering if there is an easy way to have urllib2 load an HTTP response directly from a file and have urllib2 parse this data to create the appropriate response object (as if the response was read from a url.
I tried using url's constructed with "file://" protocol, however the http response headers at the top of the file were not read nor parsed properly.
Alternatively I am considering writing a small web server class to serve the test files, however this seems like a little more work than I'd like. It would be easier to have urllib2 somehow reconstruct the response object from the http responses I've already saved in files (without having to build a web server to serve them again)
Any ideas?