Skip to content

Commit

Permalink
fix: Correct uuidv4 check
Browse files Browse the repository at this point in the history
  • Loading branch information
sondrelg committed Jul 29, 2024
1 parent c9927f3 commit 117687f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion asgi_correlation_id/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 117687f

Please sign in to comment.