1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/tests/utils.py

283 lines
8.0 KiB
Python
Raw Normal View History

2014-04-26 20:47:14 +03:00
# coding=utf-8
2016-03-01 17:22:50 +02:00
"""Utilities for HTTPie test suite."""
2014-04-24 15:07:31 +03:00
import os
import sys
import time
import json
import tempfile
from pathlib import Path
from typing import Optional, Union
2014-04-24 15:07:31 +03:00
2019-09-16 13:26:18 +02:00
from httpie.status import ExitStatus
from httpie.config import Config
from httpie.context import Environment
2014-04-24 15:07:31 +03:00
from httpie.core import main
TESTS_ROOT = Path(__file__).parent
2014-04-24 15:07:31 +03:00
CRLF = '\r\n'
2014-04-24 16:48:01 +03:00
COLOR = '\x1b['
HTTP_OK = '200 OK'
# noinspection GrazieInspection
2014-04-24 16:48:01 +03:00
HTTP_OK_COLOR = (
2014-04-24 15:07:31 +03:00
'HTTP\x1b[39m\x1b[38;5;245m/\x1b[39m\x1b'
'[38;5;37m1.1\x1b[39m\x1b[38;5;245m \x1b[39m\x1b[38;5;37m200'
'\x1b[39m\x1b[38;5;245m \x1b[39m\x1b[38;5;136mOK'
)
def mk_config_dir() -> Path:
2016-03-01 17:22:50 +02:00
dirname = tempfile.mkdtemp(prefix='httpie_config_')
return Path(dirname)
2014-04-24 16:48:01 +03:00
2014-04-25 14:10:01 +03:00
def add_auth(url, auth):
proto, rest = url.split('://', 1)
return proto + '://' + auth + '@' + rest
2014-04-24 15:07:31 +03:00
class MockEnvironment(Environment):
2016-03-01 17:22:50 +02:00
"""Environment subclass with reasonable defaults for testing."""
2014-04-24 15:07:31 +03:00
colors = 0
stdin_isatty = True,
stdout_isatty = True
is_windows = False
2014-04-24 16:48:01 +03:00
def __init__(self, create_temp_config_dir=True, **kwargs):
2014-04-24 15:07:31 +03:00
if 'stdout' not in kwargs:
2016-03-01 17:11:06 +02:00
kwargs['stdout'] = tempfile.TemporaryFile(
mode='w+b',
prefix='httpie_stdout'
)
2014-04-24 15:07:31 +03:00
if 'stderr' not in kwargs:
2016-03-01 17:11:06 +02:00
kwargs['stderr'] = tempfile.TemporaryFile(
mode='w+t',
prefix='httpie_stderr'
)
super().__init__(**kwargs)
self._create_temp_config_dir = create_temp_config_dir
2016-03-01 17:11:06 +02:00
self._delete_config_dir = False
self._temp_dir = Path(tempfile.gettempdir())
2016-03-01 17:11:06 +02:00
@property
def config(self) -> Config:
if (self._create_temp_config_dir
and self._temp_dir not in self.config_dir.parents):
self.create_temp_config_dir()
return super().config
2016-03-01 17:11:06 +02:00
def create_temp_config_dir(self):
self.config_dir = mk_config_dir()
self._delete_config_dir = True
2016-03-01 17:11:06 +02:00
def cleanup(self):
self.stdout.close()
self.stderr.close()
2016-03-01 17:11:06 +02:00
if self._delete_config_dir:
assert self._temp_dir in self.config_dir.parents
2016-03-01 17:11:06 +02:00
from shutil import rmtree
rmtree(self.config_dir, ignore_errors=True)
2014-04-24 15:07:31 +03:00
def __del__(self):
# noinspection PyBroadException
2016-03-01 17:11:06 +02:00
try:
self.cleanup()
except Exception:
pass
2014-04-24 15:07:31 +03:00
class BaseCLIResponse:
2016-03-01 17:22:50 +02:00
"""
Represents the result of simulated `$ http' invocation via `http()`.
2016-03-01 17:22:50 +02:00
Holds and provides access to:
- stdout output: print(self)
- stderr output: print(self.stderr)
- devnull output: print(self.devnull)
2016-03-01 17:22:50 +02:00
- exit_status output: print(self.exit_status)
"""
stderr: str = None
devnull: str = None
json: dict = None
exit_status: ExitStatus = None
2016-03-01 17:22:50 +02:00
class BytesCLIResponse(bytes, BaseCLIResponse):
"""
Used as a fallback when a StrCLIResponse cannot be used.
E.g. when the output contains binary data or when it is colorized.
`.json` will always be None.
"""
class StrCLIResponse(str, BaseCLIResponse):
@property
def json(self) -> Optional[dict]:
2016-03-01 17:22:50 +02:00
"""
Return deserialized the request or response JSON body,
if one (and only one) included in the output and is parsable.
2016-03-01 17:22:50 +02:00
"""
if not hasattr(self, '_json'):
self._json = None
# De-serialize JSON body if possible.
if COLOR in self:
# Colorized output cannot be parsed.
pass
elif self.strip().startswith('{'):
# Looks like JSON body.
self._json = json.loads(self)
elif (self.count('Content-Type:') == 1
and 'application/json' in self):
2016-03-01 17:22:50 +02:00
# Looks like a whole JSON HTTP message,
# try to extract its body.
try:
j = self.strip()[self.strip().rindex('\r\n\r\n'):]
except ValueError:
pass
else:
try:
# noinspection PyAttributeOutsideInit
2016-03-01 17:22:50 +02:00
self._json = json.loads(j)
except ValueError:
pass
return self._json
2016-03-02 07:31:23 +02:00
class ExitStatusError(Exception):
2016-03-02 07:16:41 +02:00
pass
def http(
*args,
program_name='http',
tolerate_error_exit_status=False,
**kwargs,
) -> Union[StrCLIResponse, BytesCLIResponse]:
2016-03-01 17:11:06 +02:00
# noinspection PyUnresolvedReferences
2014-04-24 15:07:31 +03:00
"""
2016-03-01 17:13:45 +02:00
Run HTTPie and capture stderr/out and exit status.
Content writtent to devnull will be captured if
env.devnull in env does not correspond to os.devnull file.
2014-04-25 13:18:35 +03:00
2016-03-01 17:13:45 +02:00
Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass instance.
2016-03-01 17:13:45 +02:00
The return value is either a `StrCLIResponse`, or `BytesCLIResponse`
if unable to decode the output.
2014-04-24 15:07:31 +03:00
2016-03-01 17:13:45 +02:00
The response has the following attributes:
2014-04-24 15:07:31 +03:00
2016-03-01 17:13:45 +02:00
`stdout` is represented by the instance itself (print r)
`stderr`: text written to stderr
`devnull` text written to devnull. String if possible bytes otherwise
2016-03-01 17:13:45 +02:00
`exit_status`: the exit status
`json`: decoded JSON (if possible) or `None`
2014-04-24 15:07:31 +03:00
2016-03-01 17:13:45 +02:00
Exceptions are propagated.
If you pass ``tolerate_error_exit_status=True``, then error exit statuses
2016-03-01 17:13:45 +02:00
won't result into an exception.
2016-03-01 17:13:45 +02:00
Example:
2014-04-24 15:07:31 +03:00
2016-03-01 17:13:45 +02:00
$ http --auth=user:password GET httpbin.org/basic-auth/user/password
2016-03-01 17:13:45 +02:00
>>> httpbin = getfixture('httpbin')
>>> r = http('-a', 'user:pw', httpbin.url + '/basic-auth/user/pw')
>>> type(r) == StrCLIResponse
True
>>> r.exit_status
<ExitStatus.SUCCESS: 0>
2016-03-01 17:13:45 +02:00
>>> r.stderr
''
>>> 'HTTP/1.1 200 OK' in r
True
>>> r.json == {'authenticated': True, 'user': 'user'}
True
2016-03-01 17:13:45 +02:00
"""
2014-04-24 15:07:31 +03:00
env = kwargs.get('env')
if not env:
env = kwargs['env'] = MockEnvironment()
2014-04-24 15:07:31 +03:00
stdout = env.stdout
stderr = env.stderr
devnull = env.devnull
2014-04-24 15:07:31 +03:00
2014-04-25 13:42:50 +03:00
args = list(args)
args_with_config_defaults = args + env.config.default_options
add_to_args = []
if '--debug' not in args_with_config_defaults:
if (not tolerate_error_exit_status
and '--traceback' not in args_with_config_defaults):
add_to_args.append('--traceback')
if not any('--timeout' in arg for arg in args_with_config_defaults):
add_to_args.append('--timeout=3')
complete_args = [program_name, *add_to_args, *args]
# print(' '.join(complete_args))
2014-04-25 13:42:50 +03:00
def dump_stderr():
stderr.seek(0)
sys.stderr.write(stderr.read())
2014-04-24 19:32:15 +03:00
try:
2014-04-24 15:07:31 +03:00
try:
exit_status = main(args=complete_args, **kwargs)
2014-04-24 15:07:31 +03:00
if '--download' in args:
# Let the progress reporter thread finish.
time.sleep(.5)
except SystemExit:
if tolerate_error_exit_status:
2016-03-02 07:16:41 +02:00
exit_status = ExitStatus.ERROR
else:
dump_stderr()
raise
2014-04-24 15:07:31 +03:00
except Exception:
stderr.seek(0)
2014-04-24 15:07:31 +03:00
sys.stderr.write(stderr.read())
raise
else:
if (not tolerate_error_exit_status
and exit_status != ExitStatus.SUCCESS):
dump_stderr()
2016-03-02 07:31:23 +02:00
raise ExitStatusError(
2016-03-02 07:16:41 +02:00
'httpie.core.main() unexpectedly returned'
f' a non-zero exit status: {exit_status}'
2016-03-02 07:16:41 +02:00
)
2014-04-24 15:07:31 +03:00
stdout.seek(0)
stderr.seek(0)
devnull.seek(0)
2014-04-24 15:07:31 +03:00
output = stdout.read()
devnull_output = devnull.read()
2014-04-24 15:07:31 +03:00
try:
2014-04-25 13:42:50 +03:00
output = output.decode('utf8')
2014-04-24 15:07:31 +03:00
except UnicodeDecodeError:
r = BytesCLIResponse(output)
2014-04-25 13:42:50 +03:00
else:
r = StrCLIResponse(output)
r.devnull = devnull_output
2014-04-24 15:07:31 +03:00
r.stderr = stderr.read()
r.exit_status = exit_status
if r.exit_status != ExitStatus.SUCCESS:
sys.stderr.write(r.stderr)
2014-04-24 15:07:31 +03:00
return r
finally:
devnull.close()
2014-04-24 15:07:31 +03:00
stdout.close()
stderr.close()
2016-03-01 17:11:06 +02:00
env.cleanup()