2015-04-20 14:57:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
# Copyright (C) 2015 GNS3 Technologies Inc.
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
This test suite check /files endpoint
|
|
|
|
"""
|
|
|
|
|
|
|
|
import json
|
|
|
|
import asyncio
|
|
|
|
import aiohttp
|
|
|
|
|
|
|
|
from gns3server.version import __version__
|
|
|
|
|
|
|
|
|
2016-03-07 14:01:35 +00:00
|
|
|
def test_stream(http_hypervisor, tmpdir, loop):
|
2015-04-20 14:57:03 +00:00
|
|
|
with open(str(tmpdir / "test"), 'w+') as f:
|
|
|
|
f.write("hello")
|
|
|
|
|
|
|
|
def go(future):
|
|
|
|
query = json.dumps({"location": str(tmpdir / "test")})
|
|
|
|
headers = {'content-type': 'application/json'}
|
2016-03-07 14:01:35 +00:00
|
|
|
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
|
2015-04-20 14:57:03 +00:00
|
|
|
response.body = yield from response.content.read(5)
|
|
|
|
with open(str(tmpdir / "test"), 'a') as f:
|
|
|
|
f.write("world")
|
|
|
|
response.body += yield from response.content.read(5)
|
|
|
|
response.close()
|
|
|
|
future.set_result(response)
|
|
|
|
|
|
|
|
future = asyncio.Future()
|
|
|
|
asyncio.async(go(future))
|
|
|
|
response = loop.run_until_complete(future)
|
|
|
|
assert response.status == 200
|
|
|
|
assert response.body == b'helloworld'
|
|
|
|
|
|
|
|
|
2016-03-07 14:01:35 +00:00
|
|
|
def test_stream_file_not_found(http_hypervisor, tmpdir, loop):
|
2015-04-20 14:57:03 +00:00
|
|
|
def go(future):
|
|
|
|
query = json.dumps({"location": str(tmpdir / "test")})
|
|
|
|
headers = {'content-type': 'application/json'}
|
2016-03-07 14:01:35 +00:00
|
|
|
response = yield from aiohttp.request("GET", http_hypervisor.get_url("/files/stream"), data=query, headers=headers)
|
2015-04-20 14:57:03 +00:00
|
|
|
response.close()
|
|
|
|
future.set_result(response)
|
|
|
|
|
|
|
|
future = asyncio.Future()
|
|
|
|
asyncio.async(go(future))
|
|
|
|
response = loop.run_until_complete(future)
|
|
|
|
assert response.status == 404
|