Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config tests #399

Merged
merged 8 commits into from
Sep 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,13 @@


@pytest.fixture(scope="function")
def certfile_and_keyfile():
def certfile_and_keyfile(tmp_path):
certfile = str(tmp_path / "cert.pem")
with open(certfile, "bw") as fout:
fout.write(CERTIFICATE)

certfile = tempfile.NamedTemporaryFile(suffix=".pem")
certfile.write(CERTIFICATE)
certfile.seek(0)
keyfile = str(tmp_path / "key.pem")
with open(keyfile, "bw") as fout:
fout.write(PRIVATE_KEY)

keyfile = tempfile.NamedTemporaryFile(suffix=".pem")
keyfile.write(PRIVATE_KEY)
keyfile.seek(0)

yield certfile, keyfile

certfile.close()
keyfile.close()
return certfile, keyfile
2 changes: 1 addition & 1 deletion tests/supervisors/test_statreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def mock_signal(handle_exit):

def test_statreload(certfile_and_keyfile):
certfile, keyfile = certfile_and_keyfile
config = Config(app=None, ssl_certfile=certfile.name, ssl_keyfile=keyfile.name)
config = Config(app=None, ssl_certfile=certfile, ssl_keyfile=keyfile)

server = Server(config)
type(server).run = lambda self: None
Expand Down
75 changes: 75 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import logging
import socket

import pytest

from uvicorn import protocols
from uvicorn.config import Config
from uvicorn.middleware.debug import DebugMiddleware
from uvicorn.middleware.wsgi import WSGIMiddleware


async def asgi_app():
pass


def wsgi_app():
pass


def test_debug_app():
config = Config(app=asgi_app, debug=True)
config.load()

assert config.debug is True
assert isinstance(config.loaded_app, DebugMiddleware)


def test_wsgi_app():
config = Config(app=wsgi_app, interface="wsgi")
config.load()

assert isinstance(config.loaded_app, WSGIMiddleware)
assert config.interface == "wsgi"


def test_proxy_headers():
config = Config(app=asgi_app, proxy_headers=True)
config.load()

assert config.proxy_headers is True


def test_app_unimportable():
config = Config(app="no.such:app")
with pytest.raises(ImportError):
config.load()


def test_concrete_http_class():
config = Config(app=asgi_app, http=protocols.http.h11_impl.H11Protocol)
config.load()
assert config.http_protocol_class is protocols.http.h11_impl.H11Protocol


def test_logger():
logger = logging.getLogger("just-for-tests")
config = Config(app=asgi_app, logger=logger)
config.load()

assert config.logger is logger


def test_socket_bind():
config = Config(app=asgi_app)
config.load()

assert isinstance(config.bind_socket(), socket.socket)


def test_ssl_config(certfile_and_keyfile):
certfile, keyfile = certfile_and_keyfile
config = Config(app=asgi_app, ssl_certfile=certfile, ssl_keyfile=keyfile)
config.load()

assert config.is_ssl is True
4 changes: 2 additions & 2 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def install_signal_handlers(self):
app=App,
loop="asyncio",
limit_max_requests=1,
ssl_keyfile=keyfile.name,
ssl_certfile=certfile.name,
ssl_keyfile=keyfile,
ssl_certfile=certfile,
)
server = CustomServer(config=config)
thread = threading.Thread(target=server.run)
Expand Down
2 changes: 1 addition & 1 deletion uvicorn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def __init__(

@property
def is_ssl(self) -> bool:
return self.ssl_keyfile or self.ssl_certfile
return bool(self.ssl_keyfile or self.ssl_certfile)

def load(self):
assert not self.loaded
Expand Down