2014-04-24 15:07:31 +03:00
|
|
|
"""High-level tests."""
|
2019-09-18 11:57:27 +02:00
|
|
|
import io
|
|
|
|
from unittest import mock
|
|
|
|
|
2015-01-23 23:04:42 +02:00
|
|
|
import pytest
|
2016-07-02 11:50:30 +02:00
|
|
|
|
2020-09-28 16:58:59 +02:00
|
|
|
import httpie
|
2019-09-18 11:57:27 +02:00
|
|
|
import httpie.__main__
|
2021-05-05 14:13:39 +02:00
|
|
|
from .fixtures import FILE_CONTENT, FILE_PATH
|
2020-09-28 16:58:59 +02:00
|
|
|
from httpie.cli.exceptions import ParseError
|
2019-09-18 11:57:27 +02:00
|
|
|
from httpie.context import Environment
|
2021-08-05 20:58:43 +02:00
|
|
|
from httpie.constants import UTF8
|
2019-09-16 13:28:01 +02:00
|
|
|
from httpie.status import ExitStatus
|
2021-05-05 14:13:39 +02:00
|
|
|
from .utils import HTTP_OK, MockEnvironment, StdinBytesIO, http
|
2014-04-24 15:07:31 +03:00
|
|
|
|
|
|
|
|
2019-09-18 11:57:27 +02:00
|
|
|
def test_main_entry_point():
|
|
|
|
# Patch stdin to bypass pytest capture
|
|
|
|
with mock.patch.object(Environment, 'stdin', io.StringIO()):
|
2021-08-06 16:57:19 +02:00
|
|
|
assert httpie.__main__.main() == ExitStatus.ERROR.value
|
2019-09-18 11:57:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
@mock.patch('httpie.core.main')
|
|
|
|
def test_main_entry_point_keyboard_interrupt(main):
|
|
|
|
main.side_effect = KeyboardInterrupt()
|
|
|
|
with mock.patch.object(Environment, 'stdin', io.StringIO()):
|
2021-08-06 16:57:19 +02:00
|
|
|
assert httpie.__main__.main() == ExitStatus.ERROR_CTRL_C.value
|
2019-09-18 11:57:27 +02:00
|
|
|
|
|
|
|
|
2016-03-03 12:47:12 +02:00
|
|
|
def test_debug():
|
|
|
|
r = http('--debug')
|
2019-09-16 13:28:01 +02:00
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2021-05-26 14:09:38 +02:00
|
|
|
assert f'HTTPie {httpie.__version__}' in r.stderr
|
2016-03-03 12:47:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_help():
|
2019-09-03 17:14:39 +02:00
|
|
|
r = http('--help', tolerate_error_exit_status=True)
|
2019-09-16 13:28:01 +02:00
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2020-12-23 23:07:27 +02:00
|
|
|
assert 'https://github.com/httpie/httpie/issues' in r
|
2016-03-03 12:47:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
def test_version():
|
2019-09-03 17:14:39 +02:00
|
|
|
r = http('--version', tolerate_error_exit_status=True)
|
2019-09-16 13:28:01 +02:00
|
|
|
assert r.exit_status == ExitStatus.SUCCESS
|
2019-08-30 11:32:14 +02:00
|
|
|
assert httpie.__version__ == r.strip()
|
2016-03-03 12:47:12 +02:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2020-04-13 20:18:01 +02:00
|
|
|
def test_path_dot_normalization():
|
|
|
|
r = http(
|
|
|
|
'--offline',
|
|
|
|
'example.org/../../etc/password',
|
|
|
|
'param==value'
|
|
|
|
)
|
|
|
|
assert 'GET /etc/password?param=value' in r
|
|
|
|
|
|
|
|
|
|
|
|
def test_path_as_is():
|
|
|
|
r = http(
|
|
|
|
'--offline',
|
|
|
|
'--path-as-is',
|
|
|
|
'example.org/../../etc/password',
|
|
|
|
'param==value'
|
|
|
|
)
|
|
|
|
assert 'GET /../../etc/password?param=value' 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
|
2020-09-28 16:58:59 +02:00
|
|
|
assert r.json['form'] == {
|
|
|
|
'foo': ['bar', 'baz']
|
|
|
|
}
|
2016-03-03 12:47:12 +02:00
|
|
|
|
|
|
|
|
2021-05-24 14:29:54 +02:00
|
|
|
def test_POST_raw(httpbin_both):
|
|
|
|
r = http('--raw', 'foo bar', 'POST', httpbin_both + '/post')
|
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"foo bar"' in r
|
|
|
|
|
|
|
|
|
2016-03-06 11:42:35 +02:00
|
|
|
def test_POST_stdin(httpbin_both):
|
2020-09-28 12:16:57 +02:00
|
|
|
env = MockEnvironment(
|
|
|
|
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
|
|
|
|
stdin_isatty=False,
|
|
|
|
)
|
|
|
|
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):
|
2020-09-28 12:16:57 +02:00
|
|
|
r = http('--form', 'POST', httpbin_both + '/post', f'file@{FILE_PATH}')
|
2019-08-29 10:44:59 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert FILE_CONTENT in r
|
|
|
|
|
|
|
|
|
2020-01-23 16:54:43 +02:00
|
|
|
def test_form_POST_file_redirected_stdin(httpbin):
|
|
|
|
"""
|
2020-12-23 23:07:27 +02:00
|
|
|
<https://github.com/httpie/httpie/issues/840>
|
2020-01-23 16:54:43 +02:00
|
|
|
|
|
|
|
"""
|
2021-08-05 20:58:43 +02:00
|
|
|
with open(FILE_PATH, encoding=UTF8):
|
2020-01-23 16:54:43 +02:00
|
|
|
r = http(
|
|
|
|
'--form',
|
|
|
|
'POST',
|
|
|
|
httpbin + '/post',
|
2020-09-28 12:16:57 +02:00
|
|
|
f'file@{FILE_PATH}',
|
2020-01-23 16:54:43 +02:00
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
env=MockEnvironment(
|
2020-09-28 12:16:57 +02:00
|
|
|
stdin=StdinBytesIO(FILE_PATH.read_bytes()),
|
2020-01-23 16:54:43 +02:00
|
|
|
stdin_isatty=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'cannot be mixed' in r.stderr
|
|
|
|
|
|
|
|
|
2021-05-24 14:29:54 +02:00
|
|
|
def test_raw_POST_key_values_supplied(httpbin):
|
|
|
|
r = http(
|
|
|
|
'--raw',
|
|
|
|
'foo bar',
|
|
|
|
'POST',
|
|
|
|
httpbin + '/post',
|
|
|
|
'foo=bar',
|
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
)
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'cannot be mixed' in r.stderr
|
|
|
|
|
|
|
|
|
|
|
|
def test_raw_POST_redirected_stdin(httpbin):
|
|
|
|
r = http(
|
|
|
|
'--raw',
|
|
|
|
'foo bar',
|
|
|
|
'POST',
|
|
|
|
httpbin + '/post',
|
|
|
|
tolerate_error_exit_status=True,
|
|
|
|
env=MockEnvironment(
|
|
|
|
stdin='some=value',
|
|
|
|
stdin_isatty=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
assert r.exit_status == ExitStatus.ERROR
|
|
|
|
assert 'cannot be mixed' in r.stderr
|
|
|
|
|
|
|
|
|
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:')
|
2020-09-28 16:58:59 +02:00
|
|
|
assert 'Accept' not in r.json['headers'] # default Accept unset
|
2016-07-02 11:50:30 +02:00
|
|
|
|
|
|
|
|
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:')
|
2020-09-28 16:58:59 +02:00
|
|
|
assert 'Host' not in r.json['headers'] # default Host unset
|
2016-11-23 22:47:06 +02:00
|
|
|
|
|
|
|
|
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;')
|
2020-09-28 16:58:59 +02:00
|
|
|
assert r.json['headers']['Accept'] == '' # Accept has no value
|
2016-07-02 11:50:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
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'] == \
|
2020-12-21 13:03:25 +02:00
|
|
|
'{"order": {"map": {"1": "first", "2": "second"}}}'
|