mirror of
https://github.com/httpie/cli.git
synced 2024-11-28 08:38:44 +02:00
Increase test coverage for error handling
This commit is contained in:
parent
7f8adad313
commit
f855de16c2
@ -67,7 +67,7 @@ def decode_args(args, stdin_encoding):
|
||||
]
|
||||
|
||||
|
||||
def main(args=sys.argv[1:], env=Environment()):
|
||||
def main(args=sys.argv[1:], env=Environment(), error=None):
|
||||
"""Run the main program and write the output to ``env.stdout``.
|
||||
|
||||
Return exit status code.
|
||||
@ -81,11 +81,14 @@ def main(args=sys.argv[1:], env=Environment()):
|
||||
if env.config.default_options:
|
||||
args = env.config.default_options + args
|
||||
|
||||
def error(msg, *args, **kwargs):
|
||||
def _error(msg, *args, **kwargs):
|
||||
msg = msg % args
|
||||
level = kwargs.get('level', 'error')
|
||||
env.stderr.write('\nhttp: %s: %s\n' % (level, msg))
|
||||
|
||||
if error is None:
|
||||
error = _error
|
||||
|
||||
debug = '--debug' in args
|
||||
traceback = debug or '--traceback' in args
|
||||
exit_status = ExitStatus.OK
|
||||
|
@ -1,4 +1,5 @@
|
||||
tox
|
||||
mock
|
||||
pytest
|
||||
pytest-cov
|
||||
pytest-httpbin
|
||||
|
48
tests/test_errors.py
Normal file
48
tests/test_errors.py
Normal file
@ -0,0 +1,48 @@
|
||||
import mock
|
||||
from pytest import raises
|
||||
from requests import Request, Timeout
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
from httpie.core import main
|
||||
|
||||
error_msg = None
|
||||
|
||||
|
||||
@mock.patch('httpie.core.get_response')
|
||||
def test_error(get_response):
|
||||
def error(msg, *args, **kwargs):
|
||||
global error_msg
|
||||
error_msg = msg % args
|
||||
|
||||
exc = ConnectionError('Connection aborted')
|
||||
exc.request = Request(method='GET', url='http://www.google.com')
|
||||
get_response.side_effect = exc
|
||||
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
|
||||
assert ret == 1
|
||||
assert error_msg == (
|
||||
'ConnectionError: '
|
||||
'Connection aborted while doing GET request to URL: '
|
||||
'http://www.google.com')
|
||||
|
||||
|
||||
@mock.patch('httpie.core.get_response')
|
||||
def test_error_traceback(get_response):
|
||||
exc = ConnectionError('Connection aborted')
|
||||
exc.request = Request(method='GET', url='http://www.google.com')
|
||||
get_response.side_effect = exc
|
||||
with raises(ConnectionError):
|
||||
ret = main(['--ignore-stdin', '--traceback', 'www.google.com'])
|
||||
|
||||
|
||||
@mock.patch('httpie.core.get_response')
|
||||
def test_timeout(get_response):
|
||||
def error(msg, *args, **kwargs):
|
||||
global error_msg
|
||||
error_msg = msg % args
|
||||
|
||||
exc = Timeout('Request timed out')
|
||||
exc.request = Request(method='GET', url='http://www.google.com')
|
||||
get_response.side_effect = exc
|
||||
ret = main(['--ignore-stdin', 'www.google.com'], error=error)
|
||||
assert ret == 2
|
||||
assert error_msg == 'Request timed out (30s).'
|
Loading…
Reference in New Issue
Block a user