| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5254f19 commit d16e41b
4 files changed
| Original file line number | Diff line number | Diff 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 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -35,7 +35,7 @@ class M(GetMixin, FakeManager): | |||
| 35 | 35 | url=url, | |
| 36 | 36 | json={"id": 42, "foo": "bar"}, | |
| 37 | 37 | status=200, | |
| 38 | - match_querystring=True, | ||
| 38 | + match=[responses.matchers.query_param_matcher({})], | ||
| 39 | 39 | ) | |
| 40 | 40 | ||
| 41 | 41 | mgr = M(gl) | |
@@ -57,7 +57,7 @@ class TestClass(RefreshMixin, FakeObject): | |||
| 57 | 57 | url=url, | |
| 58 | 58 | json={"id": 42, "foo": "bar"}, | |
| 59 | 59 | status=200, | |
| 60 | - match_querystring=True, | ||
| 60 | + match=[responses.matchers.query_param_matcher({})], | ||
| 61 | 61 | ) | |
| 62 | 62 | ||
| 63 | 63 | mgr = FakeManager(gl) | |
@@ -80,7 +80,7 @@ class M(GetWithoutIdMixin, FakeManager): | |||
| 80 | 80 | url=url, | |
| 81 | 81 | json={"foo": "bar"}, | |
| 82 | 82 | status=200, | |
| 83 | - match_querystring=True, | ||
| 83 | + match=[responses.matchers.query_param_matcher({})], | ||
| 84 | 84 | ) | |
| 85 | 85 | ||
| 86 | 86 | mgr = M(gl) | |
@@ -102,7 +102,7 @@ class M(ListMixin, FakeManager): | |||
| 102 | 102 | url=url, | |
| 103 | 103 | json=[{"id": 42, "foo": "bar"}, {"id": 43, "foo": "baz"}], | |
| 104 | 104 | status=200, | |
| 105 | - match_querystring=True, | ||
| 105 | + match=[responses.matchers.query_param_matcher({})], | ||
| 106 | 106 | ) | |
| 107 | 107 | ||
| 108 | 108 | # test RESTObjectList | |
@@ -134,7 +134,7 @@ class M(ListMixin, FakeManager): | |||
| 134 | 134 | url=url, | |
| 135 | 135 | json=[{"id": 42, "foo": "bar"}], | |
| 136 | 136 | status=200, | |
| 137 | - match_querystring=True, | ||
| 137 | + match=[responses.matchers.query_param_matcher({})], | ||
| 138 | 138 | ) | |
| 139 | 139 | ||
| 140 | 140 | mgr = M(gl) | |
@@ -177,7 +177,7 @@ class M(CreateMixin, FakeManager): | |||
| 177 | 177 | url=url, | |
| 178 | 178 | json={"id": 42, "foo": "bar"}, | |
| 179 | 179 | status=200, | |
| 180 | - match_querystring=True, | ||
| 180 | + match=[responses.matchers.query_param_matcher({})], | ||
| 181 | 181 | ) | |
| 182 | 182 | ||
| 183 | 183 | mgr = M(gl) | |
@@ -202,7 +202,7 @@ class M(CreateMixin, FakeManager): | |||
| 202 | 202 | url=url, | |
| 203 | 203 | json={"id": 42, "foo": "bar"}, | |
| 204 | 204 | status=200, | |
| 205 | - match_querystring=True, | ||
| 205 | + match=[responses.matchers.query_param_matcher({})], | ||
| 206 | 206 | ) | |
| 207 | 207 | ||
| 208 | 208 | mgr = M(gl) | |
@@ -243,7 +243,7 @@ class M(UpdateMixin, FakeManager): | |||
| 243 | 243 | url=url, | |
| 244 | 244 | json={"id": 42, "foo": "baz"}, | |
| 245 | 245 | status=200, | |
| 246 | - match_querystring=True, | ||
| 246 | + match=[responses.matchers.query_param_matcher({})], | ||
| 247 | 247 | ) | |
| 248 | 248 | ||
| 249 | 249 | mgr = M(gl) | |
@@ -268,7 +268,7 @@ class M(UpdateMixin, FakeManager): | |||
| 268 | 268 | url=url, | |
| 269 | 269 | json={"foo": "baz"}, | |
| 270 | 270 | status=200, | |
| 271 | - match_querystring=True, | ||
| 271 | + match=[responses.matchers.query_param_matcher({})], | ||
| 272 | 272 | ) | |
| 273 | 273 | ||
| 274 | 274 | mgr = M(gl) | |
@@ -289,7 +289,7 @@ class M(DeleteMixin, FakeManager): | |||
| 289 | 289 | url=url, | |
| 290 | 290 | json="", | |
| 291 | 291 | status=200, | |
| 292 | - match_querystring=True, | ||
| 292 | + match=[responses.matchers.query_param_matcher({})], | ||
| 293 | 293 | ) | |
| 294 | 294 | ||
| 295 | 295 | mgr = M(gl) | |
@@ -311,7 +311,7 @@ class TestClass(SaveMixin, base.RESTObject): | |||
| 311 | 311 | url=url, | |
| 312 | 312 | json={"id": 42, "foo": "baz"}, | |
| 313 | 313 | status=200, | |
| 314 | - match_querystring=True, | ||
| 314 | + match=[responses.matchers.query_param_matcher({})], | ||
| 315 | 315 | ) | |
| 316 | 316 | ||
| 317 | 317 | mgr = M(gl) | |
@@ -334,7 +334,7 @@ class M(SetMixin, FakeManager): | |||
| 334 | 334 | url=url, | |
| 335 | 335 | json={"key": "foo", "value": "bar"}, | |
| 336 | 336 | status=200, | |
| 337 | - match_querystring=True, | ||
| 337 | + match=[responses.matchers.query_param_matcher({})], | ||
| 338 | 338 | ) | |
| 339 | 339 | ||
| 340 | 340 | mgr = M(gl) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -58,7 +58,7 @@ def resp_page_1(): | |||
| 58 | 58 | "headers": headers, | |
| 59 | 59 | "content_type": "application/json", | |
| 60 | 60 | "status": 200, | |
| 61 | - "match_querystring": True, | ||
| 61 | + "match": [responses.matchers.query_param_matcher({})], | ||
| 62 | 62 | } | |
| 63 | 63 | ||
| 64 | 64 | ||
@@ -81,7 +81,6 @@ def resp_page_2(): | |||
| 81 | 81 | "content_type": "application/json", | |
| 82 | 82 | "status": 200, | |
| 83 | 83 | "match": [responses.matchers.query_param_matcher(params)], | |
| 84 | - "match_querystring": False, | ||
| 85 | 84 | } | |
| 86 | 85 | ||
| 87 | 86 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments