HTTP and HTTPS testing server
Project description
HttpTestServer
**************
.. image:: https://travis-ci.org/grupotaric/httptestserver.svg?branch=master
:target: https://travis-ci.org/grupotaric/httptestserver
HTTP/HTTPS server which can be run within a Python process. Runs in a
different thread along with the application exposing a simple thread-safe API,
so the calling code can control of how the server behaves.
Sometimes integration tests cannot do with mocking the ``socket.socket``
function avoiding real networking, this partially solves that problem by
providing a real server which is as easy to use and can perform real network
communication in a controlled and reliable way.
.. code:: python
import requests
from httptestserver import HttpsServerTest
class TestApplication(HttpsServerTest):
# Test what was actually get by the server
def test_it_should_send_headers(self):
headers = {'key': 'value'}
requests.get(self.default_url, headers=headers)
assert self.server.data['headers']['key'] == 'value'
# Control server responses
def test_it_should_parse_json_response(self):
self.server.data['headers'] = {'Content-Type': 'application/json'}
self.server.data['response_content'] = "{'key': 'value'}"
response = requests.get(self.default_url)
assert response.json() == {'key': 'value'}
# Make the server behave as you want
def test_it_should_raise_timeout_at_2s_wait(self):
self.server.data['response_timeout'] = 2
try:
requests.get(self.default_url, timeout=1)
except requests.exceptions.Timeout:
pass
else:
assert False
# Access to server's requests/responses history
def test_it_should_make_two_requests(self):
self.server.reset()
requests.get(self.default_url)
requests.get(self.default_url + '2')
assert len(self.server.history) == 2
assert self.server.history[-1]['path'] == self.default_url + '2'
Features:
* Runs in a different thread at the same time of your tests.
* Control server responses and behaviour.
* Access to server internal state and data after or during the request.
* HTTPs support, it bundles a self-signed certificate useful for testing.
* History of all server performed requests/responses.
Api
===
Functions which return a running server instance:
.. autofunction:: start_server
.. autofunction:: start_ssl_server
Context managers for short in-place usage:
.. autofunction:: http_server
.. autofunction:: https_server
The :class:`Server` class, with all the available functionality:
.. autoclass:: Server
:members:
The default handler is :class:`Handler` but it can be subclassed and extended:
.. autoclass:: httptestserver.server.Handler
:members:
Some mixings to start the server and use it directly from tests.
.. autoclass:: HttpServerTest
:members:
.. autoclass:: HttpsServerTest
:members:
Development
===========
In order get a development environment, create a virtualenv and install the
desired requirements.
.. code:: bash
virtualenv env
env/bin/activate
pip install -r dev-requirements.txt
The included certificate was generated using SSL:
.. code:: bash
openssl req -new -x509 -keyout server.pem -out server.pem -days 40000 -nodes
Tests
-----
To run the tests just use **tox** or **nose**:
.. code:: bash
tox
.. code:: bash
nosetests
Documentation
-------------
To generate the documentation change to the ``docs`` directory and run make.
You need to install the ``sphinx`` and ``changelog`` packages in order to be
able to run the makefile.
.. code:: bash
cd docs
make html
open build/html/index.html
**************
.. image:: https://travis-ci.org/grupotaric/httptestserver.svg?branch=master
:target: https://travis-ci.org/grupotaric/httptestserver
HTTP/HTTPS server which can be run within a Python process. Runs in a
different thread along with the application exposing a simple thread-safe API,
so the calling code can control of how the server behaves.
Sometimes integration tests cannot do with mocking the ``socket.socket``
function avoiding real networking, this partially solves that problem by
providing a real server which is as easy to use and can perform real network
communication in a controlled and reliable way.
.. code:: python
import requests
from httptestserver import HttpsServerTest
class TestApplication(HttpsServerTest):
# Test what was actually get by the server
def test_it_should_send_headers(self):
headers = {'key': 'value'}
requests.get(self.default_url, headers=headers)
assert self.server.data['headers']['key'] == 'value'
# Control server responses
def test_it_should_parse_json_response(self):
self.server.data['headers'] = {'Content-Type': 'application/json'}
self.server.data['response_content'] = "{'key': 'value'}"
response = requests.get(self.default_url)
assert response.json() == {'key': 'value'}
# Make the server behave as you want
def test_it_should_raise_timeout_at_2s_wait(self):
self.server.data['response_timeout'] = 2
try:
requests.get(self.default_url, timeout=1)
except requests.exceptions.Timeout:
pass
else:
assert False
# Access to server's requests/responses history
def test_it_should_make_two_requests(self):
self.server.reset()
requests.get(self.default_url)
requests.get(self.default_url + '2')
assert len(self.server.history) == 2
assert self.server.history[-1]['path'] == self.default_url + '2'
Features:
* Runs in a different thread at the same time of your tests.
* Control server responses and behaviour.
* Access to server internal state and data after or during the request.
* HTTPs support, it bundles a self-signed certificate useful for testing.
* History of all server performed requests/responses.
Api
===
Functions which return a running server instance:
.. autofunction:: start_server
.. autofunction:: start_ssl_server
Context managers for short in-place usage:
.. autofunction:: http_server
.. autofunction:: https_server
The :class:`Server` class, with all the available functionality:
.. autoclass:: Server
:members:
The default handler is :class:`Handler` but it can be subclassed and extended:
.. autoclass:: httptestserver.server.Handler
:members:
Some mixings to start the server and use it directly from tests.
.. autoclass:: HttpServerTest
:members:
.. autoclass:: HttpsServerTest
:members:
Development
===========
In order get a development environment, create a virtualenv and install the
desired requirements.
.. code:: bash
virtualenv env
env/bin/activate
pip install -r dev-requirements.txt
The included certificate was generated using SSL:
.. code:: bash
openssl req -new -x509 -keyout server.pem -out server.pem -days 40000 -nodes
Tests
-----
To run the tests just use **tox** or **nose**:
.. code:: bash
tox
.. code:: bash
nosetests
Documentation
-------------
To generate the documentation change to the ``docs`` directory and run make.
You need to install the ``sphinx`` and ``changelog`` packages in order to be
able to run the makefile.
.. code:: bash
cd docs
make html
open build/html/index.html
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
httptestserver-0.1.0.tar.gz
(8.5 kB
view details)
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file httptestserver-0.1.0.tar.gz.
File metadata
- Download URL: httptestserver-0.1.0.tar.gz
- Upload date:
- Size: 8.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fe28da8872adb373acbcdd31a498212012e226c050f65c63c6ff1d9fcf5aff3
|
|
| MD5 |
02217a72485b7fb82e9d1650674778e9
|
|
| BLAKE2b-256 |
b0605e181dd838800eba3c57a10b435cbf4d4fd40d54a7a9a9d7ac6dd9ded039
|
File details
Details for the file httptestserver-0.1.0.linux-x86_64.tar.gz.
File metadata
- Download URL: httptestserver-0.1.0.linux-x86_64.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d7249d2339ec7ace292f2a6e10b69aeccee9440a31f41b7b2661e75e1eca0f41
|
|
| MD5 |
d1a0e0cf01c334df963f00dbb7f28f6f
|
|
| BLAKE2b-256 |
66e64a4c8482116f3eedc4cf36c513203b9ff70733479afc1097aca70b9a61ea
|
File details
Details for the file httptestserver-0.1.0-py3-none-any.whl.
File metadata
- Download URL: httptestserver-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43c88dfcf74259a718606e5b685b64a1e34b5a848eab95e906f08eddd22efb9c
|
|
| MD5 |
498fc69ce3967149b51715d4818539f5
|
|
| BLAKE2b-256 |
fac9b65c19ba695edfd689926da9c7d7b1d8fb4a806c76b2f786ac25d2fbe2ad
|