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 hashes)
Built Distributions
Close
Hashes for httptestserver-0.1.0.linux-x86_64.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7249d2339ec7ace292f2a6e10b69aeccee9440a31f41b7b2661e75e1eca0f41 |
|
MD5 | d1a0e0cf01c334df963f00dbb7f28f6f |
|
BLAKE2b-256 | 66e64a4c8482116f3eedc4cf36c513203b9ff70733479afc1097aca70b9a61ea |
Close
Hashes for httptestserver-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43c88dfcf74259a718606e5b685b64a1e34b5a848eab95e906f08eddd22efb9c |
|
MD5 | 498fc69ce3967149b51715d4818539f5 |
|
BLAKE2b-256 | fac9b65c19ba695edfd689926da9c7d7b1d8fb4a806c76b2f786ac25d2fbe2ad |