Skip to content

Commit cede788

Browse files
committed
doc: make the documentation more complete
1 parent 37c6406 commit cede788

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

doc/guide.rst

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
User's Guide
33
============
44

5-
Starting and stopping the server
6-
--------------------------------
5+
Starting and stopping
6+
---------------------
77
The server can be started by instatiating it and then calling the
88
:py:meth:`pytest_httpserver.HTTPServer.start` method. This will start the server in a separate
99
thread, so you will need to make sure that the :py:meth:`pytest_httpserver.HTTPServer.stop` method
@@ -12,5 +12,46 @@ is called before your code exits.
1212
A free TCP port needs to be specified when instantiating the server, where no other daemon is listening.
1313

1414
If you are using the pytest plugin it is done automatically by the plugin. Possibility to change
15-
the TCP port in this case is TBD.
15+
the TCP port is TBD.
1616

17+
18+
Configuring
19+
-----------
20+
By configuring the server means registering handlers for specific requests. Once a request matches
21+
with the configuration the specified response handler is fired and the reponse is served.
22+
23+
Requests
24+
~~~~~~~~
25+
When registering a :py:class:`pytest_httpserver.server.RequestMatcher`, it can use various parts
26+
of the HTTP request to be matched: URI, method, data, headers, and query string can be specified.
27+
All of these are based on simple qeuality checking, with the exception of method and URI where a special
28+
value specifying `any` can be given (variables `URI_DEFAULT` and `METHOD_ALL`, respectively).
29+
30+
:py:class:`pytest_httpserver.server.HTTPServer` also determines how these matchers are looked up and
31+
what their lifetime is. You can register handlers which handle any amount of requests, but you can also
32+
register one-shot handlers which only handle one request and then they disappear.
33+
34+
Also, there's ordered handlers which also specify the order of the requests to be handled. Not matching
35+
the order of their registration, the server will refuse to serve any further requests.
36+
37+
With all of these, you can create a server with very permissive to very strict request handling.
38+
39+
Responses
40+
~~~~~~~~~
41+
Once the request is matched with one of the matchers, the handler gets fired, which can return a static
42+
response or you can create a function which can return a dynamic response.
43+
When dealing with static responses you can determine all parts of the http response (status, headers,
44+
content, etc), and you can also specify a JSON-serializable object to be returned as a json.
45+
46+
47+
Debugging errors while testing
48+
------------------------------
49+
When the tests are running against the server and no matcher can be found for the given request, the server
50+
replies with HTTP status 500, and a short error text. This is not very helpful in most cases so if you want
51+
to check what is the situation, you should call :py:meth:`pytest_httpserver.HTTPServer.format_matchers` or
52+
:py:meth:`pytest_httpserver.HTTPServer.check_assertions` methods. The first one returns a human-readable
53+
string representation of the matchers registered. The second one raises `AssertionError` with the errors
54+
happened during the testing in the server.
55+
56+
Also there's a :py:attr:`pytest_httpserver.HTTPServer.log` attribute which contains the request-reponse
57+
object pairs what the server handled.

pytest_httpserver/httpserver.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ class HTTPServer: # pylint: disable=too-many-instance-attributes
253253
254254
:param host: the host or IP where the server will listen
255255
:param port: the TCP port where the server will listen
256+
257+
.. py:attribute:: log
258+
259+
Attribute containing the list of two-element tuples. Each tuple contains
260+
:py:class:`Request` and :py:class:`Response` object which represents the
261+
incoming request and the outgoing response which happened during the lifetime
262+
of the server.
256263
"""
257264

258265
def __init__(self, host="localhost", port=4000):

0 commit comments

Comments
 (0)