Skip to main content

HTTP traffic mocking and expectations made easy

Project description

Versatile, expressive and hackable utility library for HTTP traffic mocking and expectations made easy in Python. Heavily inspired by gock.

To get started, read the documentation, how it works, FAQ or examples.

Features

  • Simple, expressive and fluent API.

  • Provides both Pythonic and chainable DSL API styles.

  • Full-featured HTTP response definitions and expectations.

  • Matches any HTTP protocol primitive (URL, method, query params, headers, body…).

  • Full regular expressions capable mock expectations matching.

  • Supports most popular HTTP clients via interceptor adapters.

  • Configurable volatile, persistent or TTL limited mocks.

  • Works with unittest and pytest.

  • First-class JSON & XML support matching and responses.

  • Supports JSON Schema body matching.

  • Works in both runtime and testing environments.

  • Can be used as decorator and/or via context managers.

  • Supports real networking mode with optional traffic filtering.

  • Map/filter mocks easily for generic or custom mock expectations.

  • Custom user-defined mock matcher functions.

  • Simulated raised error exceptions.

  • Network delay simulation (only available for aiohttp).

  • Pluggable and hackable API.

  • Customizable HTTP traffic mock interceptor engine.

  • Supports third-party mocking engines, such as mocket.

  • Fits good for painless test doubles.

  • Does not support WebSocket traffic mocking.

  • Works with +3.9 (including PyPy).

  • Dependency-less: just 3 small dependencies for JSONSchema, XML tree comparison, and URL parsing.

Supported HTTP clients

pook can work with multiple mock engines, however it provides a built-in one by default, which currently supports traffic mocking in the following HTTP clients:

More HTTP clients can be supported progressively.

Note: only recent HTTP client package versions were tested.

Installation

Using pip package manager (requires pip 1.8+):

pip install --upgrade pook

Or install the latest sources from GitHub:

pip install -e git+git://github.com/h2non/pook.git#egg=pook

Getting started

See ReadTheDocs documentation:

Documentation Status

API

See annotated API reference documention.

Examples

See examples documentation for full featured code and use case examples.

Basic mocking:

import pook
import requests

@pook.on
def test_my_api():
    mock = pook.get('http://twitter.com/api/1/foobar', reply=404, response_json={'error': 'not found'})

    resp = requests.get('http://twitter.com/api/1/foobar')
    assert resp.status_code == 404
    assert resp.json() == {"error": "not found"}
    assert mock.calls == 1

Using the chainable API DSL:

import pook
import requests

@pook.on
def test_my_api():
    mock = (pook.get('http://twitter.com/api/1/foobar')
              .reply(404)
              .json({'error': 'not found'}))

    resp = requests.get('http://twitter.com/api/1/foobar')
    assert resp.json() == {"error": "not found"}
    assert mock.calls == 1

Using the decorator:

import pook
import requests

@pook.get('http://httpbin.org/status/500', reply=204)
@pook.get('http://httpbin.org/status/400', reply=200)
def fetch(url):
    return requests.get(url)

res = fetch('http://httpbin.org/status/400')
print('#1 status:', res.status_code)

res = fetch('http://httpbin.org/status/500')
print('#2 status:', res.status_code)

Simple unittest integration:

import pook
import unittest
import requests


class TestUnitTestEngine(unittest.TestCase):

    @pook.on
    def test_request(self):
        pook.get('server.com/foo').reply(204)
        res = requests.get('http://server.com/foo')
        self.assertEqual(res.status_code, 204)

    def test_request_with_context_manager(self):
        with pook.use():
            pook.get('server.com/bar', reply=204)
            res = requests.get('http://server.com/bar')
            self.assertEqual(res.status_code, 204)

Using the context manager for isolated HTTP traffic interception blocks:

import pook
import requests

# Enable HTTP traffic interceptor
with pook.use():
    pook.get('http://httpbin.org/status/500', reply=204)

    res = requests.get('http://httpbin.org/status/500')
    print('#1 status:', res.status_code)

# Interception-free HTTP traffic
res = requests.get('http://httpbin.org/status/200')
print('#2 status:', res.status_code)

Example using mocket Python library as underlying mock engine:

import pook
import requests
from mocket.plugins.pook_mock_engine import MocketEngine

# Use mocket library as underlying mock engine
pook.set_mock_engine(MocketEngine)

# Explicitly enable pook HTTP mocking (optional)
pook.on()

# Target server URL to mock out
url = 'http://twitter.com/api/1/foobar'

# Define your mock
mock = pook.get(url,
                reply=404, times=2,
                headers={'content-type': 'application/json'},
                response_json={'error': 'foo'})

# Run first HTTP request
requests.get(url)
assert mock.calls == 1

# Run second HTTP request
res = requests.get(url)
assert mock.calls == 2

# Assert response data
assert res.status_code == 404
assert res.json() == {'error': 'foo'}

# Explicitly disable pook (optional)
pook.off()

Example using Hy language (Lisp dialect for Python):

(import [pook])
(import [requests])

(defn request [url &optional [status 404]]
  (doto (.mock pook url) (.reply status))
  (let [res (.get requests url)]
    (. res status_code)))

(defn run []
  (with [(.use pook)]
    (print "Status:" (request "http://server.com/foo" :status 204))))

;; Run test program
(defmain [&args] (run))

Contributing

See contributing for how to contribute to Pook.

License

MIT - Tomas Aparicio

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

pook-2.1.2.tar.gz (32.8 kB view details)

Uploaded Source

Built Distribution

pook-2.1.2-py3-none-any.whl (46.3 kB view details)

Uploaded Python 3

File details

Details for the file pook-2.1.2.tar.gz.

File metadata

  • Download URL: pook-2.1.2.tar.gz
  • Upload date:
  • Size: 32.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for pook-2.1.2.tar.gz
Algorithm Hash digest
SHA256 4acbb9d13ac18b807fd3a54b414a22a16b75db6e5048bc88461479da03c4ecbf
MD5 c84a195bb356accaa542e7e88bfeefc3
BLAKE2b-256 89fa0ad434d4b6fc7aa399f55bd0813b76cb18b19504b4b3b486a006a7d5f44f

See more details on using hashes here.

File details

Details for the file pook-2.1.2-py3-none-any.whl.

File metadata

  • Download URL: pook-2.1.2-py3-none-any.whl
  • Upload date:
  • Size: 46.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.13.0

File hashes

Hashes for pook-2.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea76784ee1440ee8dde08047c6a1f68be46a07d07a5180d068a047db5620ca1d
MD5 70442ac058ab9a456a671939e13cb829
BLAKE2b-256 c4bc83928acd3cdfbd4ef686b6892c21b341497b07d42002e82d9d8f619b541c

See more details on using hashes here.

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