2015-02-08 17:17:35 +01:00
|
|
|
import mock
|
|
|
|
from pytest import raises
|
|
|
|
from requests import Request, Timeout
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
2016-03-01 14:10:54 +01:00
|
|
|
from httpie import ExitStatus
|
2015-02-08 17:17:35 +01:00
|
|
|
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
|
2016-03-01 14:10:54 +01:00
|
|
|
ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error)
|
|
|
|
assert ret == ExitStatus.ERROR
|
2015-02-08 17:17:35 +01:00
|
|
|
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):
|
2016-03-04 18:42:13 +01:00
|
|
|
main(['--ignore-stdin', '--traceback', 'www.google.com'])
|
2015-02-08 17:17:35 +01:00
|
|
|
|
|
|
|
|
|
|
|
@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
|
2016-03-01 14:10:54 +01:00
|
|
|
ret = main(['--ignore-stdin', 'www.google.com'], custom_log_error=error)
|
|
|
|
assert ret == ExitStatus.ERROR_TIMEOUT
|
2015-02-08 17:17:35 +01:00
|
|
|
assert error_msg == 'Request timed out (30s).'
|