Skip to content

Commit d44f0e5

Browse files
committed
Factorize test certificates serialization
1 parent 84abc7f commit d44f0e5

1 file changed

Lines changed: 14 additions & 54 deletions

File tree

test/conftest.py

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,22 @@ def certs_dir(tmp_path_factory):
5454

5555

5656
@contextlib.contextmanager
57-
def run_server_in_thread(scheme, host, ca_certs, server_certs):
57+
def run_server_in_thread(scheme, host, tmpdir, ca, server_cert):
58+
ca_cert_path = str(tmpdir / "ca.pem")
59+
server_cert_path = str(tmpdir / "server.pem")
60+
server_key_path = str(tmpdir / "server.key")
61+
ca.cert_pem.write_to_path(ca_cert_path)
62+
server_cert.private_key_pem.write_to_path(server_key_path)
63+
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
64+
server_certs = {"keyfile": server_key_path, "certfile": server_cert_path}
65+
5866
io_loop = ioloop.IOLoop.current()
5967
app = web.Application([(r".*", TestingApp)])
6068
server, port = run_tornado_app(app, io_loop, server_certs, scheme, host)
6169
server_thread = threading.Thread(target=io_loop.start)
6270
server_thread.start()
6371

64-
yield ServerConfig(host, port, ca_certs)
72+
yield ServerConfig(host, port, ca_cert_path)
6573

6674
io_loop.add_callback(server.stop)
6775
io_loop.add_callback(io_loop.stop)
@@ -75,19 +83,7 @@ def no_san_server(tmp_path_factory):
7583
# only common name, no subject alternative names
7684
server_cert = ca.issue_cert(common_name=u"localhost")
7785

78-
ca_cert_path = str(tmpdir / "ca.pem")
79-
server_cert_path = str(tmpdir / "server.pem")
80-
server_key_path = str(tmpdir / "server.key")
81-
ca.cert_pem.write_to_path(ca_cert_path)
82-
server_cert.private_key_pem.write_to_path(server_key_path)
83-
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
84-
85-
with run_server_in_thread(
86-
"https",
87-
"localhost",
88-
ca_cert_path,
89-
{"keyfile": server_key_path, "certfile": server_cert_path},
90-
) as cfg:
86+
with run_server_in_thread("https", "localhost", tmpdir, ca, server_cert) as cfg:
9187
yield cfg
9288

9389

@@ -98,19 +94,7 @@ def ip_san_server(tmp_path_factory):
9894
# IP address in Subject Alternative Name
9995
server_cert = ca.issue_cert(u"127.0.0.1")
10096

101-
ca_cert_path = str(tmpdir / "ca.pem")
102-
server_cert_path = str(tmpdir / "server.pem")
103-
server_key_path = str(tmpdir / "server.key")
104-
ca.cert_pem.write_to_path(ca_cert_path)
105-
server_cert.private_key_pem.write_to_path(server_key_path)
106-
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
107-
108-
with run_server_in_thread(
109-
"https",
110-
"127.0.0.1",
111-
ca_cert_path,
112-
{"keyfile": server_key_path, "certfile": server_cert_path},
113-
) as cfg:
97+
with run_server_in_thread("https", "127.0.0.1", tmpdir, ca, server_cert) as cfg:
11498
yield cfg
11599

116100

@@ -121,19 +105,7 @@ def ipv6_addr_server(tmp_path_factory):
121105
# IP address in Common Name
122106
server_cert = ca.issue_cert(common_name=u"::1")
123107

124-
ca_cert_path = str(tmpdir / "ca.pem")
125-
server_cert_path = str(tmpdir / "server.pem")
126-
server_key_path = str(tmpdir / "server.key")
127-
ca.cert_pem.write_to_path(ca_cert_path)
128-
server_cert.private_key_pem.write_to_path(server_key_path)
129-
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
130-
131-
with run_server_in_thread(
132-
"https",
133-
"::1",
134-
ca_cert_path,
135-
{"keyfile": server_key_path, "certfile": server_cert_path},
136-
) as cfg:
108+
with run_server_in_thread("https", "::1", tmpdir, ca, server_cert) as cfg:
137109
yield cfg
138110

139111

@@ -144,17 +116,5 @@ def ipv6_san_server(tmp_path_factory):
144116
# IP address in Subject Alternative Name
145117
server_cert = ca.issue_cert(u"::1")
146118

147-
ca_cert_path = str(tmpdir / "ca.pem")
148-
server_cert_path = str(tmpdir / "server.pem")
149-
server_key_path = str(tmpdir / "server.key")
150-
ca.cert_pem.write_to_path(ca_cert_path)
151-
server_cert.private_key_pem.write_to_path(server_key_path)
152-
server_cert.cert_chain_pems[0].write_to_path(server_cert_path)
153-
154-
with run_server_in_thread(
155-
"https",
156-
"::1",
157-
ca_cert_path,
158-
{"keyfile": server_key_path, "certfile": server_cert_path},
159-
) as cfg:
119+
with run_server_in_thread("https", "::1", tmpdir, ca, server_cert) as cfg:
160120
yield cfg

0 commit comments

Comments
 (0)