Skip to content

Remove usage of httmock and clean up deprecations #1845

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

Merged
merged 2 commits into from
Jan 18, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
coverage
httmock
pytest==6.2.5
pytest-console-scripts==1.2.1
pytest-cov
Expand Down
66 changes: 66 additions & 0 deletions tests/unit/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import datetime
import io
import json
from typing import Optional

import requests


# NOTE: The function `httmock_response` and the class `Headers` is taken from
# https://github.com/patrys/httmock/ which is licensed under the Apache License, Version
# 2.0. Thus it is allowed to be used in this project.
# https://www.apache.org/licenses/GPL-compatibility.html
class Headers(object):
def __init__(self, res):
self.headers = res.headers

def get_all(self, name, failobj=None):
return self.getheaders(name)

def getheaders(self, name):
return [self.headers.get(name)]


def httmock_response(
status_code: int = 200,
content: str = "",
headers=None,
reason=None,
elapsed=0,
request: Optional[requests.models.PreparedRequest] = None,
stream: bool = False,
http_vsn=11,
) -> requests.models.Response:
res = requests.Response()
res.status_code = status_code
if isinstance(content, (dict, list)):
content = json.dumps(content).encode("utf-8")
if isinstance(content, str):
content = content.encode("utf-8")
res._content = content
res._content_consumed = content
res.headers = requests.structures.CaseInsensitiveDict(headers or {})
res.encoding = requests.utils.get_encoding_from_headers(res.headers)
res.reason = reason
res.elapsed = datetime.timedelta(elapsed)
res.request = request
if hasattr(request, "url"):
res.url = request.url
if isinstance(request.url, bytes):
res.url = request.url.decode("utf-8")
if "set-cookie" in res.headers:
res.cookies.extract_cookies(
requests.cookies.MockResponse(Headers(res)),
requests.cookies.MockRequest(request),
)
if stream:
res.raw = io.BytesIO(content)
else:
res.raw = io.BytesIO(b"")
res.raw.version = http_vsn

# normally this closes the underlying connection,
# but we have nothing to free.
res.close = lambda *args, **kwargs: None

return res
24 changes: 12 additions & 12 deletions tests/unit/mixins/test_mixin_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class M(GetMixin, FakeManager):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -57,7 +57,7 @@ class TestClass(RefreshMixin, FakeObject):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = FakeManager(gl)
Expand All @@ -80,7 +80,7 @@ class M(GetWithoutIdMixin, FakeManager):
url=url,
json={"foo": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -102,7 +102,7 @@ class M(ListMixin, FakeManager):
url=url,
json=[{"id": 42, "foo": "bar"}, {"id": 43, "foo": "baz"}],
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

# test RESTObjectList
Expand Down Expand Up @@ -134,7 +134,7 @@ class M(ListMixin, FakeManager):
url=url,
json=[{"id": 42, "foo": "bar"}],
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand Down Expand Up @@ -177,7 +177,7 @@ class M(CreateMixin, FakeManager):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -202,7 +202,7 @@ class M(CreateMixin, FakeManager):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand Down Expand Up @@ -243,7 +243,7 @@ class M(UpdateMixin, FakeManager):
url=url,
json={"id": 42, "foo": "baz"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -268,7 +268,7 @@ class M(UpdateMixin, FakeManager):
url=url,
json={"foo": "baz"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -289,7 +289,7 @@ class M(DeleteMixin, FakeManager):
url=url,
json="",
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -311,7 +311,7 @@ class TestClass(SaveMixin, base.RESTObject):
url=url,
json={"id": 42, "foo": "baz"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand All @@ -334,7 +334,7 @@ class M(SetMixin, FakeManager):
url=url,
json={"key": "foo", "value": "bar"},
status=200,
match_querystring=True,
match=[responses.matchers.query_param_matcher({})],
)

mgr = M(gl)
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/test_gitlab.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def resp_page_1():
"headers": headers,
"content_type": "application/json",
"status": 200,
"match_querystring": True,
"match": [responses.matchers.query_param_matcher({})],
}


Expand All @@ -81,7 +81,6 @@ def resp_page_2():
"content_type": "application/json",
"status": 200,
"match": [responses.matchers.query_param_matcher(params)],
"match_querystring": False,
}


Expand Down
Loading