diff --git a/src/osmium/replication/server.py b/src/osmium/replication/server.py index 37f2e64..60ce965 100644 --- a/src/osmium/replication/server.py +++ b/src/osmium/replication/server.py @@ -65,6 +65,15 @@ def __init__(self, url: str, diff_type: str = 'osc.gz') -> None: than osc.gz files, set the `diff_type` to the given file suffix. """ + # Canonicalize the baseurl to have exactle one trailing slash. + # Try to avoid calling `example.com/area-updates//state.txt` + # Don't turn `http://example.com/` into `http://example.com` + # Remove all trailing /'s then add one back + while url.endswith("/"): + url = url.rstrip("/") + # put a final / at the end + url = url + "/" + self.baseurl = url self.diff_type = diff_type self.extra_request_params: Dict[str, Any] = dict(timeout=60, stream=True) @@ -483,16 +492,19 @@ def get_state_url(self, seq: Optional[int]) -> str: i.e. the state file in the root directory of the replication service. """ + + # baseurl is guaranteed to end with /, as per __init__ if seq is None: - return self.baseurl + '/state.txt' + return self.baseurl + 'state.txt' - return '%s/%03i/%03i/%03i.state.txt' % \ + return '%s%03i/%03i/%03i.state.txt' % \ (self.baseurl, seq / 1000000, (seq % 1000000) / 1000, seq % 1000) def get_diff_url(self, seq: int) -> str: """ Returns the URL to the diff file for the given sequence id. """ - return '%s/%03i/%03i/%03i.%s' % \ + # baseurl is guaranteed to end with /, as per __init__ + return '%s%03i/%03i/%03i.%s' % \ (self.baseurl, seq / 1000000, (seq % 1000000) / 1000, seq % 1000, self.diff_type) diff --git a/test/test_replication.py b/test/test_replication.py index bc5710d..5cc4246 100644 --- a/test/test_replication.py +++ b/test/test_replication.py @@ -22,28 +22,43 @@ pytestmark = [pytest.mark.thread_unsafe, pytest.mark.iterations(1)] -@pytest.mark.parametrize("inp,outp", [ - (None, 'https://text.org/state.txt'), - (1, 'https://text.org/000/000/001.state.txt'), - (999, 'https://text.org/000/000/999.state.txt'), - (1000, 'https://text.org/000/001/000.state.txt'), - (573923, 'https://text.org/000/573/923.state.txt'), - (3290012, 'https://text.org/003/290/012.state.txt'), +@pytest.mark.parametrize("baseurl,inp,outp", [ + ("https://text.org", None, 'https://text.org/state.txt'), + ("https://text.org", 1, 'https://text.org/000/000/001.state.txt'), + ("https://text.org", 999, 'https://text.org/000/000/999.state.txt'), + ("https://text.org", 1000, 'https://text.org/000/001/000.state.txt'), + ("https://text.org", 573923, 'https://text.org/000/573/923.state.txt'), + ("https://text.org", 3290012, 'https://text.org/003/290/012.state.txt'), + + ("https://text.org/", None, 'https://text.org/state.txt'), + ("https://text.org/", 1, 'https://text.org/000/000/001.state.txt'), + ("https://text.org//", None, 'https://text.org/state.txt'), + ("https://text.org//", 1, 'https://text.org/000/000/001.state.txt'), + + ("https://text.org/area-updates", None, 'https://text.org/area-updates/state.txt'), + ("https://text.org/area-updates", 3290012, 'https://text.org/area-updates/003/290/012.state.txt'), + ("https://text.org/area-updates/", None, 'https://text.org/area-updates/state.txt'), + ("https://text.org/area-updates/", 3290012, 'https://text.org/area-updates/003/290/012.state.txt'), + ("https://text.org/area-updates//", None, 'https://text.org/area-updates/state.txt'), + ("https://text.org/area-updates//", 3290012, 'https://text.org/area-updates/003/290/012.state.txt'), + ("https://text.org/area-updates///", 3290012, 'https://text.org/area-updates/003/290/012.state.txt'), ]) -def test_get_state_url(inp, outp): - svr = rserv.ReplicationServer("https://text.org") +def test_get_state_url(baseurl, inp, outp): + svr = rserv.ReplicationServer(baseurl) assert outp == svr.get_state_url(inp) -@pytest.mark.parametrize("inp,outp", [ - (1, 'https://who.is/me//000/000/001.osc.gz'), - (500, 'https://who.is/me//000/000/500.osc.gz'), - (83750, 'https://who.is/me//000/083/750.osc.gz'), - (999999999, 'https://who.is/me//999/999/999.osc.gz'), +@pytest.mark.parametrize("baseurl,inp,outp", [ + ("https://who.is/me/", 1, 'https://who.is/me//sdfsdf000/000/001.osc.gz'), + ("https://who.is/me/", 500, 'https://who.is/me/000/000/500.osc.gz'), + ("https://who.is/me/", 83750, 'https://who.is/me/000/083/750.osc.gz'), + ("https://who.is/me/", 999999999, 'https://who.is/me/999/999/999.osc.gz'), + + ("https://who.is/me", 999999999, 'https://who.is/me/999/999/999.osc.gz'), ]) -def test_get_diff_url(inp, outp): - svr = rserv.ReplicationServer("https://who.is/me/") +def test_get_diff_url(baseurl, inp, outp): + svr = rserv.ReplicationServer(baseurl) assert outp, svr.get_diff_url(inp)