Skip to content

Commit

Permalink
refactor(examples): enable pook engine per example file
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Mar 18, 2017
1 parent 279098d commit 856db99
Show file tree
Hide file tree
Showing 17 changed files with 51 additions and 3 deletions.
4 changes: 4 additions & 0 deletions examples/README.rst
Expand Up @@ -3,11 +3,15 @@

Run examples via:

.. code:: python
$ python examples/<example>.py
Example:

.. code:: python
$ python examples/requests_client.py
Expand Down
3 changes: 3 additions & 0 deletions examples/chainable_api.py
Expand Up @@ -3,6 +3,9 @@
import requests


# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
.json({'foo': 'bar'})
.type('json')
Expand Down
3 changes: 3 additions & 0 deletions examples/http_client_native.py
Expand Up @@ -2,6 +2,9 @@
import pook


# Enable mock engine
pook.on()

mock = pook.get('http://httpbin.org/ip',
reply=404, response_type='json',
response_json={'error': 'not found'})
Expand Down
2 changes: 2 additions & 0 deletions examples/json_matching.py
Expand Up @@ -2,6 +2,8 @@
import pook
import requests

# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
.json({'foo': 'bar'})
Expand Down
3 changes: 3 additions & 0 deletions examples/json_schema.py
Expand Up @@ -10,6 +10,9 @@
}
}

# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
.jsonschema(schema)
.reply(204)
Expand Down
3 changes: 3 additions & 0 deletions examples/match_callback.py
Expand Up @@ -6,6 +6,9 @@ def on_match(request, mock):
print('On match:', request, mock)


# Enable mock engine
pook.on()

pook.get('httpbin.org/ip',
reply=403, response_type='json',
response_headers={'pepe': 'lopez'},
Expand Down
2 changes: 1 addition & 1 deletion examples/mocket_example.py
Expand Up @@ -8,7 +8,7 @@
# Use mocket library as underlying mock engine
pook.set_mock_engine(MocketEngine)

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

# Target server URL to mock out
Expand Down
3 changes: 3 additions & 0 deletions examples/network_mode.py
Expand Up @@ -2,6 +2,9 @@
import requests


# Enable mock engine
pook.on()

# Enable network mode
pook.enable_network()

Expand Down
2 changes: 2 additions & 0 deletions examples/nose_example.py
Expand Up @@ -11,6 +11,7 @@ def test_simple_pook_request():
assert res.status_code == 204


@pook.on
def test_enable_engine():
pook.get('server.com/foo').reply(204)
res = requests.get('http://server.com/foo')
Expand All @@ -30,6 +31,7 @@ def test_context_manager():
assert res.status_code == 204


@pook.on
def test_no_match_exception():
pook.get('server.com/bar', reply=204)
try:
Expand Down
3 changes: 3 additions & 0 deletions examples/persistent_mock.py
Expand Up @@ -2,6 +2,9 @@
import requests


# Enable mock engine
pook.on()

(pook.get('httpbin.org')
.headers({'Client': 'requests'})
.persist()
Expand Down
2 changes: 2 additions & 0 deletions examples/pytest_example.py
Expand Up @@ -12,6 +12,7 @@ def test_simple_pook_request():
assert res.status_code == 204


@pook.on
def test_enable_engine():
pook.get('server.com/foo').reply(204)
res = requests.get('http://server.com/foo')
Expand All @@ -32,6 +33,7 @@ def test_context_manager():
assert res.status_code == 204


@pook.on
def test_no_match_exception():
pook.get('server.com/bar', reply=204)
with pytest.raises(Exception):
Expand Down
3 changes: 3 additions & 0 deletions examples/regex.py
Expand Up @@ -2,6 +2,9 @@
import requests


# Enable mock engine
pook.on()

# Mock definition based
(pook.get(pook.regex('h[t]{2}pbin.*'))
.path(pook.regex('/foo/[a-z]+/baz'))
Expand Down
3 changes: 3 additions & 0 deletions examples/requests_client.py
Expand Up @@ -2,6 +2,9 @@
import requests


# Enable mock engine
pook.on()

pook.get('httpbin.org/ip',
reply=403, response_type='json',
response_headers={'server': 'pook'},
Expand Down
4 changes: 4 additions & 0 deletions examples/simulated_error.py
@@ -1,6 +1,10 @@
import pook
import requests


# Enable mock engine
pook.on()

# Simulated error exception on request matching
pook.get('httpbin.org/status/500', error=Exception('simulated error'))

Expand Down
4 changes: 4 additions & 0 deletions examples/time_ttl_mock.py
@@ -1,6 +1,10 @@
import pook
import requests


# Enable mock engine
pook.on()

# Declare mock
(pook.get('httpbin.org')
.times(2)
Expand Down
3 changes: 2 additions & 1 deletion examples/unittest_example.py
Expand Up @@ -5,7 +5,7 @@

class TestUnitTestEngine(unittest.TestCase):

@pook.activate
@pook.on
def test_request(self):
pook.get('server.com/foo').reply(204)
res = requests.get('http://server.com/foo')
Expand All @@ -17,6 +17,7 @@ def test_request_with_context_manager(self):
res = requests.get('http://server.com/bar')
self.assertEqual(res.status_code, 204)

@pook.on
def test_no_match_exception(self):
pook.get('server.com/bar', reply=204)
try:
Expand Down
7 changes: 6 additions & 1 deletion pook/engine.py
Expand Up @@ -426,7 +426,12 @@ def match(self, request):

# Validate that we have a mock
if not self.should_use_network(request):
msg = 'Cannot match mock for request:\n{}'.format(request)
msg = 'pook error!\n\n'

msg += (
'=> Cannot match any mock for the '
'following request:\n{}'.format(request)
)

# Compose unmatch error details, if debug mode is enabled
if self.debug:
Expand Down

0 comments on commit 856db99

Please sign in to comment.