From 594fed924a52182a91c83d9178ecdb4e8dae2d81 Mon Sep 17 00:00:00 2001
From: Vincent Texier <vit@free.fr>
Date: Sun, 22 Nov 2020 13:32:11 +0100
Subject: [PATCH] [fix] fix bug in API.reverse_url

Cover with tests
---
 duniterpy/api/client.py   |  7 +++++--
 tests/api/bma/test_bma.py | 38 +++++++++++++++++++++++++++++++++++++-
 2 files changed, 42 insertions(+), 3 deletions(-)

diff --git a/duniterpy/api/client.py b/duniterpy/api/client.py
index d3864f9a..072d71dd 100644
--- a/duniterpy/api/client.py
+++ b/duniterpy/api/client.py
@@ -232,11 +232,14 @@ class API:
                 api_path=self.connection_handler.path,
             )
         else:
-            url = "{scheme}://{server}:{port}/".format(
+            url = "{scheme}://{server}:{port}".format(
                 scheme=scheme, server=server, port=port
             )
 
-        return url + path
+        if len(path.strip()) > 0:
+            return f"{url}/{path}"
+
+        return url
 
     async def requests_get(self, path: str, **kwargs: Any) -> ClientResponse:
         """
diff --git a/tests/api/bma/test_bma.py b/tests/api/bma/test_bma.py
index 85e2bcbd..41f54d9a 100644
--- a/tests/api/bma/test_bma.py
+++ b/tests/api/bma/test_bma.py
@@ -21,7 +21,7 @@ import unittest
 import aiohttp
 
 from duniterpy.api.client import API, parse_error
-from duniterpy.api.endpoint import BMAEndpoint
+from duniterpy.api.endpoint import BMAEndpoint, SecuredBMAEndpoint, GVAEndpoint
 
 
 class TestBmaApi(unittest.TestCase):
@@ -53,6 +53,42 @@ class TestBmaApi(unittest.TestCase):
 
         self.loop.run_until_complete(go())
 
+    def test_reverse_url_complete_bmas(self):
+        async def go():
+            endpoint = SecuredBMAEndpoint(
+                "test.com",
+                "124.2.2.1",
+                "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ",
+                9092,
+                "api_path",
+            )
+            session = aiohttp.ClientSession()
+            api = API(endpoint.conn_handler(session))
+            self.assertEqual(
+                api.reverse_url("http", "/test/url"),
+                "http://test.com:9092/api_path/test/url",
+            )
+            await session.close()
+
+        self.loop.run_until_complete(go())
+
+    def test_reverse_url_complete_gva(self):
+        async def go():
+            endpoint = GVAEndpoint(
+                "S",
+                "test.com",
+                "124.2.2.1",
+                "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ",
+                9092,
+                "gva",
+            )
+            session = aiohttp.ClientSession()
+            api = API(endpoint.conn_handler(session))
+            self.assertEqual(api.reverse_url("https", ""), "https://test.com:9092/gva")
+            await session.close()
+
+        self.loop.run_until_complete(go())
+
     def test_reverse_url_only_ipv4(self):
         async def go():
             endpoint = BMAEndpoint("", "124.2.2.1", "", 9092)
-- 
GitLab