8000 Merge pull request #1845 from python-gitlab/jlvillal/rm_httmock · python-gitlab/python-gitlab@ff4b1cc · GitHub
[go: up one dir, main page]

Skip to content

Commit ff4b1cc

Browse files
authored
Merge pull request #1845 from python-gitlab/jlvillal/rm_httmock
Remove usage of httmock and clean up deprecations
2 parents 8af403c + d16e41b commit ff4b1cc

File tree

5 files changed

+405
-238
lines changed

5 files changed

+405
-238
lines changed

requirements-test.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
coverage
2-
httmock
32
pytest==6.2.5
43
pytest-console-scripts==1.2.1
54
pytest-cov

tests/unit/helpers.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import datetime
2+
import io
3+
import json
4+
from typing import Optional
5+
6+
import requests
7+
8+
9+
# NOTE: The function `httmock_response` and the class `Headers` is taken from
10+
# https://github.com/patrys/httmock/ which is licensed under the Apache License, Version
11+
# 2.0. Thus it is allowed to be used in this project.
12+
# https://www.apache.org/licenses/GPL-compatibility.html
13+
class Headers(object):
14+
def __init__(self, res):
15+
self.headers = res.headers
16+
17+
def get_all(self, name, failobj=None):
18+
return self.getheaders(name)
19+
20+
def getheaders(self, name):
21+
return [self.headers.get(name)]
22+
23+
24+
def httmock_response(
25+
status_code: int = 200,
26+
content: str = "",
27+
headers=None,
28+
reason=None,
29+
elapsed=0,
30+
request: Optional[requests.models.PreparedRequest] = None,
31+
stream: bool = False,
32+
http_vsn=11,
33+
) -> requests.models.Response:
34+
res = requests.Response()
35+
res.status_code = status_code
36+
if isinstance(content, (dict, list)):
37+
content = json.dumps(content).encode("utf-8")
38+
if isinstance(content, str):
39+
content = content.encode("utf-8")
40+
res._content = content
41+
res._content_consumed = content
42+
res.headers = requests.structures.CaseInsensitiveDict(headers or {})
43+
res.encoding = requests.utils.get_encoding_from_headers(res.headers)
44+
res.reason = reason
45+
res.elapsed = datetime.timedelta(elapsed)
46+
res.request = request
47+
if hasattr(request, "url"):
48+
res.url = request.url
49+
if isinstance(request.url, bytes):
50+
res.url = request.url.decode("utf-8")
51+
if "set-cookie" in res.headers:
52+
res.cookies.extract_cookies(
53+
requests.cookies.MockResponse(Headers(res)),
54+
requests.cookies.MockRequest(request),
55+
)
56+
if stream:
57+
res.raw = io.BytesIO(content)
58+
else:
59+
res.raw = io.BytesIO(b"")
60+
res.raw.version = http_vsn
61+
62+
# normally this closes the underlying connection,
63+
# but we have nothing to free.
64+
res.close = lambda *args, **kwargs: None
65+
66+
return res

tests/unit/mixins/test_mixin_methods.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class M(GetMixin, FakeManager):
3535
url=url,
3636
json={"id": 42, "foo": "bar"},
3737
status=200,
38-
match_querystring=True,
38+
match=[responses.matchers.query_param_matcher({})],
3939
)
4040

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

6363
mgr = FakeManager(gl)
@@ -80,7 +80,7 @@ class M(GetWithoutIdMixin, FakeManager):
8080
url=url,
8181
json={"foo": "bar"},
8282
status=200,
83-
match_querystring=True,
83+
match=[responses.matchers.query_param_matcher({})],
8484
)
8585

8686
mgr = M(gl)
@@ -102,7 +102,7 @@ class M(ListMixin, FakeManager):
102102
url=url,
103103
< F438 span class=pl-s1>json=[{"id": 42, "foo": "bar"}, {"id": 43, "foo": "baz"}],
104104
status=200,
105-
match_querystring=True,
105+
match=[responses.matchers.query_param_matcher({})],
106106
)
107107

108108
# test RESTObjectList
@@ -134,7 +134,7 @@ class M(ListMixin, FakeManager):
134134
url=url,
135135
json=[{"id": 42, "foo": "bar"}],
136136
status=200,
137-
match_querystring=True,
137+
match=[responses.matchers.query_param_matcher({})],
138138
)
139139

140140
mgr = M(gl)
@@ -177,7 +177,7 @@ class M(CreateMixin, FakeManager):
177177
url=url,
178178
json={"id": 42, "foo": "bar"},
179179
status=200,
180-
match_querystring=True,
180+
match=[responses.matchers.query_param_matcher({})],
181181
)
182182

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

208208
mgr = M(gl)
@@ -243,7 +243,7 @@ class M(UpdateMixin, FakeManager):
243243
url=url,
244244
json={"id": 42, "foo": "baz"},
245245
status=200,
246-
match_querystring=True,
246+
match=[responses.matchers.query_param_matcher({})],
247247
)
248248

249249
mgr = M(gl)
@@ -268,7 +268,7 @@ class M(UpdateMixin, FakeManager):
268268
url=url,
269269
json={"foo": "baz"},
270270
status=200,
271-
match_querystring=True,
271+
match=[responses.matchers.query_param_matcher({})],
272272
)
273273

274274
mgr = M(gl)
@@ -289,7 +289,7 @@ class M(DeleteMixin, FakeManager):
289289
url=url,
290290
json="",
291291
status=200,
292-
match_querystring=True,
292+
match=[responses.matchers.query_param_matcher({})],
293293
)
294294

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

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

340340
mgr = M(gl)

tests/unit/test_gitlab.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def resp_page_1():
5858
"headers": headers,
5959
"content_type": "application/json",
6060
"status": 200,
61-
"match_querystring": True,
61+
"match": [responses.matchers.query_param_matcher({})],
6262
}
6363

6464

@@ -81,7 +81,6 @@ def resp_page_2():
8181
"content_type": "application/json",
8282
"status": 200,
8383
"match": [responses.matchers.query_param_matcher(params)],
84-
"match_querystring": False,
8584
}
8685

8786

0 commit comments

Comments
 (0)
0