From: Stefan Praszalowicz Date: Wed, 8 Aug 2012 18:12:32 +0000 (-0700) Subject: Wrap socket.error in ConnectionError (+ unit tests) X-Git-Tag: v0.13.7~13^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=875c9e18ab967464d13a2ea804119316c73277bb;p=services%2Fpython-requests.git Wrap socket.error in ConnectionError (+ unit tests) --- diff --git a/requests/models.py b/requests/models.py index d8c0f3e..44bf581 100644 --- a/requests/models.py +++ b/requests/models.py @@ -8,6 +8,7 @@ This module contains the primary objects that power Requests. """ import os +import socket from datetime import datetime from .hooks import dispatch_hook, HOOKS @@ -608,6 +609,9 @@ class Request(object): ) self.sent = True + except socket.error as sockerr: + raise ConnectionError(sockerr) + except MaxRetryError as e: raise ConnectionError(e) diff --git a/tests/test_requests.py b/tests/test_requests.py index dd08b2a..0a52c4c 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -811,6 +811,20 @@ class RequestsTestSuite(TestSetup, TestBaseMixin, unittest.TestCase): assert ds1.prefetch assert not ds2.prefetch + def test_connection_error(self): + try: + get('http://localhost:1/nope') + except requests.ConnectionError: + pass + else: + assert False + + def test_connection_error_with_safe_mode(self): + config = {'safe_mode': True} + r = get('http://localhost:1/nope', allow_redirects=False, config=config) + assert r.content == None + + # def test_invalid_content(self): # # WARNING: if you're using a terrible DNS provider (comcast), # # this will fail.