/usr/lib/python3/dist-packages/hug/test.py is in python3-hug 2.3.0-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | """hug/test.py.
Defines utility function that aid in the round-trip testing of Hug APIs
Copyright (C) 2016 Timothy Edmund Crosley
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
"""
from __future__ import absolute_import
import json
import sys
from functools import partial
from io import BytesIO
from unittest import mock
from urllib.parse import urlencode
from falcon import HTTP_METHODS
from falcon.testing import StartResponseMock, create_environ
from hug import output_format
from hug.api import API
def call(method, api_or_module, url, body='', headers=None, params=None, query_string='', scheme='http', **kwargs):
"""Simulates a round-trip call against the given API / URL"""
api = API(api_or_module).http.server()
response = StartResponseMock()
headers = {} if headers is None else headers
if not isinstance(body, str) and 'json' in headers.get('content-type', 'application/json'):
body = output_format.json(body)
headers.setdefault('content-type', 'application/json')
params = params if params else {}
params.update(kwargs)
if params:
query_string = '{}{}{}'.format(query_string, '&' if query_string else '', urlencode(params, True))
result = api(create_environ(path=url, method=method, headers=headers, query_string=query_string,
body=body, scheme=scheme), response)
if result:
try:
response.data = result[0].decode('utf8')
except TypeError:
response.data = []
for chunk in result:
response.data.append(chunk.decode('utf8'))
response.data = "".join(response.data)
except UnicodeDecodeError:
response.data = result[0]
response.content_type = response.headers_dict['content-type']
if response.content_type == 'application/json':
response.data = json.loads(response.data)
return response
for method in HTTP_METHODS:
tester = partial(call, method)
tester.__doc__ = """Simulates a round-trip HTTP {0} against the given API / URL""".format(method.upper())
globals()[method.lower()] = tester
def cli(method, *args, **arguments):
"""Simulates testing a hug cli method from the command line"""
collect_output = arguments.pop('collect_output', True)
command_args = [method.__name__] + list(args)
for name, values in arguments.items():
if not isinstance(values, (tuple, list)):
values = (values, )
for value in values:
command_args.append('--{0}'.format(name))
if not value in (True, False):
command_args.append('{0}'.format(value))
old_sys_argv = sys.argv
sys.argv = [str(part) for part in command_args]
old_output = method.interface.cli.output
if collect_output:
method.interface.cli.outputs = lambda data: to_return.append(data)
to_return = []
try:
method.interface.cli()
except Exception as e:
to_return = (e, )
method.interface.cli.output = old_output
sys.argv = old_sys_argv
return to_return and to_return[0] or None
|