You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-07-17 01:42:46 +02:00
Added exit status constants, cleaned up main().
This commit is contained in:
@ -25,21 +25,26 @@ import json
|
||||
import argparse
|
||||
import tempfile
|
||||
import unittest
|
||||
try:
|
||||
from unittest import skipIf
|
||||
except ImportError:
|
||||
def skipIf(cond, test_method):
|
||||
if cond:
|
||||
return test_method
|
||||
return lambda self: None
|
||||
try:
|
||||
from urllib.request import urlopen
|
||||
except ImportError:
|
||||
from urllib2 import urlopen
|
||||
try:
|
||||
from unittest import skipIf
|
||||
except ImportError:
|
||||
|
||||
def skipIf(cond, reason):
|
||||
def decorator(test_method):
|
||||
if cond:
|
||||
return lambda self: None
|
||||
return test_method
|
||||
return decorator
|
||||
|
||||
|
||||
from requests.compat import is_windows, is_py26, bytes, str
|
||||
|
||||
|
||||
|
||||
#################################################################
|
||||
# Utils/setup
|
||||
#################################################################
|
||||
@ -48,6 +53,7 @@ from requests.compat import is_windows, is_py26, bytes, str
|
||||
TESTS_ROOT = os.path.abspath(os.path.dirname(__file__))
|
||||
sys.path.insert(0, os.path.realpath(os.path.join(TESTS_ROOT, '..')))
|
||||
|
||||
from httpie import EXIT
|
||||
from httpie import input
|
||||
from httpie.models import Environment
|
||||
from httpie.core import main
|
||||
@ -108,8 +114,10 @@ class TestEnvironment(Environment):
|
||||
super(TestEnvironment, self).__init__(**kwargs)
|
||||
|
||||
|
||||
class BytesResponse(bytes): pass
|
||||
class StrResponse(str): pass
|
||||
class BytesResponse(bytes):
|
||||
stderr = json = exit_status = None
|
||||
class StrResponse(str):
|
||||
stderr = json = exit_status = None
|
||||
|
||||
|
||||
def http(*args, **kwargs):
|
||||
@ -139,7 +147,7 @@ def http(*args, **kwargs):
|
||||
sys.stderr.write(env.stderr.read())
|
||||
raise
|
||||
except SystemExit:
|
||||
exit_status = 1
|
||||
exit_status = EXIT.ERROR
|
||||
|
||||
env.stdout.seek(0)
|
||||
env.stderr.seek(0)
|
||||
@ -792,7 +800,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
httpbin('/status/200')
|
||||
)
|
||||
self.assertIn(OK, r)
|
||||
self.assertEqual(r.exit_status, 0)
|
||||
self.assertEqual(r.exit_status, EXIT.OK)
|
||||
|
||||
def test_error_response_exits_0_without_check_status(self):
|
||||
r = http(
|
||||
@ -800,7 +808,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
httpbin('/status/500')
|
||||
)
|
||||
self.assertIn('HTTP/1.1 500', r)
|
||||
self.assertEqual(r.exit_status, 0)
|
||||
self.assertEqual(r.exit_status, EXIT.OK)
|
||||
|
||||
def test_3xx_check_status_exits_3_and_stderr_when_stdout_redirected(self):
|
||||
r = http(
|
||||
@ -811,7 +819,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
env=TestEnvironment(stdout_isatty=False,)
|
||||
)
|
||||
self.assertIn('HTTP/1.1 301', r)
|
||||
self.assertEqual(r.exit_status, 3)
|
||||
self.assertEqual(r.exit_status, EXIT.ERROR_HTTP_3XX)
|
||||
self.assertIn('301 moved permanently', r.stderr.lower())
|
||||
|
||||
def test_3xx_check_status_redirects_allowed_exits_0(self):
|
||||
@ -823,7 +831,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
)
|
||||
# The redirect will be followed so 200 is expected.
|
||||
self.assertIn('HTTP/1.1 200 OK', r)
|
||||
self.assertEqual(r.exit_status, 0)
|
||||
self.assertEqual(r.exit_status, EXIT.OK)
|
||||
|
||||
def test_4xx_check_status_exits_4(self):
|
||||
r = http(
|
||||
@ -832,7 +840,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
httpbin('/status/401')
|
||||
)
|
||||
self.assertIn('HTTP/1.1 401', r)
|
||||
self.assertEqual(r.exit_status, 4)
|
||||
self.assertEqual(r.exit_status, EXIT.ERROR_HTTP_4XX)
|
||||
# Also stderr should be empty since stdout isn't redirected.
|
||||
self.assertTrue(not r.stderr)
|
||||
|
||||
@ -843,7 +851,7 @@ class ExitStatusTest(BaseTestCase):
|
||||
httpbin('/status/500')
|
||||
)
|
||||
self.assertIn('HTTP/1.1 500', r)
|
||||
self.assertEqual(r.exit_status, 5)
|
||||
self.assertEqual(r.exit_status, EXIT.ERROR_HTTP_5XX)
|
||||
|
||||
|
||||
class FakeWindowsTest(BaseTestCase):
|
||||
@ -855,7 +863,7 @@ class FakeWindowsTest(BaseTestCase):
|
||||
httpbin('/get'),
|
||||
env=env
|
||||
)
|
||||
self.assertNotEqual(r.exit_status, 0)
|
||||
self.assertNotEqual(r.exit_status, EXIT.OK)
|
||||
self.assertIn('Windows', r.stderr)
|
||||
self.assertIn('--output', r.stderr)
|
||||
|
||||
|
Reference in New Issue
Block a user