Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
@pook.get('server.com/foo', reply=204)
def test_decorator(self):
res = requests.get('http://server.com/foo')
self.assertEqual(res.status_code, 204)
def test_pook_engine():
url = 'http://twitter.com/api/1/foobar'
status = 404
response_json = {'error': 'foo'}
mock = pook.get(
url,
headers={'content-type': 'application/json'},
reply=status,
response_json=response_json,
)
mock.persist()
requests.get(url)
assert mock.calls == 1
resp = requests.get(url)
assert resp.status_code == status
assert resp.json() == response_json
assert mock.calls == 2
def test_enable_engine():
pook.get('server.com/foo').reply(204)
res = requests.get('http://server.com/foo')
assert res.status_code == 204
def test_context_manager():
with pook.use():
pook.get('server.com/baz', reply=204)
res = requests.get('http://server.com/baz')
assert res.status_code == 204
# -*- coding: utf-8 -*-
import pook
import requests
from riprova import retry
# Define HTTP mocks
pook.get('server.com').times(3).reply(503)
pook.get('server.com').times(1).reply(200).json({'hello': 'world'})
# Retry evaluator function used to determine if the operated failed or not
def evaluator(response):
if response.status != 200:
return Exception('failed request')
return False
# On retry even subcriptor
def on_retry(err, next_try):
print('Operation error {}'.format(err))
print('Next try in {}ms'.format(next_try))
import sys, os
sys.path.append(os.path.dirname(__name__))
import requests
import pook
# from requests.exceptions import ConnectionError, HTTPError
if __name__ == '__main__':
m = pook.get('http://httpbin.org/ip?foo=bar')
m.type('application/json')
m.type('application/xml')
m.reply(404).json({'error': 'not found'})
# Testing
pook.activate()
res = requests.get('http://httpbin.org/ip?foo=bar&baz=foo')
print('Status:', res.status_code)
print('Headers:', res.headers)
print('Body:', res.text)
# res = requests.get('http://httpbin.org/ip?foo=bar')
print('Mock:', m)
pook.disable()
# res = requests.get('http://httpbin.org/ip')
@pook.get('http://httpbin.org/status/400', reply=200, persist=True)
def fetch(url):
return requests.get(url)
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)
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())
res = requests.get('http://httpbin.org/headers')
print('Status:', res.status_code)
import pook
import requests
with pook.context():
pook.get('httpbin.org/ip', reply=403,
response_headers={'pepe': 'lopez'},
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())
print('Is done:', pook.isdone())
print('Pending mocks:', pook.pending_mocks())
print('Unmatched requests:', pook.unmatched_requests())