31
31
import requests
32
32
import responses
33
33
import socket
34
- import sys
35
34
import threading
36
35
import time
37
36
from urllib .parse import urlunsplit
45
44
import talisker .statsd
46
45
import talisker .testing
47
46
47
+ try :
48
+ # Compatible urllib3 HTTPResponses subclass their Base class.
49
+ URLLIB3_COMPATIBLE_VERSION = issubclass (
50
+ urllib3 .response .HTTPResponse ,
51
+ urllib3 .response .BaseHTTPResponse
52
+ )
53
+ except AttributeError :
54
+ URLLIB3_COMPATIBLE_VERSION = False
55
+
48
56
49
57
def request (method = 'GET' , host = 'http://example.com' , url = '/' , ** kwargs ):
50
58
req = requests .Request (method , url = host + url , ** kwargs )
@@ -482,7 +490,13 @@ def set_responses(self, responses):
482
490
self .response_iter = iter (responses )
483
491
484
492
def make_response (self , content , status = '200 OK' , headers = {}):
485
- """Make a fake http.client.HTTPResponse based on a byte stream."""
493
+ """Make a fake HTTPResponse based on a byte stream.
494
+
495
+ For versions of urllib3 where urllib3.response.HTTPResponse is
496
+ not API-compatible with http.client.HTTPResponse, we return the
497
+ http.client version. For versions after, we return a
498
+ urllib3.response.HTTPResponse.
499
+ """
486
500
if not headers :
487
501
headers ["Content-Type" ] = "text/html"
488
502
@@ -496,8 +510,7 @@ def make_response(self, content, status='200 OK', headers={}):
496
510
http_response = http .client .HTTPResponse (sock )
497
511
http_response .begin ()
498
512
499
- # Python versions below 3.7 expect a non-urllib3 response.
500
- if sys .version_info .minor < 7 :
513
+ if not URLLIB3_COMPATIBLE_VERSION :
501
514
return http_response
502
515
503
516
response = urllib3 .response .HTTPResponse (
0 commit comments