How to use the pook.on function in pook

To help you get started, we’ve selected a few pook examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github h2non / pook / tests / integration / engines / unittest_suite.py View on Github external
    @pook.on
    def test_no_match_exception(self):
        pook.get('server.com/bar', reply=204)
        try:
            requests.get('http://server.com/baz')
        except Exception:
            pass
        else:
            raise RuntimeError('expected to fail')
github h2non / pook / tests / integration / engines / unittest_suite.py View on Github external
    @pook.on
    def test_simple_pook_request(self):
        pook.get('server.com/foo').reply(204)
        res = requests.get('http://server.com/foo')
        self.assertEqual(res.status_code, 204)
github h2non / pook / tests / integration / engines / nose_suite.py View on Github external
@pook.on
def test_enable_engine():
    pook.get('server.com/foo').reply(204)
    res = requests.get('http://server.com/foo')
    assert res.status_code == 204
github h2non / pook / examples / pytest_example.py View on Github external
@pook.on
def test_enable_engine():
    pook.get('server.com/foo').reply(204)
    res = requests.get('http://server.com/foo')
    assert res.status_code == 204
    pook.disable()
github h2non / pook / examples / match_callback.py View on Github external
import pook
import requests


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'},
         response_json={'error': 'not found'},
         callback=on_match)

res = requests.get('http://httpbin.org/ip')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.json())

print('Is done:', pook.isdone())
print('Pending mocks:', pook.pending_mocks())
print('Unmatched requests:', pook.unmatched_requests())
github h2non / pook / examples / chainable_api.py View on Github external
import json
import pook
import requests


# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
    .json({'foo': 'bar'})
    .type('json')
    .header('Client', 'requests')
    .reply(204)
    .headers({'server': 'pook'})
    .json({'error': 'simulated'}))

res = requests.post('http://httpbin.org/post',
                    data=json.dumps({'foo': 'bar'}),
                    headers={'Client': 'requests',
                             'Content-Type': 'application/json'})

print('Status:', res.status_code)
print('Body:', res.json())
github h2non / pook / examples / regex.py View on Github external
import pook
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'))
    .header('Client', pook.regex('requests|pook'))
    .times(2)
    .reply(200)
    .headers({'server': 'pook'})
    .json({'foo': 'bar'}))

# Perform request
res = requests.get('http://httpbin.org/foo/bar/baz',
                   headers={'Client': 'requests'})
print('Status:', res.status_code)
print('Body:', res.json())
github h2non / pook / examples / json_matching.py View on Github external
import json
import pook
import requests

# Enable mock engine
pook.on()

(pook.post('httpbin.org/post')
    .json({'foo': 'bar'})
    .reply(204)
    .json({'error': 'simulated'}))

res = requests.post('http://httpbin.org/post',
                    data=json.dumps({'foo': 'bar'}))
print('Status:', res.status_code)
print('Body:', res.json())

print('Is done:', pook.isdone())
print('Pending mocks:', pook.pending_mocks())
github h2non / pook / examples / requests_client.py View on Github external
import pook
import requests


# Enable mock engine
pook.on()

pook.get('httpbin.org/ip',
         reply=403, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

pook.get('httpbin.org/headers',
         reply=404, response_type='json',
         response_headers={'server': 'pook'},
         response_json={'error': 'not found'})

res = requests.get('http://httpbin.org/ip')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.json())
github h2non / pook / examples / simulated_error.py View on Github external
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'))

try:
    requests.get('http://httpbin.org/status/500')
except Exception as err:
    print('Error:', err)