2016-03-01 14:24:50 +02:00
|
|
|
import os
|
|
|
|
from tempfile import gettempdir
|
|
|
|
|
2014-04-28 11:01:39 +03:00
|
|
|
import pytest
|
|
|
|
|
2015-02-07 17:29:17 +02:00
|
|
|
from utils import TestEnvironment, http, HTTP_OK, COLOR, CRLF
|
2014-04-24 15:58:15 +03:00
|
|
|
from httpie import ExitStatus
|
2016-03-01 14:24:50 +02:00
|
|
|
from httpie.compat import urlopen
|
2014-04-29 00:33:30 +03:00
|
|
|
from httpie.output.formatters.colors import get_lexer
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2016-03-03 12:47:12 +02:00
|
|
|
@pytest.mark.parametrize('stdout_isatty', [True, False])
|
2016-03-01 14:24:50 +02:00
|
|
|
def test_output_option(httpbin, stdout_isatty):
|
|
|
|
output_filename = os.path.join(gettempdir(), test_output_option.__name__)
|
|
|
|
url = httpbin + '/robots.txt'
|
|
|
|
|
|
|
|
r = http('--output', output_filename, url,
|
|
|
|
env=TestEnvironment(stdout_isatty=stdout_isatty))
|
|
|
|
assert r == ''
|
|
|
|
|
|
|
|
expected_body = urlopen(url).read().decode()
|
|
|
|
with open(output_filename, 'r') as f:
|
|
|
|
actual_body = f.read()
|
|
|
|
|
|
|
|
assert actual_body == expected_body
|
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestVerboseFlag:
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_verbose(self, httpbin):
|
|
|
|
r = http('--verbose',
|
|
|
|
'GET', httpbin.url + '/get', 'test-header:__test__')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.count('__test__') == 2
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_verbose_form(self, httpbin):
|
2017-03-10 12:27:38 +02:00
|
|
|
# https://github.com/jakubroztocil/httpie/issues/53
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--verbose', '--form', 'POST', httpbin.url + '/post',
|
2014-04-24 18:08:40 +03:00
|
|
|
'A=B', 'C=D')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
2014-04-24 20:56:11 +03:00
|
|
|
assert 'A=B&C=D' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_verbose_json(self, httpbin):
|
|
|
|
r = http('--verbose',
|
|
|
|
'POST', httpbin.url + '/post', 'foo=bar', 'baz=bar')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert HTTP_OK in r
|
2014-06-28 17:35:57 +03:00
|
|
|
assert '"baz": "bar"' in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2016-03-09 15:58:11 +02:00
|
|
|
def test_verbose_implies_all(self, httpbin):
|
|
|
|
r = http('--verbose', '--follow', httpbin + '/redirect/1')
|
|
|
|
assert 'GET /redirect/1 HTTP/1.1' in r
|
|
|
|
assert 'HTTP/1.1 302 FOUND' in r
|
|
|
|
assert 'GET /get HTTP/1.1' in r
|
|
|
|
assert HTTP_OK in r
|
|
|
|
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-04-28 11:01:39 +03:00
|
|
|
class TestColors:
|
|
|
|
|
2016-03-01 08:57:15 +02:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
argnames=['mime', 'explicit_json', 'body', 'expected_lexer_name'],
|
|
|
|
argvalues=[
|
|
|
|
('application/json', False, None, 'JSON'),
|
|
|
|
('application/json+foo', False, None, 'JSON'),
|
|
|
|
('application/foo+json', False, None, 'JSON'),
|
|
|
|
('application/json-foo', False, None, 'JSON'),
|
|
|
|
('application/x-json', False, None, 'JSON'),
|
|
|
|
('foo/json', False, None, 'JSON'),
|
|
|
|
('foo/json+bar', False, None, 'JSON'),
|
|
|
|
('foo/bar+json', False, None, 'JSON'),
|
|
|
|
('foo/json-foo', False, None, 'JSON'),
|
|
|
|
('foo/x-json', False, None, 'JSON'),
|
|
|
|
('application/vnd.comverge.grid+hal+json', False, None, 'JSON'),
|
|
|
|
('text/plain', True, '{}', 'JSON'),
|
|
|
|
('text/plain', True, 'foo', 'Text only'),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
def test_get_lexer(self, mime, explicit_json, body, expected_lexer_name):
|
|
|
|
lexer = get_lexer(mime, body=body, explicit_json=explicit_json)
|
2014-04-28 11:01:39 +03:00
|
|
|
assert lexer is not None
|
2016-03-01 08:57:15 +02:00
|
|
|
assert lexer.name == expected_lexer_name
|
2014-04-28 11:01:39 +03:00
|
|
|
|
|
|
|
def test_get_lexer_not_found(self):
|
|
|
|
assert get_lexer('xxx/yyy') is None
|
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestPrettyOptions:
|
2014-04-24 15:07:31 +03:00
|
|
|
"""Test the --pretty flag handling."""
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_pretty_enabled_by_default(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(colors=256)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('GET', httpbin.url + '/get', env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert COLOR in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_pretty_enabled_by_default_unless_stdout_redirected(self, httpbin):
|
|
|
|
r = http('GET', httpbin.url + '/get')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert COLOR not in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_force_pretty(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(stdout_isatty=False, colors=256)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--pretty=all', 'GET', httpbin.url + '/get', env=env, )
|
2014-04-24 15:58:15 +03:00
|
|
|
assert COLOR in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_force_ugly(self, httpbin):
|
|
|
|
r = http('--pretty=none', 'GET', httpbin.url + '/get')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert COLOR not in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_subtype_based_pygments_lexer_match(self, httpbin):
|
2014-04-24 15:07:31 +03:00
|
|
|
"""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)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', '--pretty=all', httpbin.url + '/post',
|
2014-04-24 16:48:01 +03:00
|
|
|
'Content-Type:text/foo+json', 'a=b', env=env)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert COLOR in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_colors_option(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(colors=256)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', '--pretty=colors',
|
|
|
|
'GET', httpbin.url + '/get', 'a=b',
|
2014-04-24 16:48:01 +03:00
|
|
|
env=env)
|
2014-04-24 15:07:31 +03:00
|
|
|
# Tests that the JSON data isn't formatted.
|
2014-04-24 15:58:15 +03:00
|
|
|
assert not r.strip().count('\n')
|
|
|
|
assert COLOR in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_format_option(self, httpbin):
|
2014-04-24 16:48:01 +03:00
|
|
|
env = TestEnvironment(colors=256)
|
2014-06-28 17:35:57 +03:00
|
|
|
r = http('--print=B', '--pretty=format',
|
|
|
|
'GET', httpbin.url + '/get', 'a=b',
|
2014-04-24 16:48:01 +03:00
|
|
|
env=env)
|
2014-04-24 15:07:31 +03:00
|
|
|
# Tests that the JSON data is formatted.
|
2014-04-24 15:58:15 +03:00
|
|
|
assert r.strip().count('\n') == 2
|
|
|
|
assert COLOR not in r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2014-04-25 12:39:59 +03:00
|
|
|
class TestLineEndings:
|
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
|
2014-04-24 15:58:15 +03:00
|
|
|
assert header.endswith(CRLF), repr(header)
|
2014-04-24 15:07:31 +03:00
|
|
|
else:
|
2014-04-25 12:39:59 +03:00
|
|
|
assert 0, 'CRLF between headers and body not found in %r' % msg
|
2014-04-24 15:07:31 +03:00
|
|
|
body = ''.join(lines)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert CRLF not in body
|
2014-04-24 15:07:31 +03:00
|
|
|
return body
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_CRLF_headers_only(self, httpbin):
|
|
|
|
r = http('--headers', 'GET', httpbin.url + '/get')
|
2014-04-24 15:07:31 +03:00
|
|
|
body = self._validate_crlf(r)
|
2014-04-24 15:58:15 +03:00
|
|
|
assert not body, 'Garbage after headers: %r' % r
|
2014-04-24 15:07:31 +03:00
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_CRLF_ugly_response(self, httpbin):
|
|
|
|
r = http('--pretty=none', 'GET', httpbin.url + '/get')
|
2014-04-24 15:07:31 +03:00
|
|
|
self._validate_crlf(r)
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_CRLF_formatted_response(self, httpbin):
|
|
|
|
r = http('--pretty=format', 'GET', httpbin.url + '/get')
|
2014-04-24 15:58:15 +03:00
|
|
|
assert r.exit_status == ExitStatus.OK
|
2014-04-24 15:07:31 +03:00
|
|
|
self._validate_crlf(r)
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_CRLF_ugly_request(self, httpbin):
|
|
|
|
r = http('--pretty=none', '--print=HB', 'GET', httpbin.url + '/get')
|
2014-04-24 15:07:31 +03:00
|
|
|
self._validate_crlf(r)
|
|
|
|
|
2014-06-28 17:35:57 +03:00
|
|
|
def test_CRLF_formatted_request(self, httpbin):
|
|
|
|
r = http('--pretty=format', '--print=HB', 'GET', httpbin.url + '/get')
|
2014-04-24 15:07:31 +03:00
|
|
|
self._validate_crlf(r)
|