fixed unit tests

This commit is contained in:
Yun Xu 2018-02-27 22:25:38 -08:00
parent c39ddd00d3
commit d1a8e8b042
3 changed files with 53 additions and 24 deletions

View File

@ -59,7 +59,9 @@ class SanicTestClient:
@self.app.exception(MethodNotSupported)
async def error_handler(request, exception):
if request.method in ['HEAD', 'PATCH', 'PUT', 'DELETE']:
return text('', exception.status_code, headers=exception.headers)
return text(
'', exception.status_code, headers=exception.headers
)
else:
return self.app.error_handler.default(request, exception)

View File

@ -15,6 +15,7 @@ class ReuseableTCPConnector(TCPConnector):
super(ReuseableTCPConnector, self).__init__(*args, **kwargs)
self.old_proto = None
if aiohttp.__version__ >= '3.0':
@asyncio.coroutine
def connect(self, req, traces=None):
new_conn = yield from super(ReuseableTCPConnector, self)\
@ -26,6 +27,18 @@ class ReuseableTCPConnector(TCPConnector):
print(new_conn.__dict__)
self.old_proto = new_conn._protocol
return new_conn
else:
@asyncio.coroutine
def connect(self, req):
new_conn = yield from super(ReuseableTCPConnector, self)\
.connect(req)
if self.old_proto is not None:
if self.old_proto != new_conn._protocol:
raise RuntimeError(
"We got a new connection, wanted the same one!")
print(new_conn.__dict__)
self.old_proto = new_conn._protocol
return new_conn
class ReuseableSanicTestClient(SanicTestClient):

View File

@ -80,6 +80,7 @@ class DelayableTCPConnector(TCPConnector):
self._post_connect_delay = _post_connect_delay
self._pre_request_delay = _pre_request_delay
if aiohttp.__version__ >= '3.0':
@asyncio.coroutine
def connect(self, req, traces=None):
d_req = DelayableTCPConnector.\
@ -92,6 +93,19 @@ class DelayableTCPConnector(TCPConnector):
t = req.loop.time()
print("Connected at {}".format(t), flush=True)
return conn
else:
@asyncio.coroutine
def connect(self, req):
d_req = DelayableTCPConnector.\
RequestContextManager(req, self._pre_request_delay)
conn = yield from super(DelayableTCPConnector, self).connect(req)
if self._post_connect_delay and self._post_connect_delay > 0:
_ = yield from asyncio.sleep(self._post_connect_delay,
loop=self._loop)
req.send = d_req.send
t = req.loop.time()
print("Connected at {}".format(t), flush=True)
return conn
class DelayableSanicTestClient(SanicTestClient):