csernazs/pytest-httpserver

Recommendations for running both http (non-s) and https test within the same session

Spitfire1900 opened this issue · 2 comments

When defining the following fixtures within a test module all tests that use httpserver will also use https, even if the test is intended to use http (non-S). What is the recommended way to both http (non-s) and https test within the same test session?

@pytest.fixture(scope="session")
def create_ca():
    return trustme.CA()


@pytest.fixture(scope="session")
def httpserver_ssl_context(create_ca: trustme.CA):
    context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
    localhost_cert = create_ca.issue_cert("localhost")
    localhost_cert.configure_cert(context)
    return context

Hi there,

Sorry for the late answer.

You can create as many http servers as you want for your test.
I would recommend using the https as you wrote in the issue, and then creating a http one by copying the logic from the make_httpserver function here and making your own fixture.

So you would have two fixtures at the end, one for http, other for https.

You would need to create handlers for both, probably a redirect for the http.

Thanks for getting back, I had answered this myself awhile ago but never posted an update.

In my case I did the following to create a parameterized version of a test that evaluated HTTP and HTTPS permutation.

@pytest.fixture(scope="session")
def cert_authority() -> trustme.CA:
    return trustme.CA()


@pytest.fixture(scope="session", params=['http', 'https'])
def make_httpserver(cert_authority: trustme.CA, request: pytest.FixtureRequest):  # pylint: disable=redefined-outer-name
    ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) if request.param == 'https' else None
    if ssl_context:
        localhost_cert = cert_authority.issue_cert("localhost")
        localhost_cert.configure_cert(ssl_context)
    http_server = HTTPServer(ssl_context=ssl_context)
    http_server.start()
    yield http_server
    http_server.clear()
    if http_server.is_running():
        http_server.stop()


def test_url(resource_path_root: Path, httpserver: HTTPServer, cert_authority: trustme.CA):  # pylint: disable=redefined-outer-name
    <test_impl....>