Skip to content

Commit d193c96

Browse files
authored
Better abstraction for when inheriting from mockhttp.Entry (#305)
* Better abstraction for when inheriting from `mockhttp.Entry`. * Pre-commit hooks bump. * Better tests for when adding trailing slash. * Adding coverage file to `make clean`. * Noqa for retrocompatibility import line. * Pytest xfail for GitHub actions runners blocked.
1 parent 09c9a4e commit d193c96

File tree

5 files changed

+44
-16
lines changed

5 files changed

+44
-16
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
- id: forbid-crlf
66
- id: remove-crlf
77
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v5.0.0
8+
rev: v6.0.0
99
hooks:
1010
- id: trailing-whitespace
1111
- id: end-of-file-fixer
@@ -15,7 +15,7 @@ repos:
1515
exclude: helm/
1616
args: [ --unsafe ]
1717
- repo: https://github.com/charliermarsh/ruff-pre-commit
18-
rev: "v0.11.11"
18+
rev: "v0.12.10"
1919
hooks:
2020
- id: ruff
2121
args: [--fix, --exit-non-zero-on-fix]

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ publish: clean install-test-requirements
4545
uv publish
4646

4747
clean:
48-
rm -rf *.egg-info dist/ requirements.txt uv.lock || true
48+
rm -rf *.egg-info dist/ requirements.txt uv.lock coverage.xml || true
4949
find . -type d -name __pycache__ -exec rm -rf {} \; || true
5050

5151
.PHONY: clean publish safetest test setup develop lint-python test-python _services-up

mocket/mocks/mockhttp.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ class Entry(MocketEntry):
142142
request_cls = Request
143143
response_cls = Response
144144

145-
def __init__(self, uri, method, responses, match_querystring=True):
145+
default_config = {"match_querystring": True}
146+
147+
def __init__(self, uri, method, responses, match_querystring: bool = True):
146148
uri = urlsplit(uri)
147149

148150
port = uri.port
@@ -151,7 +153,7 @@ def __init__(self, uri, method, responses, match_querystring=True):
151153

152154
super().__init__((uri.hostname, port), responses)
153155
self.schema = uri.scheme
154-
self.path = uri.path
156+
self.path = uri.path or "/"
155157
self.query = uri.query
156158
self.method = method.upper()
157159
self._sent_data = b""
@@ -227,16 +229,15 @@ def register(cls, method, uri, *responses, **config):
227229
if "body" in config or "status" in config:
228230
raise AttributeError("Did you mean `Entry.single_register(...)`?")
229231

230-
default_config = dict(match_querystring=True, add_trailing_slash=True)
231-
default_config.update(config)
232-
config = default_config
232+
if config.keys() - cls.default_config.keys():
233+
raise KeyError(
234+
f"Invalid config keys: {config.keys() - cls.default_config.keys()}"
235+
)
233236

234-
if config["add_trailing_slash"] and not urlsplit(uri).path:
235-
uri += "/"
237+
_config = cls.default_config.copy()
238+
_config.update({k: v for k, v in config.items() if k in _config})
236239

237-
Mocket.register(
238-
cls(uri, method, responses, match_querystring=config["match_querystring"])
239-
)
240+
Mocket.register(cls(uri, method, responses, **_config))
240241

241242
@classmethod
242243
def single_register(
@@ -246,8 +247,9 @@ def single_register(
246247
body="",
247248
status=200,
248249
headers=None,
249-
match_querystring=True,
250250
exception=None,
251+
match_querystring=True,
252+
**config,
251253
):
252254
response = (
253255
exception
@@ -260,4 +262,5 @@ def single_register(
260262
uri,
261263
response,
262264
match_querystring=match_querystring,
265+
**config,
263266
)

tests/test_http.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import requests
1313

1414
from mocket import Mocket, Mocketizer, mocketize
15-
from mocket.mockhttp import Entry, Response
15+
from mocket.mocks.mockhttp import Entry, Response
1616

1717

1818
class HttpTestCase(TestCase):
@@ -433,3 +433,25 @@ def test_suggestion_for_register_and_status(self):
433433
url,
434434
status=201,
435435
)
436+
437+
def test_invalid_config_key(self):
438+
url = "http://foobar.com/path"
439+
with self.assertRaises(KeyError):
440+
Entry.register(
441+
Entry.POST,
442+
url,
443+
Response(body='{"foo":"bar0"}', status=200),
444+
invalid_key=True,
445+
)
446+
447+
def test_add_trailing_slash(self):
448+
url = "http://testme.org"
449+
entry = Entry(url, "GET", [Response(body='{"foo":"bar0"}', status=200)])
450+
self.assertEqual(entry.path, "/")
451+
452+
@mocketize
453+
def test_mocket_with_no_path(self):
454+
Entry.register(Entry.GET, "http://httpbin.local", Response(status=202))
455+
response = urlopen("http://httpbin.local/")
456+
self.assertEqual(response.code, 202)
457+
self.assertEqual(Mocket._entries[("httpbin.local", 80)][0].path, "/")

tests/test_https.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import requests
88

99
from mocket import Mocket, Mocketizer, mocketize
10-
from mocket.mockhttp import Entry
10+
from mocket.mockhttp import Entry # noqa - test retrocompatibility
1111

1212

1313
@pytest.fixture
@@ -43,6 +43,7 @@ def test_json(response):
4343

4444

4545
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
46+
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
4647
def test_truesendall_with_recording_https(url_to_mock):
4748
with tempfile.TemporaryDirectory() as temp_dir, Mocketizer(
4849
truesocket_recording_dir=temp_dir
@@ -62,6 +63,7 @@ def test_truesendall_with_recording_https(url_to_mock):
6263

6364

6465
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
66+
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
6567
def test_truesendall_after_mocket_session(url_to_mock):
6668
Mocket.enable()
6769
Mocket.disable()
@@ -71,6 +73,7 @@ def test_truesendall_after_mocket_session(url_to_mock):
7173

7274

7375
@pytest.mark.skipif('os.getenv("SKIP_TRUE_HTTP", False)')
76+
@pytest.mark.xfail(reason="Service down or blocking GitHub actions IPs")
7477
def test_real_request_session(url_to_mock):
7578
session = requests.Session()
7679

0 commit comments

Comments
 (0)