Quick fix to gr.utils.validate_url (#2659)

* fix validate

* formatting

* fix tests

* added flaky mark
This commit is contained in:
Abubakar Abid 2022-11-16 12:21:51 -08:00 committed by GitHub
parent 4b57984ead
commit 8ec0a1e2b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View File

@ -7,6 +7,9 @@
* Updated the minimum FastApi used in tests to version 0.87 [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2647](https://github.com/gradio-app/gradio/pull/2647)
* Fixed bug where interfaces with examples could not be loaded with `gr.Interface.load` by [@freddyaboulton](https://github.com/freddyaboulton) [PR 2640](https://github.com/gradio-app/gradio/pull/2640)
* Fixed bug where the `interactive` property of a component could not be updated by [@freddyaboulton](https://github.com/freddyaboulton) in [PR 2639](https://github.com/gradio-app/gradio/pull/2639)
* Fixed bug where some URLs were not being recognized as valid URLs and thus were not
loading correctly in various components by [@abidlabs](https://github.com/abidlabs) in [PR 2659](https://github.com/gradio-app/gradio/pull/2659)
## Documentation Changes:
No changes to highlight.

View File

@ -678,12 +678,11 @@ def append_unique_suffix(name: str, list_of_names: List[str]):
def validate_url(possible_url: str) -> bool:
headers = {"User-Agent": "gradio (https://gradio.app/; team@gradio.app)"}
try:
if requests.get(possible_url).status_code == 200:
return True
return requests.get(possible_url, headers=headers).ok
except Exception:
pass
return False
return False
def is_update(val):

View File

@ -31,6 +31,7 @@ from gradio.utils import (
readme_to_html,
sanitize_list_for_csv,
sanitize_value_for_csv,
validate_url,
version_check,
)
@ -488,6 +489,21 @@ class TestSanitizeForCSV:
assert sanitize_list_for_csv([1, ["ab", "=de"]]) == [1, ["ab", "'=de"]]
class TestValidateURL:
@pytest.mark.flaky
def test_valid_urls(self):
assert validate_url("https://www.gradio.app")
assert validate_url("http://gradio.dev")
assert validate_url(
"https://upload.wikimedia.org/wikipedia/commons/b/b0/Bengal_tiger_%28Panthera_tigris_tigris%29_female_3_crop.jpg"
)
def test_invalid_urls(self):
assert not (validate_url("C:/Users/"))
assert not (validate_url("C:\\Users\\"))
assert not (validate_url("/home/user"))
class TestAppendUniqueSuffix:
def test_no_suffix(self):
name = "test"