Skip to content

Commit

Permalink
Adding 'pook' integration (#41).
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgio Salluzzo committed Jan 29, 2017
1 parent d2a27f7 commit 04c0c84
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ install-dev-requirements:
pip install -q -e .

install-test-requirements:
pip install -q -r test_requirements.txt
pip install -q -e .[tests]

test-python:
@echo "Running Python tests"
Expand Down
1 change: 0 additions & 1 deletion mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,4 +343,3 @@ def wrapper(test, *args, **kw):
return test(*args, **kw)
return decorator.decorator(wrapper, test)
mocketize = Mocketizer.wrap

Empty file added mocket/plugins/__init__.py
Empty file.
65 changes: 65 additions & 0 deletions mocket/plugins/mocket_pook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
try: # pook not available on Python 2.6
from pook.engine import MockEngine
from pook.interceptors.base import BaseInterceptor

from mocket.mocket import Mocket
from mocket.mockhttp import Entry, Response


class MocketPookEntry(Entry):
pook_request = None
pook_engine = None

def can_handle(self, data):
can_handle = super(MocketPookEntry, self).can_handle(data)

if can_handle:
self.pook_engine.match(self.pook_request)
return can_handle

@classmethod
def single_register(cls, method, uri, body='', status=200, headers=None):
entry = cls(uri, method, Response(body=body, status=status, headers=headers))
Mocket.register(entry)
return entry


class MocketInterceptor(BaseInterceptor):
def activate(self):
Mocket.disable()
Mocket.enable()

def disable(self):
Mocket.disable()


class MocketEngine(MockEngine):

def __init__(self, engine):
# Store plugins engine
self.engine = engine
# Store HTTP client interceptors
self.interceptors = []
# Self-register MocketInterceptor
self.add_interceptor(MocketInterceptor)

def activate(self):
for mock in self.engine.mocks:

request = mock._request
method = request.method
url = request.rawurl

response = mock._response
body = response._body
status = response._status
headers = response._headers

entry = MocketPookEntry.single_register(method, url, body, status, headers)
entry.pook_engine = self.engine
entry.pook_request = request

super(MocketEngine, self).activate()

except ImportError:
pass
30 changes: 21 additions & 9 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from setuptools import setup, find_packages, os
import sys
from io import open

from setuptools import setup, find_packages, os

major, minor = sys.version_info[:2]

# Hack to prevent stupid "TypeError: 'NoneType' object is not callable" error
# in multiprocessing/util.py _exit_function when running `python
# setup.py test` (see
Expand All @@ -11,25 +15,33 @@
except ImportError:
pass

dev_requires = []
install_requires = open(os.path.join(os.path.dirname(__file__), 'requirements.txt')).read()
tests_require = open(os.path.join(os.path.dirname(__file__), 'test_requirements.txt')).read()
install_requires = open(os.path.join(os.path.dirname(__file__), 'requirements.txt')).readlines()
tests_requires = open(os.path.join(os.path.dirname(__file__), 'test_requirements.txt')).readlines()
pook_requires = ['pook>=0.1.13']

exclude_packages = ['tests', 'tests35', 'mocket.plugins.pook']
# plugins not available on Python 2.6
if major > 2 or (major == 2 and minor > 6):
exclude = exclude_packages[:-1]
tests_requires += pook_requires

setup(
name='mocket',
version='1.5.2',
version='1.6.0',
# author='Andrea de Marco, Giorgio Salluzzo',
author='Giorgio Salluzzo',
# author_email='24erre@gmail.com, giorgio.salluzzo@gmail.com',
author_email='giorgio.salluzzo@gmail.com',
url='https://github.com/mindflayer/python-mocket',
description='Socket Mock Framework - for all kinds of socket animals, web-clients included - with gevent/asyncio/SSL support',
description='Socket Mock Framework - for all kinds of socket animals, web-clients included - \
with gevent/asyncio/SSL support',
long_description=open('README.rst', encoding='utf-8').read(),
packages=find_packages(exclude=('tests', 'tests35')),
packages=find_packages(exclude=exclude_packages),
install_requires=install_requires,
extras_require={
'tests': tests_require,
'dev': dev_requires,
'tests': tests_requires,
'dev': [],
'pook': pook_requires, # plugins version supporting mocket.plugins.pook.MocketEngine
},
test_suite='runtests.runtests',
license='BSD',
Expand Down
2 changes: 1 addition & 1 deletion test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pytest
pytest-cov
mock
requests
redis
redis
28 changes: 28 additions & 0 deletions tests/tests27/test_pook.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import requests

from mocket.plugins.mocket_pook import MocketEngine


def test_pook_engine():
import pook
pook.activate()

pook.set_mock_engine(MocketEngine)
mock = pook.get(
'http://twitter.com/api/1/foobar',
body='{"a": 1}',
headers={'content-type': 'application/json'},
reply=404,
response_json={'error': 'foo'},
)
mock.persist()

requests.get('http://twitter.com/api/1/foobar')
assert mock.calls == 1

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

pook.disable()

0 comments on commit 04c0c84

Please sign in to comment.