2015-01-14 02:26:24 +02: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/>.
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
Base code use for all API tests
|
|
|
|
"""
|
2015-01-14 02:26:24 +02:00
|
|
|
|
|
|
|
import json
|
2020-10-02 09:37:50 +03:00
|
|
|
import pytest
|
2015-01-20 03:30:57 +02:00
|
|
|
|
2015-01-14 02:26:24 +02:00
|
|
|
|
|
|
|
class Query:
|
2016-03-07 16:01:35 +02:00
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
Helper to make queries against the test server
|
2016-03-07 16:01:35 +02:00
|
|
|
"""
|
2015-01-20 14:24:00 +02:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
def __init__(self, http_client, prefix='', api_version=None):
|
2016-03-07 16:01:35 +02:00
|
|
|
"""
|
2016-04-15 18:57:06 +03:00
|
|
|
:param prefix: Prefix added before path (ex: /compute)
|
2020-06-16 07:29:03 +03:00
|
|
|
:param api_version: Version of the API
|
2016-03-07 16:01:35 +02:00
|
|
|
"""
|
2020-06-16 07:29:03 +03:00
|
|
|
|
|
|
|
self._http_client = http_client
|
2016-03-07 16:01:35 +02:00
|
|
|
self._prefix = prefix
|
|
|
|
self._api_version = api_version
|
2015-01-14 02:26:24 +02:00
|
|
|
|
2015-01-21 18:11:21 +02:00
|
|
|
def post(self, path, body={}, **kwargs):
|
2020-06-16 07:29:03 +03:00
|
|
|
return self._request("POST", path, body, **kwargs)
|
2015-01-14 02:26:24 +02:00
|
|
|
|
2015-01-21 22:46:16 +02:00
|
|
|
def put(self, path, body={}, **kwargs):
|
2020-06-16 07:29:03 +03:00
|
|
|
return self._request("PUT", path, body, **kwargs)
|
2015-01-21 22:46:16 +02:00
|
|
|
|
2015-01-14 02:26:24 +02:00
|
|
|
def get(self, path, **kwargs):
|
2020-06-16 07:29:03 +03:00
|
|
|
return self._request("GET", path, **kwargs)
|
2015-01-14 02:26:24 +02:00
|
|
|
|
2015-01-16 22:39:58 +02:00
|
|
|
def delete(self, path, **kwargs):
|
2020-06-16 07:29:03 +03:00
|
|
|
return self._request("DELETE", path, **kwargs)
|
|
|
|
|
|
|
|
def patch(self, path, **kwargs):
|
|
|
|
return self._request("PATCH", path, **kwargs)
|
2015-01-16 22:39:58 +02:00
|
|
|
|
2016-03-07 16:01:35 +02:00
|
|
|
def get_url(self, path):
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2016-03-07 16:01:35 +02:00
|
|
|
if self._api_version is None:
|
2020-06-16 07:29:03 +03:00
|
|
|
return "/{}{}".format(self._prefix, path)
|
|
|
|
return "/v{}{}{}".format(self._api_version, self._prefix, path)
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
@pytest.mark.asyncio
|
2020-06-16 07:29:03 +03:00
|
|
|
async def _request(self, method, path, body=None, raw=False, **kwargs):
|
|
|
|
|
|
|
|
if body is not None and raw is False:
|
2015-01-14 02:26:24 +02:00
|
|
|
body = json.dumps(body)
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
async with self._http_client as ac:
|
|
|
|
response = await ac.request(method, self.get_url(path), data=body, **kwargs)
|
|
|
|
#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, "")
|
2018-10-17 13:32:10 +03:00
|
|
|
|
2020-06-16 07:29:03 +03:00
|
|
|
#response.json = {}
|
|
|
|
#response.html = ""
|
2020-10-02 09:37:50 +03:00
|
|
|
if response.content is not None:
|
|
|
|
if response.headers.get("content-type") == "application/json":
|
2018-10-17 13:32:10 +03:00
|
|
|
try:
|
2020-10-02 09:37:50 +03:00
|
|
|
response.json = response.json(encoding="utf-8")
|
2018-10-17 13:32:10 +03:00
|
|
|
except ValueError:
|
|
|
|
response.json = None
|
2020-10-02 09:37:50 +03:00
|
|
|
# else:
|
|
|
|
# try:
|
|
|
|
# response.html = response.text
|
|
|
|
# except UnicodeDecodeError:
|
|
|
|
# response.html = None
|
2018-10-17 13:32:10 +03:00
|
|
|
return response
|