sanic/tests/test_logo.py

98 lines
2.4 KiB
Python
Raw Normal View History

2018-12-30 11:18:06 +00:00
import asyncio
import logging
2018-12-30 11:18:06 +00:00
from sanic.config import BASE_LOGO
from sanic.testing import PORT
2018-12-30 11:18:06 +00:00
try:
import uvloop # noqa
ROW = 0
except BaseException:
ROW = 1
2018-12-30 11:18:06 +00:00
2018-12-30 11:46:08 +00:00
def test_logo_base(app, caplog):
server = app.create_server(
debug=True, return_asyncio_server=True, port=PORT
)
2018-12-30 11:46:08 +00:00
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop._stopping = False
2018-12-30 11:18:06 +00:00
with caplog.at_level(logging.DEBUG):
2018-12-30 11:46:08 +00:00
_server = loop.run_until_complete(server)
_server.close()
loop.run_until_complete(_server.wait_closed())
app.stop()
2018-12-30 11:18:06 +00:00
assert caplog.record_tuples[ROW][1] == logging.DEBUG
assert caplog.record_tuples[ROW][2] == BASE_LOGO
2018-12-30 11:18:06 +00:00
2018-12-30 11:46:08 +00:00
def test_logo_false(app, caplog):
2018-12-30 11:18:06 +00:00
app.config.LOGO = False
2018-12-30 11:46:08 +00:00
server = app.create_server(
debug=True, return_asyncio_server=True, port=PORT
)
2018-12-30 11:46:08 +00:00
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop._stopping = False
2018-12-30 11:18:06 +00:00
with caplog.at_level(logging.DEBUG):
2018-12-30 11:46:08 +00:00
_server = loop.run_until_complete(server)
_server.close()
loop.run_until_complete(_server.wait_closed())
app.stop()
2018-12-30 11:18:06 +00:00
Fix Ctrl+C and tests on Windows. (#1808) * Fix Ctrl+C on Windows. * Disable testing of a function N/A on Windows. * Add test for coverage, avoid crash on missing _stopping. * Initialise StreamingHTTPResponse.protocol = None * Improved comments. * Reduce amount of data in test_request_stream to avoid failures on Windows. * The Windows test doesn't work on Windows :( * Use port numbers more likely to be free than 8000. * Disable the other signal tests on Windows as well. * Windows doesn't properly support SO_REUSEADDR, so that's disabled in Python, and thus rebinding fails. For successful testing, reuse port instead. * app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests * Revert "app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests" This reverts commit dc5d682448e3f6595bdca5cb764e5f26ca29e295. * Use random test server port on most tests. Should avoid port/addr reuse issues. * Another test to random port instead of 8000. * Fix deprecation warnings about missing name on Sanic() in tests. * Linter and typing * Increase test coverage * Rewrite test for ctrlc_windows_workaround * py36 compat * py36 compat * py36 compat * Don't rely on loop internals but add a stopping flag to app. * App may be restarted. * py36 compat * Linter * Add a constant for OS checking. Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
2020-03-26 04:42:46 +00:00
banner, port = caplog.record_tuples[ROW][2].rsplit(":", 1)
assert caplog.record_tuples[ROW][1] == logging.INFO
Fix Ctrl+C and tests on Windows. (#1808) * Fix Ctrl+C on Windows. * Disable testing of a function N/A on Windows. * Add test for coverage, avoid crash on missing _stopping. * Initialise StreamingHTTPResponse.protocol = None * Improved comments. * Reduce amount of data in test_request_stream to avoid failures on Windows. * The Windows test doesn't work on Windows :( * Use port numbers more likely to be free than 8000. * Disable the other signal tests on Windows as well. * Windows doesn't properly support SO_REUSEADDR, so that's disabled in Python, and thus rebinding fails. For successful testing, reuse port instead. * app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests * Revert "app.run argument handling: added server kwargs (alike create_server), added warning on extra kwargs, made auto_reload explicit argument. Another go at Windows tests" This reverts commit dc5d682448e3f6595bdca5cb764e5f26ca29e295. * Use random test server port on most tests. Should avoid port/addr reuse issues. * Another test to random port instead of 8000. * Fix deprecation warnings about missing name on Sanic() in tests. * Linter and typing * Increase test coverage * Rewrite test for ctrlc_windows_workaround * py36 compat * py36 compat * py36 compat * Don't rely on loop internals but add a stopping flag to app. * App may be restarted. * py36 compat * Linter * Add a constant for OS checking. Co-authored-by: L. Kärkkäinen <tronic@users.noreply.github.com>
2020-03-26 04:42:46 +00:00
assert banner == "Goin' Fast @ http://127.0.0.1"
assert int(port) > 0
2018-12-30 11:18:06 +00:00
2018-12-30 11:46:08 +00:00
def test_logo_true(app, caplog):
2018-12-30 11:18:06 +00:00
app.config.LOGO = True
2018-12-30 11:46:08 +00:00
server = app.create_server(
debug=True, return_asyncio_server=True, port=PORT
)
2018-12-30 11:46:08 +00:00
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop._stopping = False
2018-12-30 11:18:06 +00:00
with caplog.at_level(logging.DEBUG):
2018-12-30 11:46:08 +00:00
_server = loop.run_until_complete(server)
_server.close()
loop.run_until_complete(_server.wait_closed())
app.stop()
2018-12-30 11:18:06 +00:00
assert caplog.record_tuples[ROW][1] == logging.DEBUG
assert caplog.record_tuples[ROW][2] == BASE_LOGO
2018-12-30 11:18:06 +00:00
2018-12-30 11:46:08 +00:00
def test_logo_custom(app, caplog):
2018-12-30 11:18:06 +00:00
app.config.LOGO = "My Custom Logo"
2018-12-30 11:46:08 +00:00
server = app.create_server(
debug=True, return_asyncio_server=True, port=PORT
)
2018-12-30 11:46:08 +00:00
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop._stopping = False
2018-12-30 11:18:06 +00:00
with caplog.at_level(logging.DEBUG):
2018-12-30 11:46:08 +00:00
_server = loop.run_until_complete(server)
_server.close()
loop.run_until_complete(_server.wait_closed())
app.stop()
2018-12-30 11:18:06 +00:00
assert caplog.record_tuples[ROW][1] == logging.DEBUG
assert caplog.record_tuples[ROW][2] == "My Custom Logo"