Skip to main content

Server for testing HTTP clients

Project description

https://travis-ci.org/lorien/test_server.png?branch=master

Simple HTTP Server for testing HTTP clients.

Installation

pip install test_server

Usage Example

Example:

from unittest import TestCase
import unittest
from urllib.request import urlopen

from test_server import TestServer, Response, HttpHeaderStorage


class UrllibTestCase(TestCase):
    @classmethod
    def setUpClass(cls):
        cls.server = TestServer()
        cls.server.start()

    @classmethod
    def tearDownClass(cls):
        cls.server.stop()

    def setUp(self):
        self.server.reset()

    def test_get(self):
        self.server.add_response(
            Response(
                data=b"hello",
                headers={"foo": "bar"},
            )
        )
        self.server.add_response(Response(data=b"zzz"))
        url = self.server.get_url()
        info = urlopen(url)
        self.assertEqual(b"hello", info.read())
        self.assertEqual("bar", info.headers["foo"])
        info = urlopen(url)
        self.assertEqual(b"zzz", info.read())
        self.assertTrue("bar" not in info.headers)


unittest.main()

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

test_server-0.0.36.tar.gz (12.1 kB view hashes)

Uploaded Source

Supported by

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