From 848de6cceb792a9659deed1ac8b59f7387430c48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sondre=20Lilleb=C3=B8=20Gundersen?= Date: Tue, 23 Jul 2024 22:30:17 +0200 Subject: [PATCH] fix: Correct uuidv4 check --- asgi_correlation_id/middleware.py | 2 +- tests/test_middleware.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/asgi_correlation_id/middleware.py b/asgi_correlation_id/middleware.py index 72a6c70..f1fe502 100644 --- a/asgi_correlation_id/middleware.py +++ b/asgi_correlation_id/middleware.py @@ -19,7 +19,7 @@ def is_valid_uuid4(uuid_: str) -> bool: Check whether a string is a valid v4 uuid. """ try: - return bool(UUID(uuid_, version=4)) + return UUID(uuid_).version == 4 except ValueError: return False diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 4a2fba3..5848ed6 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -169,3 +169,12 @@ async def test_custom_generator(): async with AsyncClient(app=generator_app, base_url='http://test') as client: response = await client.get('test', headers={'X-Request-ID': 'bad-uuid'}) assert response.headers['X-Request-ID'] == TRANSFORMER_VALUE + + +def test_is_valid_uuid4(): + assert is_valid_uuid4('3758c31e-1177-4540-ba33-0109c405579a') is True + assert is_valid_uuid4('9e6454c4-21d5-4e4a-a66a-b28f15576414') is True + assert is_valid_uuid4('9e6454c421d54e4aa66ab28f15576414') is True + assert is_valid_uuid4('foo') is False + assert is_valid_uuid4('9e6454c4-21d5-4e4a-a66a-b28f15576414-1') is False + assert is_valid_uuid4('00000000000000000000000000000000') is False