Skip to content

Commit

Permalink
refactor(examples)
Browse files Browse the repository at this point in the history
  • Loading branch information
h2non committed Nov 21, 2016
1 parent 4b80df9 commit a9b82f2
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
27 changes: 27 additions & 0 deletions examples/aiohttp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import aiohttp
import asyncio
import async_timeout
import pook


async def fetch(session, url, data):
with async_timeout.timeout(10):
async with session.get(url, data=data) as res:
print('Status:', res.status)
print('Headers:', res.headers)
print('Body:', await res.text())


with pook.use(network=True):
pook.get('http://httpbin.org/ip',
reply=404, response_type='json',
response_headers={'Server': 'nginx'},
response_json={'error': 'not found'})

async def main(loop):
async with aiohttp.ClientSession(loop=loop) as session:
await fetch(session, 'http://httpbin.org/ip',
bytearray('foo bar', 'utf-8'))

loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
24 changes: 24 additions & 0 deletions examples/requests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import requests
import pook


# Use context
with pook.use():
def on_match(request, mock):
print('On match:', request, mock)

pook.get('http://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())
Empty file added examples/urllib3.py
Empty file.

0 comments on commit a9b82f2

Please sign in to comment.