Skip to main content

HTTP(s) and SMTP testing server

Project description

Latest Pypi version Documentation Status Last build status

HTTP(s) and SMTP servers which can be run within a Python process. Serving from different thread along with application and tests, exposing a simple thread-safe API, so the calling code can control how the server behaves.

Sometimes integration tests cannot do with mocking the socket.socket function avoiding real networking, this partially solves the problem by providing a real server which is easy to use and can perform real network communication in a controlled and reliable way.

Features:

  • Runs in a different thread along with 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.

  • SMTP support which will collect and parse all your outgoing email.

  • History of all performed requests/responses.

Supports python 2.7 and 3.4

Functions

Functions that return a running server instance:

>>> server = start_server()
>>> server.host
'127.0.0.1'

Or context managers for limited use:

>>> with http_server() as server:
...     server.host
'127.0.0.1'

Mixin classes

Mixins that include an working server as self.server.

import requests
from httptestserver import HttpsTestServer

class TestApplication(HttpsTestServer):

    # 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'

Development

In order get a development environment, create a virtualenv and install the desired requirements.

virtualenv env
env/bin/activate
pip install -r dev-requirements.txt

The included certificate was generated using SSL:

openssl req -new -x509 -keyout server.pem -out server.pem -days 40000 -nodes

Tests

To run the tests just use tox or nose:

tox
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.

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.2.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

httptestserver-0.2.0-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

httptestserver-0.2.0-py2-none-any.whl (13.2 kB view details)

Uploaded Python 2

File details

Details for the file httptestserver-0.2.0.tar.gz.

File metadata

File hashes

Hashes for httptestserver-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94cdc1eb9ce05df27ca8725ba8d454a26bd05169cdde2b12d9cba5b4f6558ab0
MD5 d916dc0bdfb070727b3c64872a090978
BLAKE2b-256 e749346c4017fa7114973b47b108eca0213513b0567da811d5a6a7d6d8c8348e

See more details on using hashes here.

File details

Details for the file httptestserver-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for httptestserver-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d386099a3735b225c0f16ec35ed19cabf9271b2ae8c1f1b64eb07cc9e694dd0
MD5 9baa271b0ddc55690ebd69cf35397b4e
BLAKE2b-256 0994d50e0fbea2277f1d6a84921cff0928883695b3d7c6b56fc98223d22726a8

See more details on using hashes here.

File details

Details for the file httptestserver-0.2.0-py2-none-any.whl.

File metadata

File hashes

Hashes for httptestserver-0.2.0-py2-none-any.whl
Algorithm Hash digest
SHA256 630f8a24386d9b66a53e3fbc3c5d7511a5a6d39bd5e300b862d7e5083f3aa127
MD5 21925159469689ffd781fe4e11f68cd0
BLAKE2b-256 1fad8c405e04b5df0fdcc89d196b001280b7d43a74cf9e5600bdaef3b67d6b32

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page