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