2014-04-24 15:07:31 +03:00
|
|
|
"""High-level tests."""
|
2015-01-23 23:04:42 +02:00
|
|
|
import pytest
|
2016-07-02 11:50:30 +02:00
|
|
|
|
|
|
|
from httpie.input import ParseError
|
2017-12-28 19:17:48 +02:00
|
|
|
from utils import MockEnvironment, http, HTTP_OK
|
2014-04-28 12:29:41 +03:00
|
|
|
from fixtures import FILE_PATH, FILE_CONTENT
|
2015-01-23 23:04:42 +02:00
|
|
|
|
2014-04-24 19:32:15 +03:00
|
|
|
import httpie
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2016-03-03 12:47:12 +02:00
|
|
|
def test_debug():
|
|
|
|
r = http('--debug')
|
2018-11-02 17:07:39 +02:00
|
|
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
2016-03-03 12:47:12 +02:00
|
|
|
assert 'HTTPie %s' % httpie.__version__ in r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
def test_help():
|
|
|
|
r = http('--help', error_exit_ok=True)
|
2018-11-02 17:07:39 +02:00
|
|
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
2017-03-10 12:27:38 +02:00
|
|
|
assert 'https://github.com/jakubroztocil/httpie/issues' in r
|
2016-03-03 12:47:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_version():
|
|
|
|
r = http('--version', error_exit_ok=True)
|
2018-11-02 17:07:39 +02:00
|
|
|
assert r.exit_status == httpie.ExitStatus.SUCCESS
|
2016-03-03 12:47:12 +02:00
|
|
|
# FIXME: py3 has version in stdout, py2 in stderr
|
|
|
|
assert httpie.__version__ == r.stderr.strip() + r.strip()
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_GET(httpbin_both):
|
|
|
|
r = http('GET', httpbin_both + '/get')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_DELETE(httpbin_both):
|
|
|
|
r = http('DELETE', httpbin_both + '/delete')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_PUT(httpbin_both):
|
|
|
|
r = http('PUT', httpbin_both + '/put', 'foo=bar')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['json']['foo'] == 'bar'
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_POST_JSON_data(httpbin_both):
|
|
|
|
r = http('POST', httpbin_both + '/post', 'foo=bar')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['json']['foo'] == 'bar'
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_POST_form(httpbin_both):
|
|
|
|
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"foo": "bar"' in r
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_POST_form_multiple_values(httpbin_both):
|
|
|
|
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar', 'foo=baz')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['form'] == {'foo': ['bar', 'baz']}
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_POST_stdin(httpbin_both):
|
2016-03-03 12:47:12 +02:00
|
|
|
with open(FILE_PATH) as f:
|
2017-12-28 19:17:48 +02:00
|
|
|
env = MockEnvironment(stdin=f, stdin_isatty=False)
|
2016-03-06 11:42:35 +02:00
|
|
|
r = http('--form', 'POST', httpbin_both + '/post', env=env)
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FILE_CONTENT in r
|
|
|
|
|
|
|
|
|
2019-08-29 10:44:59 +02:00
|
|
|
def test_POST_file(httpbin_both):
|
|
|
|
r = http('--form', 'POST', httpbin_both + '/post', 'file@' + FILE_PATH)
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FILE_CONTENT in r
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_headers(httpbin_both):
|
|
|
|
r = http('GET', httpbin_both + '/headers', 'Foo:bar')
|
2016-03-03 12:47:12 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"User-Agent": "HTTPie' in r, r
|
|
|
|
assert '"Foo": "bar"' in r
|
|
|
|
|
|
|
|
|
2016-07-02 11:50:30 +02:00
|
|
|
def test_headers_unset(httpbin_both):
|
|
|
|
r = http('GET', httpbin_both + '/headers')
|
|
|
|
assert 'Accept' in r.json['headers'] # default Accept present
|
|
|
|
|
|
|
|
r = http('GET', httpbin_both + '/headers', 'Accept:')
|
|
|
|
assert 'Accept' not in r.json['headers'] # default Accept unset
|
|
|
|
|
|
|
|
|
2016-11-23 22:47:06 +02:00
|
|
|
@pytest.mark.skip('unimplemented')
|
|
|
|
def test_unset_host_header(httpbin_both):
|
|
|
|
r = http('GET', httpbin_both + '/headers')
|
|
|
|
assert 'Host' in r.json['headers'] # default Host present
|
|
|
|
|
|
|
|
r = http('GET', httpbin_both + '/headers', 'Host:')
|
|
|
|
assert 'Host' not in r.json['headers'] # default Host unset
|
|
|
|
|
|
|
|
|
2016-07-02 11:50:30 +02:00
|
|
|
def test_headers_empty_value(httpbin_both):
|
|
|
|
r = http('GET', httpbin_both + '/headers')
|
|
|
|
assert r.json['headers']['Accept'] # default Accept has value
|
|
|
|
|
|
|
|
r = http('GET', httpbin_both + '/headers', 'Accept;')
|
|
|
|
assert r.json['headers']['Accept'] == '' # Accept has no value
|
|
|
|
|
|
|
|
|
|
|
|
def test_headers_empty_value_with_value_gives_error(httpbin):
|
|
|
|
with pytest.raises(ParseError):
|
|
|
|
http('GET', httpbin + '/headers', 'Accept;SYNTAX_ERROR')
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_json_input_preserve_order(httpbin_both):
|
|
|
|
r = http('PATCH', httpbin_both + '/patch',
|
2016-03-03 12:47:12 +02:00
|
|
|
'order:={"map":{"1":"first","2":"second"}}')
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['data'] == \
|
|
|
|
'{"order": {"map": {"1": "first", "2": "second"}}}'
|