Skip to main content

Simple http server for API mocking during automated testing

Project description

Simple http server for API mocking during automated testing Plays nicely with httpsrvvcr library for automated request recording

Example usage

A typical usage pattern would probably look like the one below.

Using requests library:

import unittest
import requests
from httpsrv import Server

server = Server(8080).start()

class MyTestCase(unittest.TestCase):
    def setUp(self):
        server.reset()

    def test_should_get_hello(self):
        # this means that server will respond once upon GET request
        # further GET requests on this path will get 500
        server.on_any('GET', '/').text('hello')
        res = requests.get('http://localhost:8080')
        assert res.text == 'hello'

    def test_should_always_respond_to_options(self):
        # this means that any OPTIONS request will get status 200
        # such behavior is particulary useful when mocking preflight queries
        server.on_any('OPTIONS', always=True).status(200)
        res = requests.get('http://localhost:8080')
        assert res.status_code == 200

    def test_should_respond_to_json(self):
        # this means that server will respond to the POST request
        # containing target json document in its body
        server.on_json('POST', '/users', {'name': 'John Doe'}).json(
            {'id': 1, 'name': 'John Doe'}, status=201)
        res = requests.post('http://localhost:8080/users', json={'name': 'John Doe'})
        assert res.status_code == 201

For more details and full list of on_* methods see API documentation

Installation

pip install httpsrv

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for httpsrv, version 1.0b1
Filename, size File type Python version Upload date Hashes
Filename, size httpsrv-1.0b1-py3-none-any.whl (7.4 kB) File type Wheel Python version 3.4 Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page