Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct uuidv4 check #92

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading