diff --git a/src/osmium/replication/server.py b/src/osmium/replication/server.py index 37f2e64..b7eae04 100644 --- a/src/osmium/replication/server.py +++ b/src/osmium/replication/server.py @@ -483,16 +483,26 @@ def get_state_url(self, seq: Optional[int]) -> str: i.e. the state file in the root directory of the replication service. """ + # Canonicalize the baseurl + # Try to avoid calling `example.com/area-updates//state.txt` + baseurl_wo_slash = self.baseurl + while baseurl_wo_slash[-1] == "/": + baseurl_wo_slash = baseurl_wo_slash[:-1] + if seq is None: - return self.baseurl + '/state.txt' + return baseurl_wo_slash + '/state.txt' return '%s/%03i/%03i/%03i.state.txt' % \ - (self.baseurl, seq / 1000000, (seq % 1000000) / 1000, seq % 1000) + (baseurl_wo_slash, 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. """ + baseurl_wo_slash = self.baseurl + while baseurl_wo_slash[-1] == "/": + baseurl_wo_slash = baseurl_wo_slash[:-1] + return '%s/%03i/%03i/%03i.%s' % \ - (self.baseurl, + (baseurl_wo_slash, seq / 1000000, (seq % 1000000) / 1000, seq % 1000, self.diff_type) diff --git a/test/test_replication.py b/test/test_replication.py index bc5710d..a6ac31c 100644 --- a/test/test_replication.py +++ b/test/test_replication.py @@ -22,28 +22,42 @@ 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'), ]) -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)