Skip to content

Commit

Permalink
moved ssl tests to test_runserver_main.py
Browse files Browse the repository at this point in the history
  • Loading branch information
zrvku2000 committed Feb 1, 2025
1 parent 0aad504 commit 7addb68
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 107 deletions.
88 changes: 88 additions & 0 deletions tests/test_runserver_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from aiohttp_devtools.runserver.serve import (
WS, create_auxiliary_app, create_main_app, modify_main_app, src_reload, start_main_app)
from aiohttp_devtools.runserver.watch import AppTask
import ssl

from .conftest import SIMPLE_APP, forked

Expand Down Expand Up @@ -308,3 +309,90 @@ async def test_websocket_reload(aux_cli):
assert reloads == 1
finally:
await ws.close()


async def check_ssl_server_running(check_callback, sslcontext):
port_open = False
async with aiohttp.ClientSession(timeout=ClientTimeout(total=1)) as session:
for i in range(50): # pragma: no branch
try:
async with session.get('https://localhost:8443/', ssl=sslcontext):
pass
except OSError:
await asyncio.sleep(0.1)
else:
port_open = True
break
assert port_open
await check_callback(session, sslcontext)
await asyncio.sleep(.25) # TODO(aiohttp 4): Remove this hack


@pytest.mark.filterwarnings(r"ignore:unclosed:ResourceWarning")
@pytest.mark.datafiles('tests/test_certs', keep_top_dir=True)
def test_start_runserver_ssl(datafiles, tmpworkdir, smart_caplog):
mktree(tmpworkdir, {
'app.py': """\
from aiohttp import web
import ssl
async def hello(request):
return web.Response(text='<h1>hello world</h1>', content_type='text/html')
async def has_error(request):
raise ValueError()
def create_app():
app = web.Application()
app.router.add_get('/', hello)
app.router.add_get('/error', has_error)
return app
def get_ssl_context():
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain('test_certs/server.crt', 'test_certs/server.key')
return ssl_context
""",
'static_dir/foo.js': 'var bar=1;',
})
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
args = runserver(app_path="app.py", static_path="static_dir",
bind_address="0.0.0.0", ssl_context_factory_name='get_ssl_context')
aux_app = args["app"]
aux_port = args["port"]
runapp_host = args["host"]
assert isinstance(aux_app, aiohttp.web.Application)
assert aux_port == 8001
assert runapp_host == "0.0.0.0"
for startup in aux_app.on_startup:
loop.run_until_complete(startup(aux_app))

ssl_context = ssl.create_default_context()
ssl_context.load_verify_locations('test_certs/rootCA.pem')

async def check_callback(session, ssl_context):
print(session, ssl_context)
async with session.get('https://localhost:8443/', ssl=ssl_context) as r:
assert r.status == 200
assert r.headers['content-type'].startswith('text/html')
text = await r.text()
print(text)
assert '<h1>hello world</h1>' in text
assert '<script src="http://localhost:8001/livereload.js"></script>' in text

async with session.get('https://localhost:8443/error', ssl=ssl_context) as r:
assert r.status == 500
assert 'raise ValueError()' in (await r.text())

try:
loop.run_until_complete(check_ssl_server_running(check_callback, ssl_context))
finally:
for shutdown in aux_app.on_shutdown:
loop.run_until_complete(shutdown(aux_app))
loop.run_until_complete(aux_app.cleanup())
assert (
'adev.server.dft INFO: Starting aux server at http://localhost:8001 ◆\n'
'adev.server.dft INFO: serving static files from ./static_dir/ at http://localhost:8001/static/\n'
'adev.server.dft INFO: Starting dev server at https://localhost:8443 ●\n'
) in smart_caplog
loop.run_until_complete(asyncio.sleep(.25)) # TODO(aiohttp 4): Remove this hack
107 changes: 0 additions & 107 deletions tests/test_runserver_with_ssl.py

This file was deleted.

0 comments on commit 7addb68

Please sign in to comment.