|
2 | 2 | import json
|
3 | 3 | import logging
|
4 | 4 | import os
|
| 5 | +import pathlib |
| 6 | +import ssl |
5 | 7 | import types
|
6 | 8 |
|
7 | 9 | import pytest
|
@@ -79,12 +81,35 @@ class TestServer:
|
79 | 81 | Will allow us to test our client by simulating different correct and incorrect server responses
|
80 | 82 | """
|
81 | 83 |
|
| 84 | + def __init__(self, with_ssl: bool = False): |
| 85 | + self.with_ssl = with_ssl |
| 86 | + |
82 | 87 | async def start(self, handler):
|
83 | 88 |
|
84 | 89 | print("Starting server")
|
85 | 90 |
|
| 91 | + extra_serve_args = {} |
| 92 | + |
| 93 | + if self.with_ssl: |
| 94 | + # This is a copy of certificate from websockets tests folder |
| 95 | + # |
| 96 | + # Generate TLS certificate with: |
| 97 | + # $ openssl req -x509 -config test_localhost.cnf -days 15340 -newkey rsa:2048 \ |
| 98 | + # -out test_localhost.crt -keyout test_localhost.key |
| 99 | + # $ cat test_localhost.key test_localhost.crt > test_localhost.pem |
| 100 | + # $ rm test_localhost.key test_localhost.crt |
| 101 | + self.testcert = bytes( |
| 102 | + pathlib.Path(__file__).with_name("test_localhost.pem") |
| 103 | + ) |
| 104 | + ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER) |
| 105 | + ssl_context.load_cert_chain(self.testcert) |
| 106 | + |
| 107 | + extra_serve_args["ssl"] = ssl_context |
| 108 | + |
86 | 109 | # Start a server with a random open port
|
87 |
| - self.start_server = websockets.server.serve(handler, "localhost", 0) |
| 110 | + self.start_server = websockets.server.serve( |
| 111 | + handler, "localhost", 0, **extra_serve_args |
| 112 | + ) |
88 | 113 |
|
89 | 114 | # Wait that the server is started
|
90 | 115 | self.server = await self.start_server
|
@@ -137,13 +162,9 @@ async def wait_connection_terminate(ws):
|
137 | 162 | assert json_result["type"] == "connection_terminate"
|
138 | 163 |
|
139 | 164 |
|
140 |
| -@pytest.fixture |
141 |
| -async def server(request): |
142 |
| - """server is a fixture used to start a dummy server to test the client behaviour. |
143 |
| -
|
144 |
| - It can take as argument either a handler function for the websocket server for complete control |
145 |
| - OR an array of answers to be sent by the default server handler |
146 |
| - """ |
| 165 | +def get_server_handler(request): |
| 166 | + """ Get the server handler provided from test or use the default |
| 167 | + server handler if the test provides only an array of answers""" |
147 | 168 |
|
148 | 169 | if isinstance(request.param, types.FunctionType):
|
149 | 170 | server_handler = request.param
|
@@ -179,6 +200,42 @@ async def default_server_handler(ws, path):
|
179 | 200 |
|
180 | 201 | server_handler = default_server_handler
|
181 | 202 |
|
| 203 | + return server_handler |
| 204 | + |
| 205 | + |
| 206 | +@pytest.fixture |
| 207 | +async def ws_ssl_server(request): |
| 208 | + """websockets server fixture using ssl |
| 209 | +
|
| 210 | + It can take as argument either a handler function for the websocket server for complete control |
| 211 | + OR an array of answers to be sent by the default server handler |
| 212 | + """ |
| 213 | + |
| 214 | + server_handler = get_server_handler(request) |
| 215 | + |
| 216 | + try: |
| 217 | + test_server = TestServer(with_ssl=True) |
| 218 | + |
| 219 | + # Starting the server with the fixture param as the handler function |
| 220 | + await test_server.start(server_handler) |
| 221 | + |
| 222 | + yield test_server |
| 223 | + except Exception as e: |
| 224 | + print("Exception received in server fixture: " + str(e)) |
| 225 | + finally: |
| 226 | + await test_server.stop() |
| 227 | + |
| 228 | + |
| 229 | +@pytest.fixture |
| 230 | +async def server(request): |
| 231 | + """server is a fixture used to start a dummy server to test the client behaviour. |
| 232 | +
|
| 233 | + It can take as argument either a handler function for the websocket server for complete control |
| 234 | + OR an array of answers to be sent by the default server handler |
| 235 | + """ |
| 236 | + |
| 237 | + server_handler = get_server_handler(request) |
| 238 | + |
182 | 239 | try:
|
183 | 240 | test_server = TestServer()
|
184 | 241 |
|
|
0 commit comments