Validate URLs in web commands before execution (#2616)

Co-authored-by: Reinier van der Leer <github@pwuts.nl>
This commit is contained in:
Eddie Cohen
2023-04-24 06:33:44 -04:00
committed by GitHub
parent 794a164098
commit 40a75c804c
7 changed files with 120 additions and 82 deletions

View File

@@ -1,5 +1,6 @@
# Generated by CodiumAI
import pytest
import requests
from autogpt.commands.web_requests import scrape_text
@@ -58,9 +59,14 @@ class TestScrapeText:
url = "http://www.example.com"
assert scrape_text(url) == expected_text
# Tests that the function returns an error message when an invalid or unreachable
# Tests that an error is raised when an invalid url is provided.
def test_invalid_url(self):
url = "invalidurl.com"
pytest.raises(ValueError, scrape_text, url)
# Tests that the function returns an error message when an unreachable
# url is provided.
def test_invalid_url(self, mocker):
def test_unreachable_url(self, mocker):
# Mock the requests.get() method to raise an exception
mocker.patch(
"requests.Session.get", side_effect=requests.exceptions.RequestException
@@ -68,7 +74,7 @@ class TestScrapeText:
# Call the function with an invalid URL and assert that it returns an error
# message
url = "http://www.invalidurl.com"
url = "http://thiswebsitedoesnotexist.net/"
error_message = scrape_text(url)
assert "Error:" in error_message