test_urls.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """Integration tests for all urls in Reflex."""
  2. import os
  3. import re
  4. from pathlib import Path
  5. import pytest
  6. import requests
  7. def check_urls(repo_dir):
  8. """Check that all URLs in the repo are valid and secure.
  9. Args:
  10. repo_dir: The directory of the repo.
  11. Returns:
  12. A list of errors.
  13. """
  14. url_pattern = re.compile(r'http[s]?://reflex\.dev[^\s")]*')
  15. errors = []
  16. for root, _dirs, files in os.walk(repo_dir):
  17. if "__pycache__" in root:
  18. continue
  19. for file_name in files:
  20. if not file_name.endswith(".py") and not file_name.endswith(".md"):
  21. continue
  22. file_path = os.path.join(root, file_name)
  23. try:
  24. with open(file_path, "r", encoding="utf-8", errors="ignore") as file:
  25. for line in file:
  26. urls = url_pattern.findall(line)
  27. for url in set(urls):
  28. if url.startswith("http://"):
  29. errors.append(
  30. f"Found insecure HTTP URL: {url} in {file_path}"
  31. )
  32. url = url.strip('"\n')
  33. try:
  34. response = requests.head(
  35. url, allow_redirects=True, timeout=5
  36. )
  37. response.raise_for_status()
  38. except requests.RequestException as e:
  39. errors.append(
  40. f"Error accessing URL: {url} in {file_path} | Error: {e}, , Check your path ends with a /"
  41. )
  42. except Exception as e:
  43. errors.append(f"Error reading file: {file_path} | Error: {e}")
  44. return errors
  45. @pytest.mark.parametrize(
  46. "repo_dir",
  47. [Path(__file__).resolve().parent.parent / "reflex"],
  48. )
  49. def test_find_and_check_urls(repo_dir):
  50. """Test that all URLs in the repo are valid and secure.
  51. Args:
  52. repo_dir: The directory of the repo.
  53. """
  54. errors = check_urls(repo_dir)
  55. assert not errors, "\n".join(errors)