How do I check for error conditions with code that communicates with remote web servers?

I have a small project that is basically a Python wrapper for a website API.

It's pretty well tested, but there are some conditions that I can't work out how easy it is to test when the remote API is not available or is otherwise broken.

In particular, I would really like a test for each of the following:

  • When the site is not available (connection timeout)
  • When the site is available but returns an HTTP error code (such as a 404 or 500 error)
  • The content is distorted. The site has an XML interface. The site ran into problems some time ago and the page that was supposed to be an XML file was an HTML page that broke XML parsing

How can I check these cases? The only thing I can think of is changing the API url to a non-existent server (for an unreachable case) and a local web server for 500/404 / malformed data errors

0


source to share


1 answer


Another answer is to mock the code that actually makes calls to the website and returns the required error conditions. I guess it means moo urllib or httplib in python



+2


source







All Articles