This repository has been archived on 2024-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
Bebop/bebop/tests/test_navigation.py

145 lines
5.9 KiB
Python
Raw Normal View History

2021-02-12 19:01:42 +01:00
import unittest
from ..navigation import (
get_parent_url, get_root_url, join_url, parse_url, pop_first_segment, remove_dot_segments,
remove_last_segment, set_parameter,
)
2021-02-12 19:01:42 +01:00
class TestNavigation(unittest.TestCase):
def test_parse_url(self):
# Basic complete URL.
res = parse_url("gemini://netloc/parse-me.gmi")
self.assertEqual(res["scheme"], "gemini")
self.assertEqual(res["netloc"], "netloc")
self.assertEqual(res["path"], "/parse-me.gmi")
2021-02-12 19:01:42 +01:00
# No scheme.
res_netloc = parse_url("//netloc/parse-me.gmi")
self.assertIsNone(res_netloc["scheme"], None)
for key in res_netloc:
if key == "scheme":
continue
self.assertEqual(res_netloc[key], res[key])
2021-02-12 19:01:42 +01:00
# No scheme but a default is provided.
res_netloc = parse_url("//netloc/parse-me.gmi", default_scheme="gemini")
self.assertDictEqual(res_netloc, res)
# No scheme nor netloc: only a path should be produced.
res = parse_url("dece.space/parse-me.gmi")
self.assertIsNone(res["scheme"])
self.assertIsNone(res["netloc"])
self.assertEqual(res["path"], "dece.space/parse-me.gmi")
# No scheme nor netloc but we should pretend having an absolute URL.
2021-02-12 19:01:42 +01:00
res = parse_url("dece.space/parse-me.gmi", absolute=True)
self.assertEqual(res["scheme"], "gemini")
self.assertEqual(res["netloc"], "dece.space")
self.assertEqual(res["path"], "/parse-me.gmi")
2021-02-12 19:01:42 +01:00
# HTTPS scheme.
2021-02-17 01:31:24 +01:00
res = parse_url("https://dece.space/index.html")
self.assertEqual(res["scheme"], "https")
self.assertEqual(res["netloc"], "dece.space")
self.assertEqual(res["path"], "/index.html")
2021-02-17 01:31:24 +01:00
# File scheme.
2021-03-08 23:40:03 +01:00
res = parse_url("file:///home/dece/gemini/index.gmi")
self.assertEqual(res["scheme"], "file")
self.assertEqual(res["path"], "/home/dece/gemini/index.gmi")
2021-03-08 23:40:03 +01:00
2021-02-12 19:01:42 +01:00
def test_join_url(self):
2021-02-17 01:31:24 +01:00
url = join_url("gemini://dece.space/", "some-file.gmi")
self.assertEqual(url, "gemini://dece.space/some-file.gmi")
url = join_url("gemini://dece.space/", "./some-file.gmi")
2021-02-12 19:01:42 +01:00
self.assertEqual(url, "gemini://dece.space/some-file.gmi")
2021-02-17 01:31:24 +01:00
url = join_url("gemini://dece.space/dir1", "/some-file.gmi")
2021-02-12 19:01:42 +01:00
self.assertEqual(url, "gemini://dece.space/some-file.gmi")
url = join_url("gemini://dece.space/dir1/file.gmi", "other-file.gmi")
self.assertEqual(url, "gemini://dece.space/dir1/other-file.gmi")
url = join_url("gemini://dece.space/dir1/file.gmi", "../top-level.gmi")
self.assertEqual(url, "gemini://dece.space/top-level.gmi")
url = join_url("s://hard/dir/a", "./../test/b/c/../d/e/f/../.././a.gmi")
self.assertEqual(url, "s://hard/test/b/d/a.gmi")
def test_remove_dot_segments(self):
paths = [
("index.gmi", "index.gmi"),
("/index.gmi", "/index.gmi"),
("./index.gmi", "index.gmi"),
("/./index.gmi", "/index.gmi"),
("/../index.gmi", "/index.gmi"),
("/a/b/c/./../../g", "/a/g"),
("mid/content=5/../6", "mid/6"),
("../../../../g", "g"),
]
for path, expected in paths:
self.assertEqual(
remove_dot_segments(path),
expected,
msg="path was " + path
)
def test_remove_last_segment(self):
self.assertEqual(remove_last_segment(""), "")
self.assertEqual(remove_last_segment("/"), "")
self.assertEqual(remove_last_segment("/a"), "")
self.assertEqual(remove_last_segment("/a/"), "/a")
self.assertEqual(remove_last_segment("/a/b"), "/a")
self.assertEqual(remove_last_segment("/a/b/c/d"), "/a/b/c")
self.assertEqual(remove_last_segment("///"), "//")
def test_pop_first_segment(self):
self.assertEqual(pop_first_segment(""), ("", ""))
self.assertEqual(pop_first_segment("a"), ("a", ""))
self.assertEqual(pop_first_segment("/a"), ("/a", ""))
self.assertEqual(pop_first_segment("/a/"), ("/a", "/"))
self.assertEqual(pop_first_segment("/a/b"), ("/a", "/b"))
self.assertEqual(pop_first_segment("a/b"), ("a", "/b"))
2021-02-16 19:10:11 +01:00
def test_set_parameter(self):
url = set_parameter("gemini://gus.guru/search", "my search")
self.assertEqual(url, "gemini://gus.guru/search?my%20search")
url = set_parameter("gemini://gus.guru/search?old%20search", "new")
self.assertEqual(url, "gemini://gus.guru/search?new")
def test_get_parent_url(self):
urls_and_parents = [
("gemini://host", "gemini://host"),
("gemini://host/", "gemini://host/"),
("gemini://host/a", "gemini://host/"),
("gemini://host/a/", "gemini://host/"),
("gemini://host/a/index.gmi", "gemini://host/a/"),
("gemini://host/a/b/", "gemini://host/a/"),
("gemini://host/a/b/file.flac", "gemini://host/a/b/"),
("//host/a/b", "//host/a/"),
("hey", "hey"), # does not really make sense but whatever
("hey/ho", "hey/"),
("hey/ho/letsgo", "hey/ho/"),
]
for url, parent in urls_and_parents:
self.assertEqual(
get_parent_url(url),
parent,
msg="URL was " + url)
def test_get_root_url(self):
urls_and_roots = [
("gemini://host", "gemini://host/"),
("gemini://host/", "gemini://host/"),
("gemini://host/a", "gemini://host/"),
("gemini://host/a/b/c", "gemini://host/"),
("//host/path", "//host/"),
("//host/path?query", "//host/"),
("dumb", "/"),
("dumb/dumber", "/"),
]
for url, root in urls_and_roots:
self.assertEqual(
get_root_url(url),
root,
msg="URL was " + url
)