httpie-cli/tests/test_output.py

142 lines
4.5 KiB
Python
Raw Normal View History

2014-04-28 10:01:39 +02:00
import pytest
2015-02-07 16:29:17 +01:00
from utils import TestEnvironment, http, HTTP_OK, COLOR, CRLF
from httpie import ExitStatus
from httpie.output.formatters.colors import get_lexer
2014-04-24 14:07:31 +02:00
class TestVerboseFlag:
def test_verbose(self, httpbin):
r = http('--verbose',
'GET', httpbin.url + '/get', 'test-header:__test__')
assert HTTP_OK in r
assert r.count('__test__') == 2
2014-04-24 14:07:31 +02:00
def test_verbose_form(self, httpbin):
2015-07-03 18:55:45 +02:00
# https://github.com/jkbrzt/httpie/issues/53
r = http('--verbose', '--form', 'POST', httpbin.url + '/post',
2014-04-24 17:08:40 +02:00
'A=B', 'C=D')
assert HTTP_OK in r
assert 'A=B&C=D' in r
2014-04-24 14:07:31 +02:00
def test_verbose_json(self, httpbin):
r = http('--verbose',
'POST', httpbin.url + '/post', 'foo=bar', 'baz=bar')
assert HTTP_OK in r
assert '"baz": "bar"' in r
2014-04-24 14:07:31 +02:00
2014-04-28 10:01:39 +02:00
class TestColors:
@pytest.mark.parametrize('mime', [
'application/json',
'application/json+foo',
'application/foo+json',
'application/json-foo',
'application/x-json',
'foo/json',
'foo/json+bar',
'foo/bar+json',
'foo/json-foo',
'foo/x-json',
'application/vnd.comverge.grid+hal+json',
2014-04-28 10:01:39 +02:00
])
def test_get_lexer(self, mime):
lexer = get_lexer(mime)
assert lexer is not None
assert lexer.name == 'JSON'
2014-04-28 10:01:39 +02:00
def test_get_lexer_not_found(self):
assert get_lexer('xxx/yyy') is None
class TestPrettyOptions:
2014-04-24 14:07:31 +02:00
"""Test the --pretty flag handling."""
def test_pretty_enabled_by_default(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(colors=256)
r = http('GET', httpbin.url + '/get', env=env)
assert COLOR in r
2014-04-24 14:07:31 +02:00
def test_pretty_enabled_by_default_unless_stdout_redirected(self, httpbin):
r = http('GET', httpbin.url + '/get')
assert COLOR not in r
2014-04-24 14:07:31 +02:00
def test_force_pretty(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(stdout_isatty=False, colors=256)
r = http('--pretty=all', 'GET', httpbin.url + '/get', env=env, )
assert COLOR in r
2014-04-24 14:07:31 +02:00
def test_force_ugly(self, httpbin):
r = http('--pretty=none', 'GET', httpbin.url + '/get')
assert COLOR not in r
2014-04-24 14:07:31 +02:00
def test_subtype_based_pygments_lexer_match(self, httpbin):
2014-04-24 14:07:31 +02:00
"""Test that media subtype is used if type/subtype doesn't
match any lexer.
"""
2014-04-24 15:48:01 +02:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=all', httpbin.url + '/post',
2014-04-24 15:48:01 +02:00
'Content-Type:text/foo+json', 'a=b', env=env)
assert COLOR in r
2014-04-24 14:07:31 +02:00
def test_colors_option(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=colors',
'GET', httpbin.url + '/get', 'a=b',
2014-04-24 15:48:01 +02:00
env=env)
2014-04-24 14:07:31 +02:00
# Tests that the JSON data isn't formatted.
assert not r.strip().count('\n')
assert COLOR in r
2014-04-24 14:07:31 +02:00
def test_format_option(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(colors=256)
r = http('--print=B', '--pretty=format',
'GET', httpbin.url + '/get', 'a=b',
2014-04-24 15:48:01 +02:00
env=env)
2014-04-24 14:07:31 +02:00
# Tests that the JSON data is formatted.
assert r.strip().count('\n') == 2
assert COLOR not in r
2014-04-24 14:07:31 +02:00
class TestLineEndings:
2014-04-24 15:48:01 +02:00
"""
Test that CRLF is properly used in headers
and as the headers/body separator.
2014-04-24 14:07:31 +02:00
2014-04-24 15:48:01 +02:00
"""
2014-04-24 14:07:31 +02: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 14:07:31 +02:00
else:
assert 0, 'CRLF between headers and body not found in %r' % msg
2014-04-24 14:07:31 +02:00
body = ''.join(lines)
assert CRLF not in body
2014-04-24 14:07:31 +02:00
return body
def test_CRLF_headers_only(self, httpbin):
r = http('--headers', 'GET', httpbin.url + '/get')
2014-04-24 14:07:31 +02:00
body = self._validate_crlf(r)
assert not body, 'Garbage after headers: %r' % r
2014-04-24 14:07:31 +02:00
def test_CRLF_ugly_response(self, httpbin):
r = http('--pretty=none', 'GET', httpbin.url + '/get')
2014-04-24 14:07:31 +02:00
self._validate_crlf(r)
def test_CRLF_formatted_response(self, httpbin):
r = http('--pretty=format', 'GET', httpbin.url + '/get')
assert r.exit_status == ExitStatus.OK
2014-04-24 14:07:31 +02:00
self._validate_crlf(r)
def test_CRLF_ugly_request(self, httpbin):
r = http('--pretty=none', '--print=HB', 'GET', httpbin.url + '/get')
2014-04-24 14:07:31 +02:00
self._validate_crlf(r)
def test_CRLF_formatted_request(self, httpbin):
r = http('--pretty=format', '--print=HB', 'GET', httpbin.url + '/get')
2014-04-24 14:07:31 +02:00
self._validate_crlf(r)