Skip to main content

HttpTestServer

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 3.2, 3.3 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'
>>> with smtp_server() as server:
...     server.inbox
[]

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):
        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

Metadata

Release files for httptestserver 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for httptestserver 0.3.1
File Size Uploaded Provenance Actions
httptestserver-0.3.1.tar.gz 11.2 kB Without provenance Details for httptestserver-0.3.1.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for httptestserver 0.3.1
File Interpreter ABI Platform Actions
httptestserver-0.3.1-py3-none-any.whl Python 3 none any Details for httptestserver-0.3.1-py3-none-any.whl
httptestserver-0.3.1-py2-none-any.whl Python 2 none any Details for httptestserver-0.3.1-py2-none-any.whl

Total release size: 39.2 kB

Release files / httptestserver-0.3.1.tar.gz

Download URL httptestserver-0.3.1.tar.gz
Size 11.2 kB
Tags Source
SHA-256 checksum
How to use checksums
80c2bde499afe275ea2327b0c5151efca8080368c22b8bd8ff4aae9d81b65a80
BLAKE2b-256 checksum
How to use checksums
2b5a1f9f20104a59083e36ca017ffc793d7fdc02342cb05034e40e8c3f0c0392
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / httptestserver-0.3.1-py3-none-any.whl

Download URL httptestserver-0.3.1-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5e26dbc53a0c43408d77a0bf4d031c9bc6a3a7dd6b4a40361ba9bcadff18850c
BLAKE2b-256 checksum
How to use checksums
bc3706b986313cf02a786e617d22520f502e197b36328bc368fb42ab3a88b96f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / httptestserver-0.3.1-py2-none-any.whl

Download URL httptestserver-0.3.1-py2-none-any.whl
Size 14.0 kB
Tags Python 2
SHA-256 checksum
How to use checksums
6800ed6bc95ff136062c3053d6696d6f3334cddd33510f708b88d2069384f881
BLAKE2b-256 checksum
How to use checksums
b13817745987a46733728263c42fd01bfc813994eae056b6b8083085990c186c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for httptestserver 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page