From d07652e41a9999cc5ff1d6b748614c92b829c1e2 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:04:52 +0200 Subject: [PATCH 1/9] Better abstraction for when inheriting from `mockhttp.Entry`. --- mocket/mocks/mockhttp.py | 23 ++++++++++++++--------- tests/test_http.py | 10 ++++++++++ 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/mocket/mocks/mockhttp.py b/mocket/mocks/mockhttp.py index 3db6a65d..97c127b4 100644 --- a/mocket/mocks/mockhttp.py +++ b/mocket/mocks/mockhttp.py @@ -142,8 +142,10 @@ class Entry(MocketEntry): request_cls = Request response_cls = Response - def __init__(self, uri, method, responses, match_querystring=True): - uri = urlsplit(uri) + default_config = {"match_querystring": True} + + def __init__(self, uri, method, responses, match_querystring: bool = True): + self.uri = urlsplit(uri) port = uri.port if not port: @@ -227,15 +229,16 @@ def register(cls, method, uri, *responses, **config): if "body" in config or "status" in config: raise AttributeError("Did you mean `Entry.single_register(...)`?") - default_config = dict(match_querystring=True, add_trailing_slash=True) - default_config.update(config) - config = default_config + if config.keys() - cls.default_config.keys(): + raise KeyError( + f"Invalid config keys: {config.keys() - cls.default_config.keys()}" + ) - if config["add_trailing_slash"] and not urlsplit(uri).path: - uri += "/" + _config = cls.default_config.copy() + _config.update({k: v for k, v in config.items() if k in _config}) Mocket.register( - cls(uri, method, responses, match_querystring=config["match_querystring"]) + cls(uri, method, responses, match_querystring=_config["match_querystring"]) ) @classmethod @@ -246,8 +249,9 @@ def single_register( body="", status=200, headers=None, - match_querystring=True, exception=None, + match_querystring=True, + **config, ): response = ( exception @@ -260,4 +264,5 @@ def single_register( uri, response, match_querystring=match_querystring, + **config, ) diff --git a/tests/test_http.py b/tests/test_http.py index afa31185..9b103f3e 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -433,3 +433,13 @@ def test_suggestion_for_register_and_status(self): url, status=201, ) + + def test_invalid_config_key(self): + url = "http://foobar.com/path" + with self.assertRaises(KeyError): + Entry.register( + Entry.POST, + url, + Response(body='{"foo":"bar0"}', status=200), + invalid_key=True, + ) From f9241ca6366e25a3a60f88dec582f69808df6b86 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:05:37 +0200 Subject: [PATCH 2/9] Pre-commit hooks bump. --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 4edd2b69..9eb1eca3 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,7 +5,7 @@ repos: - id: forbid-crlf - id: remove-crlf - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v5.0.0 + rev: v6.0.0 hooks: - id: trailing-whitespace - id: end-of-file-fixer @@ -15,7 +15,7 @@ repos: exclude: helm/ args: [ --unsafe ] - repo: https://github.com/charliermarsh/ruff-pre-commit - rev: "v0.11.11" + rev: "v0.12.10" hooks: - id: ruff args: [--fix, --exit-non-zero-on-fix] From 249af18b5b68faf77e9321a6e6a6fc6eafb90408 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:07:28 +0200 Subject: [PATCH 3/9] Revert. --- mocket/mocks/mockhttp.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mocket/mocks/mockhttp.py b/mocket/mocks/mockhttp.py index 97c127b4..f3c0d9f2 100644 --- a/mocket/mocks/mockhttp.py +++ b/mocket/mocks/mockhttp.py @@ -145,7 +145,7 @@ class Entry(MocketEntry): default_config = {"match_querystring": True} def __init__(self, uri, method, responses, match_querystring: bool = True): - self.uri = urlsplit(uri) + uri = urlsplit(uri) port = uri.port if not port: From 32468b5334b7dceba497ddb6827a4c1f75901dd9 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:13:26 +0200 Subject: [PATCH 4/9] Don't stop adding the trailing slash. --- mocket/mocks/mockhttp.py | 2 +- tests/test_http.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/mocket/mocks/mockhttp.py b/mocket/mocks/mockhttp.py index f3c0d9f2..35f198b3 100644 --- a/mocket/mocks/mockhttp.py +++ b/mocket/mocks/mockhttp.py @@ -153,7 +153,7 @@ def __init__(self, uri, method, responses, match_querystring: bool = True): super().__init__((uri.hostname, port), responses) self.schema = uri.scheme - self.path = uri.path + self.path = uri.path or "/" self.query = uri.query self.method = method.upper() self._sent_data = b"" diff --git a/tests/test_http.py b/tests/test_http.py index 9b103f3e..1af89a51 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -443,3 +443,8 @@ def test_invalid_config_key(self): Response(body='{"foo":"bar0"}', status=200), invalid_key=True, ) + + def test_add_trailing_slash(self): + url = "http://testme.org" + entry = Entry(url, "GET", [Response(body='{"foo":"bar0"}', status=200)]) + self.assertEqual(entry.path, "/") From ed5ac6ff2436a51d8d6e57d66d974efebe104a4b Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:22:27 +0200 Subject: [PATCH 5/9] Better test trailing slash. --- tests/test_http.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/test_http.py b/tests/test_http.py index 1af89a51..ab4057e3 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -12,7 +12,7 @@ import requests from mocket import Mocket, Mocketizer, mocketize -from mocket.mockhttp import Entry, Response +from mocket.mocks.mockhttp import Entry, Response class HttpTestCase(TestCase): @@ -448,3 +448,10 @@ def test_add_trailing_slash(self): url = "http://testme.org" entry = Entry(url, "GET", [Response(body='{"foo":"bar0"}', status=200)]) self.assertEqual(entry.path, "/") + + @mocketize + def test_mocket_with_no_path(self): + Entry.register(Entry.GET, "http://httpbin.local", Response(status=202)) + response = urlopen("http://httpbin.local/") + self.assertEqual(response.code, 202) + self.assertEqual(Mocket._entries[("httpbin.local", 80)][0].path, "/") From dbf9fa7b75da3c26f0f00505d98da4dc6b9ad4ee Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:33:11 +0200 Subject: [PATCH 6/9] Better abstraction. --- mocket/mocks/mockhttp.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/mocket/mocks/mockhttp.py b/mocket/mocks/mockhttp.py index 35f198b3..da1163b7 100644 --- a/mocket/mocks/mockhttp.py +++ b/mocket/mocks/mockhttp.py @@ -237,9 +237,7 @@ def register(cls, method, uri, *responses, **config): _config = cls.default_config.copy() _config.update({k: v for k, v in config.items() if k in _config}) - Mocket.register( - cls(uri, method, responses, match_querystring=_config["match_querystring"]) - ) + Mocket.register(cls(uri, method, responses, **_config)) @classmethod def single_register( From e45b79a3eb3a5dd402bd06650cdbacd8d49232f0 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:42:37 +0200 Subject: [PATCH 7/9] Adding coverage file. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 3452a467..445a345b 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ publish: clean install-test-requirements uv publish clean: - rm -rf *.egg-info dist/ requirements.txt uv.lock || true + rm -rf *.egg-info dist/ requirements.txt uv.lock coverage.xml || true find . -type d -name __pycache__ -exec rm -rf {} \; || true .PHONY: clean publish safetest test setup develop lint-python test-python _services-up From 570d2f3182e8ebdfbba28471eee72fb4aa46a139 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:47:14 +0200 Subject: [PATCH 8/9] Noqa for retrocompatibility import line. --- tests/test_https.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_https.py b/tests/test_https.py index f8c8549e..6736971e 100644 --- a/tests/test_https.py +++ b/tests/test_https.py @@ -7,7 +7,7 @@ import requests from mocket import Mocket, Mocketizer, mocketize -from mocket.mockhttp import Entry +from mocket.mockhttp import Entry # noqa - test retrocompatibility @pytest.fixture From a856d930ef3b497d0c7fddbdd4a12df8bfa87141 Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Sat, 23 Aug 2025 18:54:19 +0200 Subject: [PATCH 9/9] GitHub actions runners blocked. --- tests/test_https.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/test_https.py b/tests/test_https.py index 6736971e..83bd38cb 100644 --- a/tests/test_https.py +++ b/tests/test_https.py @@ -43,6 +43,7 @@ def test_json(response): @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)') +@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs") def test_truesendall_with_recording_https(url_to_mock): with tempfile.TemporaryDirectory() as temp_dir, Mocketizer( truesocket_recording_dir=temp_dir @@ -62,6 +63,7 @@ def test_truesendall_with_recording_https(url_to_mock): @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)') +@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs") def test_truesendall_after_mocket_session(url_to_mock): Mocket.enable() Mocket.disable() @@ -71,6 +73,7 @@ def test_truesendall_after_mocket_session(url_to_mock): @pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)') +@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs") def test_real_request_session(url_to_mock): session = requests.Session()