-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.