Fix tests to work with new version of aiohttp (>= 3.0)

This commit is contained in:
grossmj committed 2018-10-17 17:32:10 +07:00
1 parent 9ae9209881
commit ab8dc52ece
18 files changed
+218 -184

No files matched your search

+24 -23
View File
@@ -66,9 +66,9 @@ class Query:
"""
Return a websocket connected to the path
"""
self._session = aiohttp.ClientSession()
async def go_request(future):
self._session = aiohttp.ClientSession()
response = await self._session.ws_connect(self.get_url(path))
future.set_result(response)
future = asyncio.Future()
@@ -90,30 +90,31 @@ class Query:
body = json.dumps(body)
connector = aiohttp.TCPConnector()
response = await aiohttp.request(method, self.get_url(path), data=body, loop=self._loop, connector=connector)
response.body = await response.read()
x_route = response.headers.get('X-Route', None)
if x_route is not None:
response.route = x_route.replace("/v{}".format(self._api_version), "")
response.route = response.route .replace(self._prefix, "")
async with aiohttp.request(method, self.get_url(path), data=body, loop=self._loop, connector=connector) as response:
response.body = await response.read()
x_route = response.headers.get('X-Route', None)
if x_route is not None:
response.route = x_route.replace("/v{}".format(self._api_version), "")
response.route = response.route .replace(self._prefix, "")
response.json = {}
response.html = ""
if response.body is not None:
if response.headers.get("CONTENT-TYPE", "") == "application/json":
try:
response.json = json.loads(response.body.decode("utf-8"))
except ValueError:
response.json = None
else:
try:
response.html = response.body.decode("utf-8")
except UnicodeDecodeError:
response.html = None
response.json = {}
response.html = ""
if response.body is not None:
if response.headers.get("CONTENT-TYPE", "") == "application/json":
try:
response.json = json.loads(response.body.decode("utf-8"))
except ValueError:
response.json = None
else:
try:
response.html = response.body.decode("utf-8")
except UnicodeDecodeError:
response.html = None
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
self._dump_example(method, response.route, path, body, response)
return response
if kwargs.get('example') and os.environ.get("PYTEST_BUILD_DOCUMENTATION") == "1":
self._dump_example(method, response.route, path, body, response)
return response
return None
def _dump_example(self, method, route, path, body, response):
"""Dump the request for the documentation"""
+6 -10
View File
@@ -340,13 +340,12 @@ def test_stop_capture(http_controller, tmpdir, project, compute, async_run):
assert response.status == 201
def test_pcap(http_controller, tmpdir, project, compute, loop):
def test_pcap(http_controller, tmpdir, project, compute, async_run):
async def go(future):
response = await aiohttp.request("GET", http_controller.get_url("/projects/{}/links/{}/pcap".format(project.id, link.id)))
response.body = await response.content.read(5)
response.close()
future.set_result(response)
async def go():
async with aiohttp.request("GET", http_controller.get_url("/projects/{}/links/{}/pcap".format(project.id, link.id))) as response:
response.body = await response.content.read(5)
return response
link = Link(project)
link._capture_file_name = "test"
@@ -354,10 +353,7 @@ def test_pcap(http_controller, tmpdir, project, compute, loop):
with open(link.capture_file_path, "w+") as f:
f.write("hello")
project._links = {link.id: link}
future = asyncio.Future()
asyncio.ensure_future(go(future))
response = loop.run_until_complete(future)
response = async_run(asyncio.ensure_future(go()))
assert response.status == 200
assert b'hello' == response.body
@@ -172,12 +172,11 @@ def test_notification(http_controller, project, controller, loop, async_run):
async def go():
connector = aiohttp.TCPConnector()
response = await aiohttp.request("GET", http_controller.get_url("/projects/{project_id}/notifications".format(project_id=project.id)), connector=connector)
response.body = await response.content.read(200)
controller.notification.project_emit("node.created", {"a": "b"})
response.body += await response.content.readany()
response.close()
return response
async with aiohttp.request("GET", http_controller.get_url("/projects/{project_id}/notifications".format(project_id=project.id)), connector=connector) as response:
response.body = await response.content.read(200)
controller.notification.project_emit("node.created", {"a": "b"})
response.body += await response.content.readany()
return response
response = async_run(asyncio.ensure_future(go()))
assert response.status == 200
@@ -205,7 +204,7 @@ def test_notification_ws(http_controller, controller, project, async_run):
assert answer["action"] == "test"
async_run(http_controller.close())
ws.close()
async_run(ws.close())
assert project.status == "opened"