|
3 | 3 | You can adapt this file completely to your liking, but it should at least |
4 | 4 | contain the root `toctree` directive. |
5 | 5 |
|
6 | | -Welcome to pytest_httpserver's documentation! |
7 | | -============================================= |
| 6 | +
|
| 7 | +User's Guide |
| 8 | +------------ |
| 9 | +pytest-httpserver is a python package which allows you to start a real HTTP server |
| 10 | +for your tests. The server can be configured programmatically to how to respond to |
| 11 | +requests. |
| 12 | + |
| 13 | +The aim of this project is to provide an easy to use API to start the server, configure |
| 14 | +the request handlers and then shut it down gracefully. All of these without touching |
| 15 | +a configuration file or dealing with daemons. |
| 16 | + |
| 17 | +As the HTTP server is spawned in a different thread and listening on a TCP port, you |
| 18 | +can use any HTTP client. This library also helps you migrating to a different HTTP |
| 19 | +client library without the need to re-write any test for your client application. |
| 20 | + |
| 21 | +This library can be used with pytest in the most convenient way but if you prefer to use |
| 22 | +other test frameworks, you can still use it with the context API or by writing a wrapper |
| 23 | +for it. |
| 24 | + |
| 25 | +Example with pytest |
| 26 | +------------------- |
| 27 | + |
| 28 | +.. code:: python |
| 29 | +
|
| 30 | + import requests |
| 31 | +
|
| 32 | + def test_json_client(httpserver: HTTPServer): |
| 33 | + httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"}) |
| 34 | + assert requests.get(httpserver.url_for("/foobar")).json() == {'foo': 'bar'} |
| 35 | +
|
| 36 | +
|
| 37 | +Example without pytest |
| 38 | +---------------------- |
| 39 | + |
| 40 | +.. code:: python |
| 41 | +
|
| 42 | + import requests |
| 43 | + import unittest |
| 44 | + from pytest_httpserver import HTTPServer |
| 45 | +
|
| 46 | + class TestJSONClient(unittest.TestCase): |
| 47 | + def setUp(self): |
| 48 | + self.httpserver = HTTPServer() |
| 49 | + self.httpserver.start() |
| 50 | +
|
| 51 | + def test_json_client(self): |
| 52 | + self.httpserver.expect_request("/foobar").respond_with_json({"foo": "bar"}) |
| 53 | + self.assertEqual(requests.get(self.httpserver.url_for("/foobar")).json(), {'foo': 'bar'}) |
| 54 | +
|
| 55 | + def tearDown(self): |
| 56 | + self.httpserver.stop() |
| 57 | +
|
| 58 | +
|
| 59 | +API Reference |
| 60 | +------------- |
| 61 | + |
| 62 | +If you are looking for information on a specific function, class or |
| 63 | +method, this part of the documentation is for you. |
8 | 64 |
|
9 | 65 | .. toctree:: |
10 | 66 | :maxdepth: 2 |
11 | | - :caption: Contents: |
| 67 | + |
| 68 | + api |
12 | 69 |
|
13 | 70 |
|
14 | 71 |
|
|
0 commit comments