2016-10-26 11:53:01 +02:00
|
|
|
import mock
|
|
|
|
|
2014-04-24 14:07:31 +02:00
|
|
|
from httpie import ExitStatus
|
2017-12-28 18:17:48 +01:00
|
|
|
from utils import MockEnvironment, http, HTTP_OK
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2016-10-26 11:53:01 +02:00
|
|
|
def test_keyboard_interrupt_during_arg_parsing_exit_status(httpbin):
|
2019-08-31 15:17:10 +02:00
|
|
|
with mock.patch('httpie.cli.definition.parser.parse_args',
|
2016-10-26 11:53:01 +02:00
|
|
|
side_effect=KeyboardInterrupt()):
|
2019-09-03 17:14:39 +02:00
|
|
|
r = http('GET', httpbin.url + '/get', tolerate_error_exit_status=True)
|
2016-10-26 11:53:01 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_CTRL_C
|
|
|
|
|
|
|
|
|
|
|
|
def test_keyboard_interrupt_in_program_exit_status(httpbin):
|
|
|
|
with mock.patch('httpie.core.program',
|
|
|
|
side_effect=KeyboardInterrupt()):
|
2019-09-03 17:14:39 +02:00
|
|
|
r = http('GET', httpbin.url + '/get', tolerate_error_exit_status=True)
|
2016-10-26 11:53:01 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_CTRL_C
|
|
|
|
|
|
|
|
|
2016-03-03 11:47:12 +01:00
|
|
|
def test_ok_response_exits_0(httpbin):
|
2016-10-26 11:53:01 +02:00
|
|
|
r = http('GET', httpbin.url + '/get')
|
2016-03-03 11:47:12 +01:00
|
|
|
assert HTTP_OK in r
|
2018-11-02 16:07:39 +01:00
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2016-03-03 11:47:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_error_response_exits_0_without_check_status(httpbin):
|
|
|
|
r = http('GET', httpbin.url + '/status/500')
|
2018-11-02 16:07:39 +01:00
|
|
|
assert '500 INTERNAL SERVER ERROR' in r
|
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2016-03-03 11:47:12 +01:00
|
|
|
assert not r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
def test_timeout_exit_status(httpbin):
|
|
|
|
|
2018-07-12 00:39:31 +02:00
|
|
|
r = http('--timeout=0.01', 'GET', httpbin.url + '/delay/0.5',
|
2019-09-03 17:14:39 +02:00
|
|
|
tolerate_error_exit_status=True)
|
2016-03-03 11:47:12 +01:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_TIMEOUT
|
|
|
|
|
|
|
|
|
|
|
|
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
|
|
|
|
httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdout_isatty=False)
|
2016-03-03 11:47:12 +01:00
|
|
|
r = http('--check-status', '--headers',
|
|
|
|
'GET', httpbin.url + '/status/301',
|
2019-09-03 17:14:39 +02:00
|
|
|
env=env, tolerate_error_exit_status=True)
|
2016-03-03 11:47:12 +01:00
|
|
|
assert '301 MOVED PERMANENTLY' in r
|
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_3XX
|
|
|
|
assert '301 moved permanently' in r.stderr.lower()
|
|
|
|
|
|
|
|
|
|
|
|
def test_3xx_check_status_redirects_allowed_exits_0(httpbin):
|
|
|
|
r = http('--check-status', '--follow',
|
|
|
|
'GET', httpbin.url + '/status/301',
|
2019-09-03 17:14:39 +02:00
|
|
|
tolerate_error_exit_status=True)
|
2016-03-03 11:47:12 +01:00
|
|
|
# The redirect will be followed so 200 is expected.
|
|
|
|
assert HTTP_OK in r
|
2018-11-02 16:07:39 +01:00
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2016-03-03 11:47:12 +01:00
|
|
|
|
|
|
|
|
|
|
|
def test_4xx_check_status_exits_4(httpbin):
|
|
|
|
r = http('--check-status', 'GET', httpbin.url + '/status/401',
|
2019-09-03 17:14:39 +02:00
|
|
|
tolerate_error_exit_status=True)
|
2016-03-03 11:47:12 +01:00
|
|
|
assert '401 UNAUTHORIZED' in r
|
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_4XX
|
|
|
|
# Also stderr should be empty since stdout isn't redirected.
|
|
|
|
assert not r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
def test_5xx_check_status_exits_5(httpbin):
|
|
|
|
r = http('--check-status', 'GET', httpbin.url + '/status/500',
|
2019-09-03 17:14:39 +02:00
|
|
|
tolerate_error_exit_status=True)
|
2016-03-03 11:47:12 +01:00
|
|
|
assert '500 INTERNAL SERVER ERROR' in r
|
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_5XX
|