Skip to content

Commit

Permalink
merge(master)
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Dec 7, 2016
2 parents 7d43f50 + e1189c9 commit a8f81f6
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 15 deletions.
21 changes: 8 additions & 13 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Versatile, expressive and hackable utility library for HTTP traffic mocking and

**Note**: still beta quality library. Additional features, more examples and better test coverage are still pending.

**UPDATE**: version `0.1.3`_ is coming shortly fixing most bugs and introducing new features.

Features
--------
Expand Down Expand Up @@ -48,7 +49,7 @@ Multiple versions can be supported, but the latest one will have always priority
Installation
------------

Using ``pip`` package manager:
Using ``pip`` package manager (requires pip 1.8+):

.. code:: bash
Expand Down Expand Up @@ -89,16 +90,12 @@ Basic mocking:
@pook.activate
def test_my_api():
mock = pook.get('http://twitter.com/api/1/foobar',
type='json',
json={'error': 'not found'})
mock.reply(404, json={'error': 'foo'})
mock = pook.get('http://twitter.com/api/1/foobar', reply=404, response_json={'error': 'foo'})
resp = requests.get('http://twitter.com/api/1/foobar')
assert resp.status_code == 404
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"}'
assert mock._matches == 1
Using the chainable API DSL:

Expand All @@ -115,10 +112,7 @@ Using the chainable API DSL:
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"}'
assert mock._matches == 1
Using the decorator:

Expand Down Expand Up @@ -263,7 +257,8 @@ MIT - Tomas Aparicio
.. _urllib3: https://github.com/shazow/urllib3
.. _urllib: https://docs.python.org/3/library/urllib.html
.. _http.client: https://docs.python.org/3/library/http.client.html
.. _pycurl: http://pycurl.io/
.. _pycurl: http://pycurl.io
.. _0.1.3: https://github.com/h2non/pook/milestone/3

.. |Build Status| image:: https://travis-ci.org/h2non/pook.svg?branch=master
:target: https://travis-ci.org/h2non/pook
Expand Down
4 changes: 2 additions & 2 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ What HTTP clients are supported?
Please, see `supported HTTP clients`_ section.


.. _supported HTTP clients: <index.html#supported-http-clients>
.. _supported HTTP clients: index.html#supported-http-clients

.. _how it works: <how_it_works.html>
.. _how it works: how_it_works.html


Does ``pook`` mock out all the outgoing HTTP traffic from my app?
Expand Down
78 changes: 78 additions & 0 deletions pook/matchers/compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import re
import functools
from ..types import isregex, isregex_expr, strip_regex

# Negate is used a reserved token identifier to negate matching
NEGATE = '!!'


def compile(expr):
try:
return re.compile(expr, re.IGNORECASE)
except:
pass


def match(expr, value):
regex = compile(expr)
if not regex:
return False
return regex.match(value) is not None


def strip_negate(value):
return value[len(NEGATE):].lstrip()


def comparison(fn):
"""
Decorator function for comparison.
Arguments:
fn (function): target function to decorate.
Returns:
function
"""
@functools.wraps(fn)
def tester(expr, value):
# If no expression value to test, pass the test
if not expr:
return True

# If no value to match against, fail the test
if expr and not value:
return False

# If string instance
negate = False
if isinstance(expr, str):
negate = str.startswith(expr, NEGATE)
if negate:
expr = strip_negate(expr)

result = fn(expr, value)
return not result if negate else result
return tester


@comparison
def compare(expr, value):
"""
Compares an string or regular expression againast a given value.
Arguments:
expr (str|regex): string or regular expression value to compare.
value (str): value to compare against to.
Returns:
bool
"""
# Try with RegExp matching
if isregex(expr):
if isregex_expr(expr):
expr = strip_regex(expr)
return re.match(expr, value)

# Strict comparison equality
return expr == value

0 comments on commit a8f81f6

Please sign in to comment.