Skip to content

Commit ede83e6

Browse files
committed
httpserver: replace **kwargs with named parameters
Restore API compatibility for the parameters specified by position, and also remove the `**kwargs` from the constructors and replace it with the real parameters to make the documentation better (and to also help IDE integration). Update documentation.
1 parent b59c63a commit ede83e6

2 files changed

Lines changed: 40 additions & 10 deletions

File tree

pytest_httpserver/blocking_httpserver.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from queue import Empty
22
from queue import Queue
3+
from ssl import SSLContext
34
from typing import Any
45
from typing import Dict
56
from typing import Mapping
@@ -37,14 +38,31 @@ class BlockingHTTPServer(HTTPServerBase):
3738
"""
3839
Server instance which enables synchronous matching for incoming requests.
3940
41+
:param host: the host or IP where the server will listen
42+
:param port: the TCP port where the server will listen
43+
:param ssl_context: the ssl context object to use for https connections
44+
4045
:param timeout: waiting time in seconds for matching and responding to an incoming request.
4146
manager
4247
43-
For further parameters and attributes see :py:class:`HttpServerBase`.
48+
.. py:attribute:: no_handler_status_code
49+
50+
Attribute containing the http status code (int) which will be the response
51+
status when no matcher is found for the request. By default, it is set to *500*
52+
but it can be overridden to any valid http status code such as *404* if needed.
4453
"""
4554

46-
def __init__(self, timeout: int = 30, **kwargs):
47-
super().__init__(**kwargs)
55+
DEFAULT_LISTEN_HOST = "localhost"
56+
DEFAULT_LISTEN_PORT = 0 # Use ephemeral port
57+
58+
def __init__(
59+
self,
60+
host=DEFAULT_LISTEN_HOST,
61+
port=DEFAULT_LISTEN_PORT,
62+
ssl_context: Optional[SSLContext] = None,
63+
timeout: int = 30,
64+
):
65+
super().__init__(host, port, ssl_context)
4866
self.timeout = timeout
4967
self.request_queue: Queue[Request] = Queue()
5068
self.request_handlers: Dict[Request, Queue[BlockingRequestHandler]] = {}

pytest_httpserver/httpserver.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,10 @@ class HTTPServerBase(abc.ABC): # pylint: disable=too-many-instance-attributes
590590
591591
"""
592592

593-
DEFAULT_LISTEN_HOST = "localhost"
594-
DEFAULT_LISTEN_PORT = 0 # Use ephemeral port
595-
596593
def __init__(
597594
self,
598-
host=DEFAULT_LISTEN_HOST,
599-
port=DEFAULT_LISTEN_PORT,
595+
host: str,
596+
port: int,
600597
ssl_context: Optional[SSLContext] = None,
601598
):
602599
"""
@@ -845,19 +842,34 @@ class HTTPServer(HTTPServerBase): # pylint: disable=too-many-instance-attribute
845842
"""
846843
Server instance which manages handlers to serve pre-defined requests.
847844
845+
:param host: the host or IP where the server will listen
846+
:param port: the TCP port where the server will listen
847+
:param ssl_context: the ssl context object to use for https connections
848+
848849
:param default_waiting_settings: the waiting settings object to use as default settings for :py:meth:`wait` context
849850
manager
851+
852+
.. py:attribute:: no_handler_status_code
853+
854+
Attribute containing the http status code (int) which will be the response
855+
status when no matcher is found for the request. By default, it is set to *500*
856+
but it can be overridden to any valid http status code such as *404* if needed.
850857
"""
851858

859+
DEFAULT_LISTEN_HOST = "localhost"
860+
DEFAULT_LISTEN_PORT = 0 # Use ephemeral port
861+
852862
def __init__(
853863
self,
864+
host=DEFAULT_LISTEN_HOST,
865+
port=DEFAULT_LISTEN_PORT,
866+
ssl_context: Optional[SSLContext] = None,
854867
default_waiting_settings: Optional[WaitingSettings] = None,
855-
**kwargs,
856868
):
857869
"""
858870
Initializes the instance.
859871
"""
860-
super().__init__(**kwargs)
872+
super().__init__(host, port, ssl_context)
861873

862874
self.ordered_handlers: List[RequestHandler] = []
863875
self.oneshot_handlers = RequestHandlerList()

0 commit comments

Comments
 (0)