fix: is_youtube_url crashes on a non-string url (#1752)
This commit is contained in:
parent
d2f6e8068d
commit
2fa4d50115
2 changed files with 16 additions and 0 deletions
|
|
@ -59,6 +59,8 @@ def init_youtube():
|
|||
|
||||
|
||||
def is_youtube_url(url: str) -> bool:
|
||||
if not isinstance(url, str):
|
||||
return False
|
||||
return "youtube.com" in url or "youtu.be" in url
|
||||
|
||||
|
||||
|
|
|
|||
14
tests/test_is_youtube_url_nonstring.py
Normal file
14
tests/test_is_youtube_url_nonstring.py
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
from src.youtube_handler import is_youtube_url
|
||||
|
||||
|
||||
def test_is_youtube_url_handles_non_string():
|
||||
# `"youtube.com" in url` raises TypeError on a non-string; a url field that
|
||||
# can be None/other (e.g. from a JSON message) should just be "not YT".
|
||||
assert is_youtube_url(123) is False
|
||||
assert is_youtube_url(None) is False
|
||||
assert is_youtube_url({"u": 1}) is False
|
||||
|
||||
|
||||
def test_is_youtube_url_detects_real_urls():
|
||||
assert is_youtube_url("https://www.youtube.com/watch?v=x") is True
|
||||
assert is_youtube_url("https://youtu.be/x") is True
|
||||
Loading…
Add table
Reference in a new issue