This commit is contained in:
Jakub Roztocil 2014-04-25 12:42:50 +02:00
parent af4aa3a761
commit 492ee392bd
2 changed files with 20 additions and 7 deletions

View File

@ -1233,7 +1233,9 @@ Changelog
* `0.9.0-dev`_
* Added ``--cert`` and ``--certkey`` parameters to specify a client side
certificate and private key for SSL
* Started using pytest.
* Switched from ``unittest`` to ``pytest``.
* Various test suite improvements.
* Added `CONTRIBUTING`_.
* `0.8.0`_ (2014-01-25)
* Added ``field=@file.txt`` and ``field:=@file.json`` for embedding
the contents of text and JSON files into request data.

View File

@ -75,7 +75,7 @@ class TestEnvironment(Environment):
def http(*args, **kwargs):
"""
Run HTTPie and capture stderr/out and exist status.
Run HTTPie and capture stderr/out and exit status.
Invoke `httpie.core.main()` with `args` and `kwargs`,
and return a `CLIResponse` subclass instance.
@ -114,10 +114,11 @@ def http(*args, **kwargs):
stdout = env.stdout
stderr = env.stderr
args = list(args)
args = list(args)
if '--debug' not in args and '--traceback' not in args:
args = ['--traceback'] + args
try:
try:
exit_status = main(args=args, **kwargs)
@ -134,11 +135,13 @@ def http(*args, **kwargs):
stderr.seek(0)
output = stdout.read()
try:
# noinspection PyArgumentList
r = StrCLIResponse(output.decode('utf8'))
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
@ -169,7 +172,9 @@ class BytesCLIResponse(bytes, BaseCLIResponse):
"""
Used as a fallback when a StrCLIResponse cannot be used.
E.g. the output contains binary data. `.json` will always be None.
E.g. when the output contains binary data or when it is colorized.
`.json` will always be None.
"""
@ -178,6 +183,11 @@ class StrCLIResponse(str, BaseCLIResponse):
@property
def json(self):
"""
Return deserialized JSON body, if one included in the output
and is parseable.
"""
if not hasattr(self, '_json'):
self._json = None
# De-serialize JSON body if possible.
@ -189,7 +199,8 @@ class StrCLIResponse(str, BaseCLIResponse):
self._json = json.loads(self)
elif (self.count('Content-Type:') == 1
and 'application/json' in self):
# Looks like a JSON HTTP message, try to extract its body.
# Looks like a whole JSON HTTP message,
# try to extract its body.
try:
j = self.strip()[self.strip().rindex('\r\n\r\n'):]
except ValueError: