1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/tests/test_output.py

114 lines
3.7 KiB
Python
Raw Normal View History

from unittest import TestCase
from httpie import ExitStatus
2014-04-24 19:20:23 +03:00
from tests import TestEnvironment, http, httpbin, HTTP_OK, COLOR, CRLF
2014-04-24 15:07:31 +03:00
class VerboseFlagTest(TestCase):
2014-04-24 15:07:31 +03:00
def test_verbose(self):
2014-04-24 16:48:01 +03:00
r = http('--verbose', 'GET', httpbin('/get'), 'test-header:__test__')
assert HTTP_OK in r
assert r.count('__test__') == 2
2014-04-24 15:07:31 +03:00
def test_verbose_form(self):
# https://github.com/jkbr/httpie/issues/53
2014-04-24 16:48:01 +03:00
r = http('--verbose', '--form', 'POST', httpbin('/post'),
2014-04-24 18:08:40 +03:00
'A=B', 'C=D')
assert HTTP_OK in r
assert 'A=B&C=D' in r
2014-04-24 15:07:31 +03:00
def test_verbose_json(self):
2014-04-24 16:48:01 +03:00
r = http('--verbose', 'POST', httpbin('/post'), 'foo=bar', 'baz=bar')
assert HTTP_OK in r
assert '"baz": "bar"' in r # request
assert r'\"baz\": \"bar\"' in r # response
2014-04-24 15:07:31 +03:00
class PrettyOptionsTest(TestCase):
2014-04-24 15:07:31 +03:00
"""Test the --pretty flag handling."""
def test_pretty_enabled_by_default(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(colors=256)
r = http('GET', httpbin('/get'), env=env)
assert COLOR in r
2014-04-24 15:07:31 +03:00
def test_pretty_enabled_by_default_unless_stdout_redirected(self):
2014-04-24 16:48:01 +03:00
r = http('GET', httpbin('/get'))
assert COLOR not in r
2014-04-24 15:07:31 +03:00
def test_force_pretty(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(stdout_isatty=False, colors=256)
r = http('--pretty=all', 'GET', httpbin('/get'), env=env, )
assert COLOR in r
2014-04-24 15:07:31 +03:00
def test_force_ugly(self):
2014-04-24 16:48:01 +03:00
r = http('--pretty=none', 'GET', httpbin('/get'))
assert COLOR not in r
2014-04-24 15:07:31 +03:00
def test_subtype_based_pygments_lexer_match(self):
"""Test that media subtype is used if type/subtype doesn't
match any lexer.
"""
2014-04-24 16:48:01 +03:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=all', httpbin('/post'),
'Content-Type:text/foo+json', 'a=b', env=env)
assert COLOR in r
2014-04-24 15:07:31 +03:00
def test_colors_option(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=colors', 'GET', httpbin('/get'), 'a=b',
env=env)
2014-04-24 15:07:31 +03:00
# Tests that the JSON data isn't formatted.
assert not r.strip().count('\n')
assert COLOR in r
2014-04-24 15:07:31 +03:00
def test_format_option(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=format', 'GET', httpbin('/get'), 'a=b',
env=env)
2014-04-24 15:07:31 +03:00
# Tests that the JSON data is formatted.
assert r.strip().count('\n') == 2
assert COLOR not in r
2014-04-24 15:07:31 +03:00
class LineEndingsTest(TestCase):
2014-04-24 16:48:01 +03:00
"""
Test that CRLF is properly used in headers
and as the headers/body separator.
2014-04-24 15:07:31 +03:00
2014-04-24 16:48:01 +03:00
"""
2014-04-24 15:07:31 +03:00
def _validate_crlf(self, msg):
lines = iter(msg.splitlines(True))
for header in lines:
if header == CRLF:
break
assert header.endswith(CRLF), repr(header)
2014-04-24 15:07:31 +03:00
else:
self.fail('CRLF between headers and body not found in %r' % msg)
body = ''.join(lines)
assert CRLF not in body
2014-04-24 15:07:31 +03:00
return body
def test_CRLF_headers_only(self):
2014-04-24 16:48:01 +03:00
r = http('--headers', 'GET', httpbin('/get'))
2014-04-24 15:07:31 +03:00
body = self._validate_crlf(r)
assert not body, 'Garbage after headers: %r' % r
2014-04-24 15:07:31 +03:00
def test_CRLF_ugly_response(self):
2014-04-24 16:48:01 +03:00
r = http('--pretty=none', 'GET', httpbin('/get'))
2014-04-24 15:07:31 +03:00
self._validate_crlf(r)
def test_CRLF_formatted_response(self):
2014-04-24 16:48:01 +03:00
r = http('--pretty=format', 'GET', httpbin('/get'))
assert r.exit_status == ExitStatus.OK
2014-04-24 15:07:31 +03:00
self._validate_crlf(r)
def test_CRLF_ugly_request(self):
2014-04-24 16:48:01 +03:00
r = http('--pretty=none', '--print=HB', 'GET', httpbin('/get'))
2014-04-24 15:07:31 +03:00
self._validate_crlf(r)
def test_CRLF_formatted_request(self):
2014-04-24 16:48:01 +03:00
r = http('--pretty=format', '--print=HB', 'GET', httpbin('/get'))
2014-04-24 15:07:31 +03:00
self._validate_crlf(r)