Skip to main content

HTTP and HTTPS testing server

Project description

Latest Pypi version Documentation Status Last build status

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 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 easy to use and can perform real network communication in a controlled and reliable way.

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.

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

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.1.1.tar.gz (8.7 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.1.1-py3.4.egg (15.7 kB view details)

Uploaded Egg

httptestserver-0.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for httptestserver-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3bde4a0b28c9e76a6196a85b670fa348ba3e30fc4ec2aa9dff0ea8cef4c5d6b0
MD5 e75d1ff5c1307ad5467d90aac18d2ddd
BLAKE2b-256 cf267665fc15459790f0bd8306dcc969acfcd0b4e3e601a0db1e44f670a6430c

See more details on using hashes here.

File details

Details for the file httptestserver-0.1.1-py3.4.egg.

File metadata

File hashes

Hashes for httptestserver-0.1.1-py3.4.egg
Algorithm Hash digest
SHA256 3f6de2c320ecc40933c13e126ca30207cefc618e22fa2cf9f501c5bd35ea5546
MD5 de605e6be6d857387f5280dc12707c71
BLAKE2b-256 0e4c0ace38d15355223d825a15daca40a9e352595b621cf3deca08b2e1f1bcda

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for httptestserver-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 00fe6797dc00eae09debe1789eb0482401d4bf87d20d005f323645431999656f
MD5 575468d45597307f3fdd75a6357b0a17
BLAKE2b-256 0aa014541b896594a9d1de09b27e21b8baf3e236533a2e33799faad87cbb1111

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