summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn L. Villalovos <john@sodarock.com>2022-01-16 11:51:09 -0800
committerJohn L. Villalovos <john@sodarock.com>2022-01-16 11:51:09 -0800
commitd16e41bda2c355077cbdc419fe2e1d994fdea403 (patch)
tree2fc3d719fdfd81b87f6dbfe124e6a4e8d69245ff
parent5254f193dc29d8854952aada19a72e5b4fc7ced0 (diff)
downloadgitlab-jlvillal/rm_httmock.tar.gz
test: convert usage of `match_querystring` to `match`jlvillal/rm_httmock
In the `responses` library the usage of `match_querystring` is deprecated. Convert to using `match`
-rw-r--r--tests/unit/helpers.py66
-rw-r--r--tests/unit/mixins/test_mixin_methods.py24
-rw-r--r--tests/unit/test_gitlab.py3
-rw-r--r--tests/unit/test_gitlab_http_methods.py112
4 files changed, 104 insertions, 101 deletions
diff --git a/tests/unit/helpers.py b/tests/unit/helpers.py
new file mode 100644
index 0000000..33a7c78
--- /dev/null
+++ b/tests/unit/helpers.py
@@ -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
diff --git a/tests/unit/mixins/test_mixin_methods.py b/tests/unit/mixins/test_mixin_methods.py
index 6ccda40..06cc322 100644
--- a/tests/unit/mixins/test_mixin_methods.py
+++ b/tests/unit/mixins/test_mixin_methods.py
@@ -35,7 +35,7 @@ def test_get_mixin(gl):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -57,7 +57,7 @@ def test_refresh_mixin(gl):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = FakeManager(gl)
@@ -80,7 +80,7 @@ def test_get_without_id_mixin(gl):
url=url,
json={"foo": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -102,7 +102,7 @@ def test_list_mixin(gl):
url=url,
json=[{"id": 42, "foo": "bar"}, {"id": 43, "foo": "baz"}],
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
# test RESTObjectList
@@ -134,7 +134,7 @@ def test_list_other_url(gl):
url=url,
json=[{"id": 42, "foo": "bar"}],
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -177,7 +177,7 @@ def test_create_mixin(gl):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -202,7 +202,7 @@ def test_create_mixin_custom_path(gl):
url=url,
json={"id": 42, "foo": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -243,7 +243,7 @@ def test_update_mixin(gl):
url=url,
json={"id": 42, "foo": "baz"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -268,7 +268,7 @@ def test_update_mixin_no_id(gl):
url=url,
json={"foo": "baz"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -289,7 +289,7 @@ def test_delete_mixin(gl):
url=url,
json="",
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -311,7 +311,7 @@ def test_save_mixin(gl):
url=url,
json={"id": 42, "foo": "baz"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
@@ -334,7 +334,7 @@ def test_set_mixin(gl):
url=url,
json={"key": "foo", "value": "bar"},
status=200,
- match_querystring=True,
+ match=[responses.matchers.query_param_matcher({})],
)
mgr = M(gl)
diff --git a/tests/unit/test_gitlab.py b/tests/unit/test_gitlab.py
index 4d742d3..3826627 100644
--- a/tests/unit/test_gitlab.py
+++ b/tests/unit/test_gitlab.py
@@ -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({})],
}
@@ -81,7 +81,6 @@ def resp_page_2():
"content_type": "application/json",
"status": 200,
"match": [responses.matchers.query_param_matcher(params)],
- "match_querystring": False,
}
diff --git a/tests/unit/test_gitlab_http_methods.py b/tests/unit/test_gitlab_http_methods.py
index 7641b40..a65b53e 100644
--- a/tests/unit/test_gitlab_http_methods.py
+++ b/tests/unit/test_gitlab_http_methods.py
@@ -1,13 +1,11 @@
-import datetime
-import io
-import json
-from typing import Optional
-
import pytest
import requests
import responses
from gitlab import GitlabHttpError, GitlabList, GitlabParsingError, RedirectError
+from tests.unit import helpers
+
+MATCH_EMPTY_QUERY_PARAMS = [responses.matchers.query_param_matcher({})]
def test_build_url(gl):
@@ -27,7 +25,7 @@ def test_http_request(gl):
url=url,
json=[{"name": "project1"}],
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
http_r = gl.http_request("get", "/projects")
@@ -44,7 +42,7 @@ def test_http_request_404(gl):
url=url,
json={},
status=400,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -61,7 +59,7 @@ def test_http_request_with_only_failures(gl, status_code):
url=url,
json={},
status=status_code,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -167,7 +165,7 @@ def create_redirect_response(
# Create a history which contains our original request which is redirected
history = [
- httmock_response(
+ helpers.httmock_response(
status_code=302,
content="",
headers={"Location": f"http://example.com/api/v4{api_path}"},
@@ -185,7 +183,7 @@ def create_redirect_response(
)
prepped = req.prepare()
- resp_obj = httmock_response(
+ resp_obj = helpers.httmock_response(
status_code=200,
content="",
headers={},
@@ -216,7 +214,7 @@ def test_http_request_302_get_does_not_raise(gl):
method=responses.GET,
url=url,
status=302,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
gl.http_request(verb=method, path=api_path)
@@ -240,7 +238,7 @@ def test_http_request_302_put_raises_redirect_error(gl):
method=responses.PUT,
url=url,
status=302,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(RedirectError) as exc:
gl.http_request(verb=method, path=api_path)
@@ -258,7 +256,7 @@ def test_get_request(gl):
url=url,
json={"name": "project1"},
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_get("/projects")
@@ -276,7 +274,7 @@ def test_get_request_raw(gl):
content_type="application/octet-stream",
body="content",
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_get("/projects")
@@ -292,7 +290,7 @@ def test_get_request_404(gl):
url=url,
json=[],
status=404,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -309,7 +307,7 @@ def test_get_request_invalid_data(gl):
body='["name": "project1"]',
content_type="application/json",
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabParsingError):
@@ -328,7 +326,7 @@ def test_list_request(gl):
json=[{"name": "project1"}],
headers={"X-Total": "1"},
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_list("/projects", as_list=True)
@@ -353,7 +351,7 @@ def test_list_request_404(gl):
url=url,
json=[],
status=404,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -370,7 +368,7 @@ def test_list_request_invalid_data(gl):
body='["name": "project1"]',
content_type="application/json",
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabParsingError):
@@ -386,7 +384,7 @@ def test_post_request(gl):
url=url,
json={"name": "project1"},
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_post("/projects")
@@ -403,7 +401,7 @@ def test_post_request_404(gl):
url=url,
json=[],
status=404,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -420,7 +418,7 @@ def test_post_request_invalid_data(gl):
content_type="application/json",
body='["name": "project1"]',
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabParsingError):
@@ -436,7 +434,7 @@ def test_put_request(gl):
url=url,
json={"name": "project1"},
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_put("/projects")
@@ -453,7 +451,7 @@ def test_put_request_404(gl):
url=url,
json=[],
status=404,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
@@ -470,7 +468,7 @@ def test_put_request_invalid_data(gl):
body='["name": "project1"]',
content_type="application/json",
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabParsingError):
@@ -486,7 +484,7 @@ def test_delete_request(gl):
url=url,
json=True,
status=200,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
result = gl.http_delete("/projects")
@@ -503,69 +501,9 @@ def test_delete_request_404(gl):
url=url,
json=[],
status=404,
- match=[responses.matchers.query_param_matcher({})],
+ match=MATCH_EMPTY_QUERY_PARAMS,
)
with pytest.raises(GitlabHttpError):
gl.http_delete("/not_there")
assert responses.assert_call_count(url, 1) is True
-
-
-# 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