8
8
import sys
9
9
from io import BytesIO , TextIOWrapper , BufferedReader
10
10
from iview .utils import fastforward
11
+ from errno import ECONNREFUSED
11
12
12
13
try : # Python 3.4
13
14
from importlib import reload
@@ -242,9 +243,14 @@ def test_close_error(self):
242
243
"Server handle() retried for POST" )
243
244
244
245
class TestMockHttp (TestPersistentHttp ):
246
+ def run (self , * pos , ** kw ):
247
+ with substattr (iview .utils .http .client , self .HTTPConnection ):
248
+ return TestPersistentHttp .run (self , * pos , ** kw )
249
+
250
+ class TestHttpSocket (TestMockHttp ):
245
251
class HTTPConnection (http .client .HTTPConnection ):
246
252
def connect (self ):
247
- self .sock = TestMockHttp .Socket (
253
+ self .sock = TestHttpSocket .Socket (
248
254
b"HTTP/1.1 200 First response\r \n "
249
255
b"Content-Length: 12\r \n "
250
256
b"\r \n "
@@ -267,10 +273,6 @@ def close(self, *pos, **kw):
267
273
def makefile (self , * pos , ** kw ):
268
274
return self .reader
269
275
270
- def run (self , * pos , ** kw ):
271
- with substattr (iview .utils .http .client , self .HTTPConnection ):
272
- return TestPersistentHttp .run (self , * pos , ** kw )
273
-
274
276
def test_reuse (self ):
275
277
"""Test existing connection is reused"""
276
278
with self .session .open ("http://localhost/one" ) as response :
@@ -297,6 +299,30 @@ def test_new_host(self):
297
299
self .assertIsNot (sock1 , sock2 , "Expected new socket connection" )
298
300
self .assertTrue (sock2 .reader , "Disconnected after second request" )
299
301
302
+ class TestHttpEstablishError (TestMockHttp ):
303
+ """Connection establishment errors should not trigger a retry"""
304
+ class HTTPConnection (http .client .HTTPConnection ):
305
+ def __init__ (self , * pos , ** kw ):
306
+ self .connect_count = 0
307
+ super ().__init__ (* pos , ** kw )
308
+ def connect (self ):
309
+ self .connect_count += 1
310
+ raise self .connect_exception
311
+
312
+ def test_refused (self ):
313
+ exception = EnvironmentError (ECONNREFUSED , "Mock connection refusal" )
314
+ self .HTTPConnection .connect_exception = exception
315
+ try :
316
+ self .session .open ("http://dummy" )
317
+ except http .client .HTTPException :
318
+ raise
319
+ except EnvironmentError as err :
320
+ if err .errno != ECONNREFUSED :
321
+ raise
322
+ else :
323
+ self .fail ("ECONNREFUSED not raised" )
324
+ self .assertEqual (1 , self .connection ._connection .connect_count )
325
+
300
326
import iview .comm
301
327
302
328
class TestProxy (TestCase ):
0 commit comments