Skip to main content

Expressive and simple library for mocking out HTTP traffic in Python.

Project description

# pook [![Build Status](https://travis-ci.org/h2non/pook.svg?branch=master)](https://travis-ci.org/h2non/pook) [![PyPI](https://img.shields.io/pypi/v/pook.svg?maxAge=2592000?style=flat-square)](https://pypi.python.org/pypi/pook) [![API](https://img.shields.io/badge/api-docs-green.svg)](https://h2non.github.io/pook) [![Documentation Status](https://readthedocs.org/projects/pook/badge/?version=latest)](http://pook.readthedocs.io/en/latest/?badge=latest)

Simply and expressive utility library for mocking and expectations for HTTP traffic in [Python](http://python.org).

Small and dependency-free package to infer file type and MIME type checking the [magic numbers](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_numbers_in_files) signature of a file or buffer.

pook is heavily inspired by [gock](https://github.com/h2non/gock).

**Note**: this is a work in progress.

## Features

- Simple, expressive and fluent API
- Full-featured, idiomatic HTTP expectations.
- JSON schema based body matching.
- Extensible: write your own HTTP expections.
- HTTP client agnostic (works with most popular HTTP libraries).
- Pluggable hackable API.
- Compatible with Python 2 and 3.

## Supported HTTP clients

- [x] urllib3
- [x] requests
- [ ] urllib
- [ ] aiohttp
- [ ] pycurl

## Installation

Using `pip` package manager:
```bash
pip install pook
```

Or install the latest sources from Github::
```bash
pip install -e git+git://github.com/h2non/pook.git#egg=pook
```

## API

See [annotated API reference](https://h2non.github.io/pook).

## Examples

#### Basic mocking

```python
import pook
import requests

@pook.activate
def test_my_api():
mock = httpok.get('http://twitter.com/api/1/foobar',
type='application/json',
json={'error': 'not found'})
mock.reply(404, json={'error': 'foo'})

resp = requests.get('http://twitter.com/api/1/foobar')
assert resp.json() == {"error": "not found"}
assert len(mock.calls) == 1
assert mock.calls[0].request.url == 'http://twitter.com/api/1/foobar'
assert mock.calls[0].response.text == '{"error": "not found"}'
```

#### Using the fluent API

```python
import pook
import requests

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

resp = requests.get('http://twitter.com/api/1/foobar')
assert resp.json() == {"error": "not found"}
assert len(mock.calls) == 1
assert mock.calls[0].request.url == 'http://twitter.com/api/1/foobar'
assert mock.calls[0].response.text == '{"error": "not found"}'
```

## 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-0.0.1.tar.gz (10.3 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: pook-0.0.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for pook-0.0.1.tar.gz
Algorithm Hash digest
SHA256 7f690cd1e744dd956ac022642287674b9616ceea6cb7e2f6def6c8e8b0d2bb07
MD5 8632df8da23eabe143d20ff28e05beee
BLAKE2b-256 c4e4870fb292fdb593c273d991de63d5ac44b340ba6298496503f3e9a0af2ead

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