test_urlhelper.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import pytest
  2. from gemini_antenna.URLHelper import URLHelper
  3. @pytest.mark.blocklist_rules(
  4. "gemini://gemini.circumlunar.space",
  5. "https://",
  6. )
  7. def test_blocklist(url_helper: URLHelper):
  8. assert url_helper.isBlocked("gemini://gemini.circumlunar.space/news/atom.xml")
  9. assert url_helper.isBlocked("gemini.circumlunar.space/news/")
  10. assert url_helper.isBlocked("https://example.com/feed")
  11. assert not url_helper.isBlocked("gopher://gemini.circumlunar.space/")
  12. assert not url_helper.isBlocked("gemini://example.com/feed")
  13. assert not url_helper.isBlocked("")
  14. with pytest.raises(TypeError):
  15. url_helper.isBlocked(None)
  16. def test_url_validation():
  17. assert URLHelper.mightBeAURL("https://😀.com")
  18. assert not URLHelper.mightBeAURL("gopher://")
  19. assert not URLHelper.mightBeAURL("gemini://.gmi")
  20. assert not URLHelper.mightBeAURL("//example.com/feed")
  21. assert not URLHelper.mightBeAURL("example.com/feed")
  22. assert not URLHelper.mightBeAURL("/etc/passwd")
  23. assert not URLHelper.mightBeAURL("mailto:example@example.com")
  24. with pytest.raises(TypeError):
  25. URLHelper.mightBeAURL(None)
  26. with pytest.raises(TypeError):
  27. URLHelper.mightBeAURL(123)
  28. with pytest.raises(TypeError):
  29. URLHelper.mightBeAURL(["gemini://example.com"])
  30. def test_url_correction():
  31. correctURLs = (
  32. "gemini://gemini.circumlunar.space/news/atom.xml",
  33. "GEMINI://example.com",
  34. "https://😀.com"
  35. )
  36. for url in correctURLs:
  37. assert URLHelper.correct(url) == url
  38. assert(URLHelper.correct("https%3A//example.com/feed%3Fformat%3Datom")
  39. == "https://example.com/feed?format=atom")
  40. assert(URLHelper.correct("example.com/my-feed")
  41. == "gemini://example.com/my-feed")
  42. def test_url_resolving():
  43. canonicalURLs = (
  44. "gemini://gemini.circumlunar.space/news/atom.xml",
  45. "gemini://gemini.circumlunar.space/news/"
  46. )
  47. for url in canonicalURLs:
  48. assert URLHelper.resolve(url) == url
  49. assert URLHelper.resolve("GEMINI://example.com") == "gemini://example.com"
  50. assert(URLHelper.resolve("gemini://example.com/../../..")
  51. == "gemini://example.com/")
  52. assert(URLHelper.resolve("gemini://example.org/feed/.")
  53. == "gemini://example.org/feed")
  54. assert(URLHelper.resolve("gemini://example.org/feed/./feed2")
  55. == "gemini://example.org/feed/feed2")
  56. assert(URLHelper.resolve("gemini://example.org/feed/./../feed2")
  57. == "gemini://example.org/feed2")