File: test_ssl.py

package info (click to toggle)
pytest-httpserver 1.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 908 kB
  • sloc: python: 2,382; makefile: 77; sh: 21
file content (48 lines) | stat: -rw-r--r-- 1,409 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
import ssl
from os.path import join as pjoin

import pytest
import requests

from pytest_httpserver import HTTPServer

pytestmark = pytest.mark.ssl

test_dir = os.path.dirname(os.path.realpath(__file__))
assets_dir = pjoin(test_dir, "assets")


@pytest.fixture(scope="session")
def httpserver_ssl_context():
    protocol = None
    for name in ("PROTOCOL_TLS_SERVER", "PROTOCOL_TLS", "PROTOCOL_TLSv1_2"):
        if hasattr(ssl, name):
            protocol = getattr(ssl, name)
            break

    assert protocol is not None, "Unable to obtain TLS protocol"

    return ssl.SSLContext(protocol)


def test_ssl(httpserver: HTTPServer):
    server_crt = pjoin(assets_dir, "server.crt")
    server_key = pjoin(assets_dir, "server.key")
    root_ca = pjoin(assets_dir, "rootCA.crt")

    assert (
        httpserver.ssl_context is not None
    ), "SSLContext not set. The session was probably started with a test that did not define an SSLContext."

    httpserver.ssl_context.load_cert_chain(server_crt, server_key)
    httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"})

    assert httpserver.is_running()

    assert httpserver.url_for("/").startswith("https://")

    # ensure we are using "localhost" and not "127.0.0.1" to pass cert verification
    url = f"https://localhost:{httpserver.port}/foobar"

    assert requests.get(url, verify=root_ca).json() == {"foo": "bar"}