Commit e6b1dcea authored by Vincent Texier's avatar Vincent Texier

issue #52 add type hinting to tests.api.webserver module

parent a8d394c3
import asyncio
import socket
import ssl
from typing import Tuple, Callable, Optional
from aiohttp import web
......@@ -7,13 +10,13 @@ from aiohttp import web
# Here is a nice mocking server
class WebFunctionalSetupMixin:
def setUp(self):
def setUp(self) -> None:
self.handler = None
self.runner = None
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(self.loop)
def tearDown(self):
def tearDown(self) -> None:
if self.handler:
self.loop.run_until_complete(self.handler.shutdown())
if self.runner:
......@@ -24,15 +27,24 @@ class WebFunctionalSetupMixin:
finally:
asyncio.set_event_loop(None)
def find_unused_port(self):
def find_unused_port(self) -> int:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 0))
port = s.getsockname()[1]
s.close()
return port
async def create_server(self, method, path, handler=None, ssl_ctx=None):
async def create_server(self, method: str, path: str, handler: Optional[Callable] = None,
ssl_ctx: Optional[ssl.SSLContext] = None) -> Tuple[web.Application, int, str]:
"""
Create a web server for tests
:param method: HTTP method type
:param path: Url path
:param handler: Callback function
:param ssl_ctx: SSL context (https is used if not None)
:return:
"""
app = web.Application()
if handler:
app.router.add_route(method, path, handler)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment