mirror of
https://github.com/Dasemu/x-space-downloader.git
synced 2026-10-13 13:39:45 +00:00
feat: add validator error cases and twitter.com support
Added comprehensive test coverage for URL validation: - Test for invalid URL formats (missing /i/, missing space ID, malformed URLs) - Test for twitter.com domain support (in addition to x.com) Updated validator implementation: - Extended regex pattern to accept both x.com and twitter.com domains - Updated group capture index to account for domain alternation All validator tests passing (3/3). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -12,12 +12,12 @@ def validate_space_url(url: str) -> Tuple[bool, Optional[str], Optional[str]]:
|
|||||||
Returns:
|
Returns:
|
||||||
Tuple of (is_valid, space_id, error_message)
|
Tuple of (is_valid, space_id, error_message)
|
||||||
"""
|
"""
|
||||||
# Pattern for X Space URLs
|
# Pattern for X Space URLs (supports both x.com and twitter.com)
|
||||||
pattern = r"https://x\.com/i/spaces/([a-zA-Z0-9]+)"
|
pattern = r"https://(x|twitter)\.com/i/spaces/([a-zA-Z0-9]+)"
|
||||||
match = re.match(pattern, url)
|
match = re.match(pattern, url)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
return False, None, "Invalid URL format. Expected: https://x.com/i/spaces/{SPACE_ID}"
|
return False, None, "Invalid URL format. Expected: https://x.com/i/spaces/{SPACE_ID}"
|
||||||
|
|
||||||
space_id = match.group(1)
|
space_id = match.group(2) # Changed from group(1) to group(2)
|
||||||
return True, space_id, None
|
return True, space_id, None
|
||||||
|
|||||||
@@ -10,3 +10,29 @@ def test_validate_space_url_valid():
|
|||||||
assert is_valid is True
|
assert is_valid is True
|
||||||
assert space_id == "1ZkKzZWnVRRKv"
|
assert space_id == "1ZkKzZWnVRRKv"
|
||||||
assert error is None
|
assert error is None
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_space_url_invalid_format():
|
||||||
|
"""Test that invalid URLs are rejected."""
|
||||||
|
invalid_urls = [
|
||||||
|
"https://x.com/spaces/1ZkKzZWnVRRKv", # Missing /i/
|
||||||
|
"https://x.com/i/spaces/", # Missing space ID
|
||||||
|
"not a url", # Completely invalid
|
||||||
|
"", # Empty string
|
||||||
|
]
|
||||||
|
|
||||||
|
for url in invalid_urls:
|
||||||
|
is_valid, space_id, error = validate_space_url(url)
|
||||||
|
assert is_valid is False
|
||||||
|
assert space_id is None
|
||||||
|
assert "Invalid URL format" in error
|
||||||
|
|
||||||
|
|
||||||
|
def test_validate_space_url_twitter_domain():
|
||||||
|
"""Test that twitter.com URLs are also accepted."""
|
||||||
|
url = "https://twitter.com/i/spaces/1ZkKzZWnVRRKv"
|
||||||
|
is_valid, space_id, error = validate_space_url(url)
|
||||||
|
|
||||||
|
assert is_valid is True
|
||||||
|
assert space_id == "1ZkKzZWnVRRKv"
|
||||||
|
assert error is None
|
||||||
|
|||||||
Reference in New Issue
Block a user