1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
# Documentation for test_server package
[](https://github.com/lorien/test_server/actions/workflows/test.yml)
[](https://github.com/lorien/test_server/actions/workflows/test.yml)
[](https://github.com/lorien/test_server/actions/workflows/mypy.yml)
[](https://coveralls.io/github/lorien/test_server)
[](http://user-agent.readthedocs.org)
Simple HTTP Server for testing HTTP clients.
## Installation
Run `pip install -U test_server`
## Usage Example
```python
from unittest import TestCase
import unittest
from urllib.request import urlopen
from test_server import TestServer, Response, HttpHeaderStorage
class UrllibTestCase(TestCase):
@classmethod
def setUpClass(cls):
cls.server = TestServer()
cls.server.start()
@classmethod
def tearDownClass(cls):
cls.server.stop()
def setUp(self):
self.server.reset()
def test_get(self):
self.server.add_response(
Response(
data=b"hello",
headers={"foo": "bar"},
)
)
self.server.add_response(Response(data=b"zzz"))
url = self.server.get_url()
info = urlopen(url)
self.assertEqual(b"hello", info.read())
self.assertEqual("bar", info.headers["foo"])
info = urlopen(url)
self.assertEqual(b"zzz", info.read())
self.assertTrue("bar" not in info.headers)
unittest.main()
```
|