|
2 | 2 | User's Guide |
3 | 3 | ============ |
4 | 4 |
|
5 | | -Starting and stopping |
6 | | ---------------------- |
7 | | -The server can be started by instatiating it and then calling the |
8 | | -:py:meth:`pytest_httpserver.HTTPServer.start` method. This will start the server in a separate |
9 | | -thread, so you will need to make sure that the :py:meth:`pytest_httpserver.HTTPServer.stop` method |
10 | | -is called before your code exits. |
11 | | - |
12 | | -When using the pytest plugin, the server is started at the first usage of the server and it will |
13 | | -remain running for the whole test suite. For each test function, a clear http server will be used |
14 | | -to avoid crosstalk. |
15 | | - |
16 | | -A free TCP port needs to be specified when instantiating the server. Setting 0 as the port number |
17 | | -will select a free (ephemeral) TCP port which is guaranteed to be free. |
18 | | -This is the default behavior. |
19 | | - |
20 | | -Specifying the bind host and port can be done in several ways: |
21 | | - |
22 | | -* Setting PYTEST_HTTPSERVER_HOST and PYTEST_HTTPSERVER_PORT will change the bind host and bind |
23 | | - port, respectively. |
24 | | - |
25 | | -* If pytest plugin is not used, the DEFAULT_LISTEN_HOST and DEFAULT_LISTEN_PORT class attributes can be set |
26 | | - on the HTTPServer class. |
27 | | - |
28 | | -* bind host and port can be specified for the constructor of the |
29 | | - :py:class:`pytest_httpserver.server.HTTPServer` class. |
30 | | - |
31 | | -* Overriding the ``httpserver_listen_address`` fixture in pytest. |
32 | | - |
33 | | - |
34 | | -Configuring |
35 | | ------------ |
36 | | -By configuring the server means registering handlers for specific requests. Once a request matches |
37 | | -with the configuration the specified response handler is fired and the reponse is served. |
38 | | - |
39 | | -Requests |
40 | | -~~~~~~~~ |
41 | | -When registering a :py:class:`pytest_httpserver.server.RequestMatcher`, it can use various parts |
42 | | -of the HTTP request to be matched: URI, method, data, headers, and query string can be specified. |
43 | | - |
44 | | -The following can be matched: |
45 | | - |
46 | | -* uri: a string, a regexp or an :py:class:`pytest_httpserver.server.URIPattern` object |
47 | | - |
48 | | -* method: GET/POST/..., specified as string |
49 | | - |
50 | | -* data: a string or bytes. It is possible to match with arbitrary byte data. |
51 | | - |
52 | | -* headers: a str-str dictionary or a :py:class:`pytest_httpserver.server.HeaderValueMatcher` object |
53 | | - which matches each header with its own provided callable |
54 | | - |
55 | | -* query_string: a string, bytes or a dict specifying the key-value pairs of the query string |
56 | | - |
57 | | -:py:class:`pytest_httpserver.server.HTTPServer` also determines how these matchers are looked up and |
58 | | -what their lifetime is. You can register handlers which handle any amount of requests, but you can also |
59 | | -register one-shot handlers which only handle one request and then they disappear. |
60 | | - |
61 | | -Also, there's ordered handlers which also specify the order of the requests to be handled. Not matching |
62 | | -the order of their registration, the server will refuse to serve any further requests. |
63 | | - |
64 | | -With all of these, you can create a server with very permissive to very strict request handling. |
65 | | - |
66 | | -Responses |
67 | | -~~~~~~~~~ |
68 | | -Once the request is matched with one of the matchers, the handler gets fired, which can return a static |
69 | | -response or you can create a function which can return a dynamic response. |
70 | | -When dealing with static responses you can determine all parts of the http response (status, headers, |
71 | | -content, etc), and you can also specify a JSON-serializable object to be returned as a json. |
72 | | - |
73 | | - |
74 | | -Waiting for test completion or errors |
75 | | -------------------------------------- |
76 | | -It is possible to wait until all oneshot and ordered handlers are exhausted or any error happened. This |
77 | | -is provided by a context manager which waits until one of these events occured. This can be further customized |
78 | | -by :py:class:`pytest_httpserver.server.WaitingSettings` object to raise or not raise assertion. |
79 | | - |
80 | | - |
81 | | -Debugging errors while testing |
82 | | ------------------------------- |
83 | | -When the tests are running against the server and no matcher can be found for the given request, the server |
84 | | -replies with HTTP status 500, and a short error text. This is not very helpful in most cases so if you want |
85 | | -to check what is the situation, you should call :py:meth:`pytest_httpserver.HTTPServer.format_matchers` or |
86 | | -:py:meth:`pytest_httpserver.HTTPServer.check_assertions` methods. The first one returns a human-readable |
87 | | -string representation of the matchers registered. The second one raises `AssertionError` with the errors |
88 | | -happened during the testing in the server. |
89 | | - |
90 | | -Also there's a :py:attr:`pytest_httpserver.HTTPServer.log` attribute which contains the request-reponse |
91 | | -object pairs what the server handled. |
| 5 | +User's guide has been superseded by the :ref:`tutorial` and the :ref:`howto`. |
0 commit comments