diff --git a/duniterpy/api/endpoint.py b/duniterpy/api/endpoint.py index 45e9881477b7cf3f90ecfa958e0deec2ffc89f1e..79ceaae075f5ca2b732773f4e169b78081d35180 100644 --- a/duniterpy/api/endpoint.py +++ b/duniterpy/api/endpoint.py @@ -42,7 +42,7 @@ class Endpoint: def inline(self) -> str: raise NotImplementedError("inline() is not implemented") - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: raise NotImplementedError("conn_handler is not implemented") def __str__(self) -> str: @@ -85,7 +85,7 @@ class UnknownEndpoint(Endpoint): doc += " {0}".format(p) return doc - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: return ConnectionHandler("", "", "", 0, "") def __str__(self) -> str: @@ -166,8 +166,7 @@ class BMAEndpoint(Endpoint): IPv6=(" {0}".format(self.ipv6) if self.ipv6 else ""), PORT=(" {0}".format(self.port) if self.port else "")) - # fixme: session must be mandatory - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: """ Return connection handler instance for the endpoint @@ -247,8 +246,7 @@ class SecuredBMAEndpoint(BMAEndpoint): inlined = [str(info) for info in (self.server, self.ipv4, self.ipv6, self.port, self.path) if info] return SecuredBMAEndpoint.API + " " + " ".join(inlined) - # fixme: session must be mandatory - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: """ Return connection handler instance for the endpoint @@ -297,7 +295,7 @@ class WS2PEndpoint(Endpoint): inlined = [str(info) for info in (self.ws2pid, self.server, self.port, self.path) if info] return WS2PEndpoint.API + " " + " ".join(inlined) - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: """ Return connection handler instance for the endpoint @@ -345,7 +343,7 @@ class ESUserEndpoint(Endpoint): inlined = [str(info) for info in (self.server, self.port) if info] return ESUserEndpoint.API + " " + " ".join(inlined) - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: """ Return connection handler instance for the endpoint @@ -392,7 +390,7 @@ class ESSubscribtionEndpoint(Endpoint): inlined = [str(info) for info in (self.server, self.port) if info] return ESSubscribtionEndpoint.API + " " + " ".join(inlined) - def conn_handler(self, session: aiohttp.ClientSession = None, proxy: str = None) -> ConnectionHandler: + def conn_handler(self, session: aiohttp.ClientSession, proxy: str = None) -> ConnectionHandler: """ Return connection handler instance for the endpoint diff --git a/tests/api/bma/test_bma.py b/tests/api/bma/test_bma.py index f013f536cafac43ff6cdc3cbc501fe9489eef8e2..b6abe42a8e25c23ca136a289f31bb7f3c2dd6b8a 100644 --- a/tests/api/bma/test_bma.py +++ b/tests/api/bma/test_bma.py @@ -1,26 +1,53 @@ +import asyncio import unittest +import aiohttp + from duniterpy.api.client import API, parse_error from duniterpy.api.endpoint import BMAEndpoint class TestBmaApi(unittest.TestCase): + def setUp(self): + self.loop = asyncio.new_event_loop() + asyncio.set_event_loop(self.loop) + + def tearDown(self): + try: + self.loop.stop() + self.loop.close() + finally: + asyncio.set_event_loop(None) + def test_reverse_url_complete(self): - endpoint = BMAEndpoint("test.com", "124.2.2.1", "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ", 9092) - api = API(endpoint.conn_handler(), "any") - self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/any/test/url") + async def go(): + endpoint = BMAEndpoint("test.com", "124.2.2.1", "2001:0db8:0000:85a3:0000:0000:ac1f:8001 ", 9092) + session = aiohttp.ClientSession() + api = API(endpoint.conn_handler(session), "any") + self.assertEqual(api.reverse_url("http", "/test/url"), "http://test.com:9092/any/test/url") + await session.close() + self.loop.run_until_complete(go()) def test_reverse_url_only_ipv4(self): - endpoint = BMAEndpoint("", "124.2.2.1", "", 9092) - api = API(endpoint.conn_handler(), "any") - self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/any/test/url") + async def go(): + endpoint = BMAEndpoint("", "124.2.2.1", "", 9092) + session = aiohttp.ClientSession() + + api = API(endpoint.conn_handler(session), "any") + self.assertEqual(api.reverse_url("http", "/test/url"), "http://124.2.2.1:9092/any/test/url") + await session.close() + self.loop.run_until_complete(go()) def test_reverse_url_only_ipv6(self): - endpoint = BMAEndpoint("", "", "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092) - api = API(endpoint.conn_handler(), "any") - self.assertEqual(api.reverse_url("http", "/test/url"), - "http://[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:9092/any/test/url") + async def go(): + endpoint = BMAEndpoint("", "", "2001:0db8:0000:85a3:0000:0000:ac1f:8001", 9092) + session = aiohttp.ClientSession() + api = API(endpoint.conn_handler(session), "any") + self.assertEqual(api.reverse_url("http", "/test/url"), + "http://[2001:0db8:0000:85a3:0000:0000:ac1f:8001]:9092/any/test/url") + await session.close() + self.loop.run_until_complete(go()) def test_parse_error(self): error = parse_error("""{