2014-04-24 14:07:31 +02:00
|
|
|
import requests
|
2014-04-24 15:17:23 +02:00
|
|
|
import pytest
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
from httpie import ExitStatus
|
2014-06-28 16:35:57 +02:00
|
|
|
from utils import TestEnvironment, http, HTTP_OK
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestExitStatus:
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_ok_response_exits_0(self, httpbin):
|
|
|
|
r = http('GET', httpbin.url + '/status/200')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.exit_status == ExitStatus.OK
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_error_response_exits_0_without_check_status(self, httpbin):
|
|
|
|
r = http('GET', httpbin.url + '/status/500')
|
|
|
|
assert '500 INTERNAL SERVER ERRO' in r
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.OK
|
|
|
|
assert not r.stderr
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-24 15:48:01 +02:00
|
|
|
@pytest.mark.skipif(
|
2014-05-17 22:24:44 +02:00
|
|
|
tuple(map(int, requests.__version__.split('.'))) < (2, 3, 0),
|
|
|
|
reason='timeout broken in requests prior v2.3.0 (#185)'
|
|
|
|
)
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_timeout_exit_status(self, httpbin):
|
2014-05-17 22:24:44 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--timeout=0.5', 'GET', httpbin.url + '/delay/1',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_TIMEOUT
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(
|
|
|
|
self, httpbin):
|
2014-04-24 15:48:01 +02:00
|
|
|
env = TestEnvironment(stdout_isatty=False)
|
2014-06-28 16:35:57 +02:00
|
|
|
r = http('--check-status', '--headers',
|
|
|
|
'GET', httpbin.url + '/status/301',
|
2014-04-26 15:06:51 +02:00
|
|
|
env=env, error_exit_ok=True)
|
2014-06-28 16:35:57 +02:00
|
|
|
assert '301 MOVED PERMANENTLY' in r
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_3XX
|
|
|
|
assert '301 moved permanently' in r.stderr.lower()
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-04-24 15:17:23 +02:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
requests.__version__ == '0.13.6',
|
|
|
|
reason='Redirects with prefetch=False are broken in Requests 0.13.6')
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_3xx_check_status_redirects_allowed_exits_0(self, httpbin):
|
|
|
|
r = http('--check-status', '--follow',
|
|
|
|
'GET', httpbin.url + '/status/301',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-04-24 14:07:31 +02:00
|
|
|
# The redirect will be followed so 200 is expected.
|
2014-06-28 16:35:57 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.OK
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_4xx_check_status_exits_4(self, httpbin):
|
|
|
|
r = http('--check-status', 'GET', httpbin.url + '/status/401',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-06-28 16:35:57 +02:00
|
|
|
assert '401 UNAUTHORIZED' in r
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_4XX
|
2014-04-24 14:07:31 +02:00
|
|
|
# Also stderr should be empty since stdout isn't redirected.
|
2014-04-24 14:58:15 +02:00
|
|
|
assert not r.stderr
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_5xx_check_status_exits_5(self, httpbin):
|
|
|
|
r = http('--check-status', 'GET', httpbin.url + '/status/500',
|
2014-04-26 15:06:51 +02:00
|
|
|
error_exit_ok=True)
|
2014-06-28 16:35:57 +02:00
|
|
|
assert '500 INTERNAL SERVER ERROR' in r
|
2014-04-24 14:58:15 +02:00
|
|
|
assert r.exit_status == ExitStatus.ERROR_HTTP_5XX
|