Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix urllib3 mocking to conform to current API #602

Merged
merged 7 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/tox.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ jobs:
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
env:
PIP_TRUSTED_HOST: "pypi.python.org pypi.org files.pythonhosted.org"
- name: Install dependencies
run: |
sudo apt update
Expand Down
2 changes: 1 addition & 1 deletion requirements.devel.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
tox==3.14.3
tox==3.28.0
5 changes: 1 addition & 4 deletions requirements.docs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
Sphinx==2.4.4
docutils==0.16
jinja2<3.1 # We need an older version due to compatilibility
markupsafe<2.1 # Needed because of jinja2
Sphinx==7.0.1
3 changes: 2 additions & 1 deletion requirements.tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ setuptools==44.0.0;python_version<"3.10"
setuptools>64;python_version>="3.10"
coverage==5.0.3;python_version<"3.10"
coverage>=6;python_version>="3.10"
flaky==3.6.1
flaky==3.7.0;python_version<"3.6"
flaky==3.8.1;python_version>="3.6"

# for integration tests
# eventlet is pinned until https://github.com/benoitc/gunicorn/pull/2581
Expand Down
4 changes: 3 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ install_requires =
contextvars~=2.4;python_version>="3.5" and python_version<"3.7"

[options.extras_require]
gunicorn = gunicorn>=19.7.0
gunicorn =
gunicorn>=19.7.0;python_version>"3.6"
gunicorn==19.7.0,<21.0;python_version>="3.5" and python_version<"3.8"
raven = raven>=6.4.0
celery =
celery~=4.4;python_version~="3.5.0"
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@
'gevent>=20.9.0',
],
gunicorn=[
'gunicorn>=19.7.0',
'gunicorn>=19.7.0;python_version>"3.6"',
'gunicorn>=19.7.0,<21.0;python_version>="3.5" and python_version<"3.8"',
],
pg=[
'sqlparse>=0.4.2',
Expand Down
40 changes: 35 additions & 5 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
import talisker.statsd
import talisker.testing

try:
# Compatible urllib3 HTTPResponses subclass their Base class.
URLLIB3_COMPATIBLE_VERSION = issubclass(
urllib3.response.HTTPResponse,
urllib3.response.BaseHTTPResponse
)
except AttributeError:
URLLIB3_COMPATIBLE_VERSION = False


def request(method='GET', host='http://example.com', url='/', **kwargs):
req = requests.Request(method, url=host + url, **kwargs)
Expand Down Expand Up @@ -481,16 +490,36 @@ def set_responses(self, responses):
self.response_iter = iter(responses)

def make_response(self, content, status='200 OK', headers={}):
"""Make a fake http.client.HTTPResponse based on a byte stream."""
"""Make a fake HTTPResponse based on a byte stream.

For versions of urllib3 where urllib3.response.HTTPResponse is
not API-compatible with http.client.HTTPResponse, we return the
http.client version. For versions after, we return a
urllib3.response.HTTPResponse.
"""
if not headers:
headers["Content-Type"] = "text/html"

formatted_headers = '\r\n'.join(
'{}: {}'.format(k, v) for k, v in headers.items()
)
stream = 'HTTP/1.1 {}\r\n{}\r\n{}'.format(
stream = 'HTTP/1.1 {}\r\n{}\r\n\r\n{}'.format(
status, formatted_headers, content,
)
sock = FakeSocket(stream.encode('utf8'))
response = http.client.HTTPResponse(sock)
response.begin() # parse the stream
http_response = http.client.HTTPResponse(sock)
http_response.begin()

if not URLLIB3_COMPATIBLE_VERSION:
return http_response

response = urllib3.response.HTTPResponse(
body=http_response,
headers=headers,
status=http_response.status,
preload_content=False,
original_response=http_response,
)
return response

def make_request(self, pool, conn, method, url, **kwargs):
Expand Down Expand Up @@ -688,7 +717,8 @@ def test_adapter_exceptions_match_default(mock_urllib3, retry, response):

exc = None
try:
session.get('http://default/')
response = session.get('http://default/')
response.raise_for_status()
except Exception as e:
exc = e

Expand Down