Skip to content

Commit

Permalink
add test for ssl init
Browse files Browse the repository at this point in the history
  • Loading branch information
tumb1er committed Jul 25, 2016
1 parent 037a385 commit 0f0b392
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
9 changes: 4 additions & 5 deletions aiohttp/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ class GunicornWebWorker(base.Worker):

def __init__(self, *args, **kw): # pragma: no cover
super().__init__(*args, **kw)
if self.cfg.is_ssl:
self.ssl_context = self._create_ssl_context(self.cfg)
else:
self.ssl_context = None

self.servers = {}
self.exit_code = 0
Expand Down Expand Up @@ -85,10 +81,13 @@ def close(self):

@asyncio.coroutine
def _run(self):

ctx = self._create_ssl_context(self.cfg) if self.cfg.is_ssl else None

for sock in self.sockets:
handler = self.make_handler(self.wsgi)
srv = yield from self.loop.create_server(handler, sock=sock.sock,
ssl=self.ssl_context)
ssl=ctx)
self.servers[srv] = handler

# If our parent changed then we shut down.
Expand Down
17 changes: 13 additions & 4 deletions tests/test_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,23 @@ def test__run_ok(worker, loop):
ret.set_result(sock)
worker.wsgi.make_handler.return_value.num_connections = 1
worker.cfg.max_requests = 100
worker.cfg.is_ssl = True

with mock.patch('aiohttp.worker.asyncio') as m_asyncio:
m_asyncio.sleep = mock.Mock(
wraps=asyncio.coroutine(lambda *a, **kw: None))
loop.run_until_complete(worker._run())
ssl_context = mock.Mock()
with mock.patch('ssl.SSLContext', return_value=ssl_context):
with mock.patch('aiohttp.worker.asyncio') as m_asyncio:
m_asyncio.sleep = mock.Mock(
wraps=asyncio.coroutine(lambda *a, **kw: None))
loop.run_until_complete(worker._run())

assert worker.notify.called
assert worker.log.info.called

args, kwargs = loop.create_server.call_args
assert 'ssl' in kwargs
ctx = kwargs['ssl']
assert ctx is ssl_context


def test__run_exc(worker, loop):
with mock.patch('aiohttp.worker.os') as m_os:
Expand All @@ -138,6 +146,7 @@ def test__run_exc(worker, loop):
worker.log = mock.Mock()
worker.loop = mock.Mock()
worker.notify = mock.Mock()
worker.cfg.is_ssl = False

with mock.patch('aiohttp.worker.asyncio.sleep') as m_sleep:
slp = helpers.create_future(loop)
Expand Down

0 comments on commit 0f0b392

Please sign in to comment.