1- |build | |doc |
1+ [ ![ Build Status] ( https://travis-ci.org/csernazs/pytest-httpserver.svg?branch=master )] ( https://travis-ci.org/csernazs/pytest-httpserver )
2+ [ ![ Documentation Status] ( https://readthedocs.org/projects/pytest-httpserver/badge/?version=latest )] ( https://pytest-httpserver.readthedocs.io/en/latest/?badge=latest )
23
4+ ## pytest_httpserver
35
4- pytest_httpserver
5- =================
66HTTP server for pytest
77
88
9- Nutshell
10- --------
9+ ### Nutshell
1110
1211This library is designed to help to test http clients without contacting the real http server.
1312In other words, it is a fake http server which is accessible via localhost can be started with
1413the pre-defined expected http requests and their responses.
1514
16- Example
17- -------
18-
19- .. code-block :: python
20-
21- def test_my_client (httpserver ): # httpserver is a pytest fixture which starts the server
22- # set up the server to serve /foobar with the json
23- httpserver.expect_request(" /foobar" ).respond_with_json({" foo" : " bar" })
24- # check that the request is served
25- assert requests.get(httpserver.url_for(" /foobar" )).json() == {' foo' : ' bar' }
15+ ### Example
2616
17+ ``` python
18+ def test_my_client (httpserver ): # httpserver is a pytest fixture which starts the server
19+ # set up the server to serve /foobar with the json
20+ httpserver.expect_request(" /foobar" ).respond_with_json({" foo" : " bar" })
21+ # check that the request is served
22+ assert requests.get(httpserver.url_for(" /foobar" )).json() == {' foo' : ' bar' }
23+ ```
2724
2825You can also use the library without pytest. There's a with statement to ensure that the server is stopped.
2926
3027
31- .. code-block :: python
28+ ``` python
29+ with HTTPServer() as httpserver:
30+ # set up the server to serve /foobar with the json
31+ httpserver.expect_request(" /foobar" ).respond_with_json({" foo" : " bar" })
32+ # check that the request is served
33+ print (requests.get(httpserver.url_for(" /foobar" )).json())
34+ ```
3235
33- with HTTPServer() as httpserver:
34- # set up the server to serve /foobar with the json
35- httpserver.expect_request(" /foobar" ).respond_with_json({" foo" : " bar" })
36- # check that the request is served
37- print (requests.get(httpserver.url_for(" /foobar" )).json())
3836
37+ ### Features
3938
40- Features
41- --------
4239You can set up a dozen of expectations for the requests, and also what response should be sent by the server to the client.
4340
4441
45- Requests
46- ~~~~~~~~
42+ #### Requests
43+
4744There are three different types:
4845
4946- ** permanent** : this will be always served when there's match for this request, you can make as many HTTP requests as you want
@@ -59,8 +56,7 @@ You can also fine-tune the expected request. The following can be specified:
5956- data (HTTP payload of the request)
6057
6158
62- Responses
63- ~~~~~~~~~
59+ #### Responses
6460
6561Once you have the expectations for the request set up, you should also define the response you want to send back.
6662The following is supported currently:
@@ -77,16 +73,7 @@ Similar to requests, you can fine-tune what response you want to send:
7773- data
7874
7975
80- Missing features
81- ----------------
76+ ### Missing features
8277* HTTP/2
8378* Keepalive
8479* TLS
85-
86-
87- .. |build | image :: https://travis-ci.org/csernazs/pytest-httpserver.svg?branch=master
88- :target: https://travis-ci.org/csernazs/pytest-httpserver
89-
90- .. |doc | image :: https://readthedocs.org/projects/pytest-httpserver/badge/?version=latest
91- :target: https://pytest-httpserver.readthedocs.io/en/latest/?badge=latest
92- :alt: Documentation Status
0 commit comments