You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Python 3 annotations, super(), pathlib, etc.
This commit is contained in:
@@ -305,7 +305,7 @@ class TestNoOptions:
|
||||
def test_invalid_no_options(self, httpbin):
|
||||
r = http('--no-war', 'GET', httpbin.url + '/get',
|
||||
error_exit_ok=True)
|
||||
assert r.exit_status == 1
|
||||
assert r.exit_status == ExitStatus.ERROR
|
||||
assert 'unrecognized arguments: --no-war' in r.stderr
|
||||
assert 'GET /get HTTP/1.1' not in r
|
||||
|
||||
|
@@ -28,5 +28,5 @@ def test_default_options_overwrite(httpbin):
|
||||
|
||||
|
||||
def test_current_version():
|
||||
version = Environment().config['__meta__']['httpie']
|
||||
version = MockEnvironment().config['__meta__']['httpie']
|
||||
assert version == __version__
|
||||
|
@@ -9,7 +9,7 @@ from utils import TESTS_ROOT
|
||||
|
||||
def has_docutils():
|
||||
try:
|
||||
# noinspection PyUnresolvedReferences
|
||||
# noinspection PyUnresolvedReferences,PyPackageRequirements
|
||||
import docutils
|
||||
return True
|
||||
except ImportError:
|
||||
@@ -17,6 +17,7 @@ def has_docutils():
|
||||
|
||||
|
||||
def rst_filenames():
|
||||
# noinspection PyShadowingNames
|
||||
for root, dirnames, filenames in os.walk(os.path.dirname(TESTS_ROOT)):
|
||||
if '.tox' not in root:
|
||||
for filename in fnmatch.filter(filenames, '*.rst'):
|
||||
|
@@ -14,7 +14,7 @@ from httpie.downloads import (
|
||||
from utils import http, MockEnvironment
|
||||
|
||||
|
||||
class Response(object):
|
||||
class Response:
|
||||
# noinspection PyDefaultArgument
|
||||
def __init__(self, url, headers={}, status_code=200):
|
||||
self.url = url
|
||||
|
@@ -24,7 +24,7 @@ def test_version():
|
||||
r = http('--version', error_exit_ok=True)
|
||||
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
||||
# FIXME: py3 has version in stdout, py2 in stderr
|
||||
assert httpie.__version__ == r.stderr.strip() + r.strip()
|
||||
assert httpie.__version__ == r.strip()
|
||||
|
||||
|
||||
def test_GET(httpbin_both):
|
||||
|
@@ -11,7 +11,7 @@ from utils import MockEnvironment, mk_config_dir, http, HTTP_OK
|
||||
from fixtures import UNICODE
|
||||
|
||||
|
||||
class SessionTestBase(object):
|
||||
class SessionTestBase:
|
||||
|
||||
def start_session(self, httpbin):
|
||||
"""Create and reuse a unique config dir for each test."""
|
||||
@@ -44,7 +44,7 @@ class TestSessionFlow(SessionTestBase):
|
||||
authorization, and response cookies.
|
||||
|
||||
"""
|
||||
super(TestSessionFlow, self).start_session(httpbin)
|
||||
super().start_session(httpbin)
|
||||
r1 = http('--follow', '--session=test', '--auth=username:password',
|
||||
'GET', httpbin.url + '/cookies/set?hello=world',
|
||||
'Hello:World',
|
||||
@@ -130,12 +130,12 @@ class TestSession(SessionTestBase):
|
||||
|
||||
def test_session_by_path(self, httpbin):
|
||||
self.start_session(httpbin)
|
||||
session_path = os.path.join(self.config_dir, 'session-by-path.json')
|
||||
r1 = http('--session=' + session_path, 'GET', httpbin.url + '/get',
|
||||
session_path = self.config_dir / 'session-by-path.json'
|
||||
r1 = http('--session', str(session_path), 'GET', httpbin.url + '/get',
|
||||
'Foo:Bar', env=self.env())
|
||||
assert HTTP_OK in r1
|
||||
|
||||
r2 = http('--session=' + session_path, 'GET', httpbin.url + '/get',
|
||||
r2 = http('--session', str(session_path), 'GET', httpbin.url + '/get',
|
||||
env=self.env())
|
||||
assert HTTP_OK in r2
|
||||
assert r2.json['headers']['Foo'] == 'Bar'
|
||||
|
@@ -5,8 +5,11 @@ import sys
|
||||
import time
|
||||
import json
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
from typing import Optional
|
||||
|
||||
from httpie import ExitStatus, EXIT_STATUS_LABELS
|
||||
from httpie import ExitStatus
|
||||
from httpie.config import Config
|
||||
from httpie.context import Environment
|
||||
from httpie.core import main
|
||||
|
||||
@@ -22,9 +25,9 @@ HTTP_OK_COLOR = (
|
||||
)
|
||||
|
||||
|
||||
def mk_config_dir():
|
||||
def mk_config_dir() -> Path:
|
||||
dirname = tempfile.mkdtemp(prefix='httpie_config_')
|
||||
return dirname
|
||||
return Path(dirname)
|
||||
|
||||
|
||||
def add_auth(url, auth):
|
||||
@@ -40,7 +43,6 @@ class MockEnvironment(Environment):
|
||||
is_windows = False
|
||||
|
||||
def __init__(self, create_temp_config_dir=True, **kwargs):
|
||||
self.create_temp_config_dir = create_temp_config_dir
|
||||
if 'stdout' not in kwargs:
|
||||
kwargs['stdout'] = tempfile.TemporaryFile(
|
||||
mode='w+b',
|
||||
@@ -51,22 +53,24 @@ class MockEnvironment(Environment):
|
||||
mode='w+t',
|
||||
prefix='httpie_stderr'
|
||||
)
|
||||
super(MockEnvironment, self).__init__(**kwargs)
|
||||
super().__init__(**kwargs)
|
||||
self._create_temp_config_dir = create_temp_config_dir
|
||||
self._delete_config_dir = False
|
||||
self._temp_dir = Path(tempfile.gettempdir())
|
||||
|
||||
@property
|
||||
def config(self):
|
||||
if (self.create_temp_config_dir
|
||||
and not self.config_dir.startswith(tempfile.gettempdir())):
|
||||
def config(self) -> Config:
|
||||
if (self._create_temp_config_dir
|
||||
and self._temp_dir not in self.config_dir.parents):
|
||||
self.config_dir = mk_config_dir()
|
||||
self._delete_config_dir = True
|
||||
return super(MockEnvironment, self).config
|
||||
return super().config
|
||||
|
||||
def cleanup(self):
|
||||
self.stdout.close()
|
||||
self.stderr.close()
|
||||
if self._delete_config_dir:
|
||||
assert self.config_dir.startswith(tempfile.gettempdir())
|
||||
assert self._temp_dir in self.config_dir.parents
|
||||
from shutil import rmtree
|
||||
rmtree(self.config_dir)
|
||||
|
||||
@@ -77,7 +81,7 @@ class MockEnvironment(Environment):
|
||||
pass
|
||||
|
||||
|
||||
class BaseCLIResponse(object):
|
||||
class BaseCLIResponse:
|
||||
"""
|
||||
Represents the result of simulated `$ http' invocation via `http()`.
|
||||
|
||||
@@ -88,9 +92,9 @@ class BaseCLIResponse(object):
|
||||
- exit_status output: print(self.exit_status)
|
||||
|
||||
"""
|
||||
stderr = None
|
||||
json = None
|
||||
exit_status = None
|
||||
stderr: str = None
|
||||
json: dict = None
|
||||
exit_status: ExitStatus = None
|
||||
|
||||
|
||||
class BytesCLIResponse(bytes, BaseCLIResponse):
|
||||
@@ -107,7 +111,7 @@ class BytesCLIResponse(bytes, BaseCLIResponse):
|
||||
class StrCLIResponse(str, BaseCLIResponse):
|
||||
|
||||
@property
|
||||
def json(self):
|
||||
def json(self) -> Optional[dict]:
|
||||
"""
|
||||
Return deserialized JSON body, if one included in the output
|
||||
and is parsable.
|
||||
@@ -132,6 +136,7 @@ class StrCLIResponse(str, BaseCLIResponse):
|
||||
pass
|
||||
else:
|
||||
try:
|
||||
# noinspection PyAttributeOutsideInit
|
||||
self._json = json.loads(j)
|
||||
except ValueError:
|
||||
pass
|
||||
@@ -174,7 +179,7 @@ def http(*args, program_name='http', **kwargs):
|
||||
>>> type(r) == StrCLIResponse
|
||||
True
|
||||
>>> r.exit_status
|
||||
0
|
||||
<ExitStatus.SUCCESS: 0>
|
||||
>>> r.stderr
|
||||
''
|
||||
>>> 'HTTP/1.1 200 OK' in r
|
||||
@@ -227,10 +232,7 @@ def http(*args, program_name='http', **kwargs):
|
||||
dump_stderr()
|
||||
raise ExitStatusError(
|
||||
'httpie.core.main() unexpectedly returned'
|
||||
' a non-zero exit status: {0} ({1})'.format(
|
||||
exit_status,
|
||||
EXIT_STATUS_LABELS[exit_status]
|
||||
)
|
||||
f' a non-zero exit status: {exit_status}'
|
||||
)
|
||||
|
||||
stdout.seek(0)
|
||||
@@ -239,10 +241,8 @@ def http(*args, program_name='http', **kwargs):
|
||||
try:
|
||||
output = output.decode('utf8')
|
||||
except UnicodeDecodeError:
|
||||
# noinspection PyArgumentList
|
||||
r = BytesCLIResponse(output)
|
||||
else:
|
||||
# noinspection PyArgumentList
|
||||
r = StrCLIResponse(output)
|
||||
r.stderr = stderr.read()
|
||||
r.exit_status = exit_status
|
||||
|
Reference in New Issue
Block a user