2021-04-30 15:08:27 +02:00
|
|
|
|
from unittest import mock
|
2015-02-08 17:17:35 +01:00
|
|
|
|
from pytest import raises
|
2019-09-03 17:14:39 +02:00
|
|
|
|
from requests import Request
|
2015-02-08 17:17:35 +01:00
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
|
2019-09-16 13:26:18 +02:00
|
|
|
|
from httpie.status import ExitStatus
|
2021-05-05 14:13:39 +02:00
|
|
|
|
from .utils import HTTP_OK, http
|
2019-08-29 09:39:19 +02:00
|
|
|
|
|
2015-02-08 17:17:35 +01:00
|
|
|
|
|
2019-09-03 17:14:39 +02:00
|
|
|
|
@mock.patch('httpie.core.program')
|
2019-12-02 17:43:16 +01:00
|
|
|
|
def test_error(program):
|
2015-02-08 17:17:35 +01:00
|
|
|
|
exc = ConnectionError('Connection aborted')
|
|
|
|
|
exc.request = Request(method='GET', url='http://www.google.com')
|
2019-12-02 17:43:16 +01:00
|
|
|
|
program.side_effect = exc
|
|
|
|
|
r = http('www.google.com', tolerate_error_exit_status=True)
|
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
2019-12-02 17:46:40 +01:00
|
|
|
|
error_msg = (
|
|
|
|
|
'ConnectionError: '
|
|
|
|
|
'Connection aborted while doing a GET request to URL: '
|
|
|
|
|
'http://www.google.com'
|
|
|
|
|
)
|
|
|
|
|
assert error_msg in r.stderr
|
2015-02-08 17:17:35 +01:00
|
|
|
|
|
|
|
|
|
|
2019-09-03 17:14:39 +02:00
|
|
|
|
@mock.patch('httpie.core.program')
|
2019-12-02 17:43:16 +01:00
|
|
|
|
def test_error_traceback(program):
|
2015-02-08 17:17:35 +01:00
|
|
|
|
exc = ConnectionError('Connection aborted')
|
|
|
|
|
exc.request = Request(method='GET', url='http://www.google.com')
|
2019-12-02 17:43:16 +01:00
|
|
|
|
program.side_effect = exc
|
2015-02-08 17:17:35 +01:00
|
|
|
|
with raises(ConnectionError):
|
2019-12-02 17:43:16 +01:00
|
|
|
|
http('--traceback', 'www.google.com')
|
2015-02-08 17:17:35 +01:00
|
|
|
|
|
|
|
|
|
|
2019-08-29 09:39:19 +02:00
|
|
|
|
def test_max_headers_limit(httpbin_both):
|
|
|
|
|
with raises(ConnectionError) as e:
|
|
|
|
|
http('--max-headers=1', httpbin_both + '/get')
|
|
|
|
|
assert 'got more than 1 headers' in str(e.value)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_max_headers_no_limit(httpbin_both):
|
|
|
|
|
assert HTTP_OK in http('--max-headers=0', httpbin_both + '/get')
|
2021-09-29 20:22:19 +02:00
|
|
|
|
|
|
|
|
|
|
2021-10-06 17:27:07 +02:00
|
|
|
|
def test_response_charset_option_unknown_encoding(httpbin_both):
|
|
|
|
|
r = http(
|
|
|
|
|
'--response-charset=foobar',
|
|
|
|
|
httpbin_both + '/get',
|
|
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
|
)
|
|
|
|
|
assert "'foobar' is not a supported encoding" in r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_response_mime_option_invalid_mime_type(httpbin_both):
|
|
|
|
|
r = http(
|
|
|
|
|
'--response-mime=foobar',
|
|
|
|
|
httpbin_both + '/get',
|
|
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
|
)
|
|
|
|
|
assert "'foobar' doesn’t look like a mime type" in r.stderr
|