title: How do I simulate connection errors and request timeouts in python unit tests
body: <p>Suppose my django/flask application pulls in information from API's, how can I test that connection exceptions are caught and handled properly?</p>
  
  <p>So for example here is a function that calls an API:</p>
  
  <pre><code>import requests
  def call_the_api():
      url = 'http://httpbin.org/get'
      try:
          req = requests.get(url)
          if req.json().get('errors'):
              logger.warn("API error response")
              return {'request_error': 'api_error_response'}
      except requests.exceptions.ConnectionError:
          logger.warn('ConnectionError')
          return {'request_error': 'ConnectionTimeout'}
      except requests.exception.Timeout:
          logger.warn('API request timed out')
          return {'request_error': 'Timeout'}
      except Exception, ex:
          logger.warn("API request Exception: %s", ex)
          return {'request_error': ex}
      else:
          return req.json()
  </code></pre>
  
  <p>For testing responses from the API I found mock to be very useful.</p>
  
  <pre><code>def mock_get_request():
      response = requests.get.return_value
      json_file = 'sample_response.json'
      json_file_path = os.path.join(os.path.dirname(__file__), json_file)
      with open(json_file_path, 'r') as f:
          response.content = response.text = f.read()
      response.status_code = 200
      response.encoding = 'utf-8'
      response.json = lambda: json.loads(response.content.decode(response.encoding))
      response.url = u'%s' % args[0]
      return response
  
  class TestSuitabilityFunctions(TestCase):
      def test_call_the_api(self):
          requests.get = MagicMock(side_effect=mock_get_request)
          resp = call_the_api()
          self.assertEqual(resp.get('url'), "http://httpbin.org/get")
  </code></pre>
  
  <p>So my question is how would I go about simulating a connection timeout or error?</p>