Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@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')
@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)
@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.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()
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())
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())
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())
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())
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())
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)