Skip to content

Commit 461b3fc

Browse files
authored
Merge pull request #36 from mocketize/asyncio
Asyncio and Aiohttp support
2 parents a557b33 + 7d9e23c commit 461b3fc

7 files changed

Lines changed: 63 additions & 8 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test-python:
1111

1212
lint-python:
1313
@echo "Linting Python files"
14-
flake8 --exit-zero --ignore=E501 --exclude=.git,compat.py mocket
14+
flake8 --exit-zero --ignore=E501,E731 --exclude=.git,compat.py mocket
1515
@echo ""
1616

1717
develop: install-dev-requirements install-test-requirements

mocket/mocket.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# coding=utf-8
22
from __future__ import unicode_literals
33
import socket
4-
from collections import defaultdict
5-
from io import BytesIO
6-
from .compat import encode_utf8, basestring, byte_type, text_type
74
import collections
5+
from io import BytesIO
6+
87
import decorator
98

9+
from .compat import encode_utf8, basestring, byte_type, text_type
10+
1011
__all__ = (
1112
'true_socket',
1213
'true_create_connection',
@@ -50,6 +51,7 @@ def __init__(self, family, type, proto=0):
5051
self._sock = self
5152
self._connected = False
5253
self._buflen = 1024
54+
self._entry = None
5355

5456
def setsockopt(self, level, optname, value):
5557
if self.true_socket:
@@ -72,8 +74,11 @@ def makefile(self, mode='r', bufsize=-1):
7274
self._bufsize = bufsize
7375
return self.fd
7476

77+
def get_entry(self, data):
78+
return Mocket.get_entry(self._host, self._port, data)
79+
7580
def sendall(self, data, *args, **kwargs):
76-
entry = Mocket.get_entry(self._host, self._port, data)
81+
entry = self.get_entry(data)
7782
if not entry:
7883
return self.true_sendall(data, *args, **kwargs)
7984
entry.collect(data)
@@ -103,14 +108,22 @@ def true_sendall(self, data, *args, **kwargs):
103108
break
104109
self.fd.seek(- written, 1)
105110

111+
def send(self, data, *args, **kwargs):
112+
entry = self.get_entry(data)
113+
if entry:
114+
if self._entry != entry:
115+
self.sendall(data, *args, **kwargs)
116+
self._entry = entry
117+
return len(data)
118+
106119
def __getattr__(self, name):
107120
# useful when clients call methods on real
108121
# socket we do not provide on the fake one
109122
return getattr(self.true_socket, name)
110123

111124

112125
class Mocket(object):
113-
_entries = defaultdict(list)
126+
_entries = collections.defaultdict(list)
114127
_requests = []
115128

116129
@classmethod
@@ -131,7 +144,7 @@ def collect(cls, data):
131144

132145
@classmethod
133146
def reset(cls):
134-
cls._entries = defaultdict(list)
147+
cls._entries = collections.defaultdict(list)
135148
cls._requests = []
136149

137150
@classmethod

runtests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,23 @@ def runtests(args=None):
88
if not args:
99
args = []
1010

11+
major, minor = sys.version_info[:2]
12+
13+
python35 = False
14+
15+
if major == 3 and minor >= 5:
16+
python35 = True
17+
18+
import pip
19+
pip.main(['install', 'aiohttp'])
20+
1121
if not any(a for a in args[1:] if not a.startswith('-')):
1222
args.append('tests')
1323
args.append('mocket')
1424

25+
if python35:
26+
args.append('tests35')
27+
1528
sys.exit(pytest.main(args))
1629

1730

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
[pytest]
1+
[tool:pytest]
22
python_files=test*.py
33
addopts=--doctest-modules --cov=mocket --cov-report=term-missing -v -x

tests/test_http.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
import json
55
import mock
66
from . import urlopen, urlencode, HTTPError
7+
78
import pytest
89
import requests
910
from unittest import TestCase
11+
1012
from mocket.mockhttp import Entry, Response
1113
from mocket.mocket import Mocket, mocketize
1214

tests35/__init__.py

Whitespace-only changes.

tests35/test_http_aiohttp.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import aiohttp
2+
import asyncio
3+
from unittest import TestCase
4+
5+
from mocket.mocket import mocketize
6+
from mocket.mockhttp import Entry
7+
8+
loop = asyncio.get_event_loop()
9+
10+
11+
class AioHttpEntryTestCase(TestCase):
12+
@mocketize
13+
def test_session(self):
14+
url = 'http://httpbin.org/ip'
15+
body = "asd" * 100
16+
Entry.single_register(Entry.GET, url, body=body, status=404)
17+
Entry.single_register(Entry.POST, url, body=body*2, status=201)
18+
19+
async def main(l):
20+
async with aiohttp.ClientSession(loop=l) as session:
21+
get_response = await session.get(url)
22+
post_response = await session.post(url, data=body*6)
23+
assert get_response.status == 404
24+
assert post_response.status == 201
25+
assert await get_response.text() == body
26+
assert await post_response.text() == body*2
27+
loop.run_until_complete(main(loop))

0 commit comments

Comments
 (0)