httpie-cli/tests/test_httpie.py

201 lines
5.4 KiB
Python
Raw Normal View History

2014-04-24 14:07:31 +02:00
"""High-level tests."""
2019-09-18 11:57:27 +02:00
import io
from unittest import mock
import pytest
2019-09-18 11:57:27 +02:00
import httpie.__main__
from httpie.context import Environment
2019-09-16 13:28:01 +02:00
from httpie.status import ExitStatus
2019-08-31 15:17:10 +02:00
from httpie.cli.exceptions import ParseError
from utils import MockEnvironment, http, HTTP_OK
from fixtures import FILE_PATH, FILE_CONTENT
2014-04-24 18:32:15 +02:00
import httpie
2014-04-24 14:07:31 +02: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()):
with pytest.raises(SystemExit) as e:
httpie.__main__.main()
assert e.value.code == ExitStatus.ERROR
@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()):
with pytest.raises(SystemExit) as e:
httpie.__main__.main()
assert e.value.code == ExitStatus.ERROR_CTRL_C
2016-03-03 11:47:12 +01:00
def test_debug():
r = http('--debug')
2019-09-16 13:28:01 +02:00
assert r.exit_status == ExitStatus.SUCCESS
2016-03-03 11:47:12 +01:00
assert 'HTTPie %s' % httpie.__version__ in r.stderr
def test_help():
r = http('--help', tolerate_error_exit_status=True)
2019-09-16 13:28:01 +02:00
assert r.exit_status == ExitStatus.SUCCESS
2017-03-10 11:27:38 +01:00
assert 'https://github.com/jakubroztocil/httpie/issues' in r
2016-03-03 11:47:12 +01:00
def test_version():
r = http('--version', tolerate_error_exit_status=True)
2019-09-16 13:28:01 +02:00
assert r.exit_status == ExitStatus.SUCCESS
assert httpie.__version__ == r.strip()
2016-03-03 11:47:12 +01:00
def test_GET(httpbin_both):
r = http('GET', httpbin_both + '/get')
2016-03-03 11:47:12 +01: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
def test_DELETE(httpbin_both):
r = http('DELETE', httpbin_both + '/delete')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
def test_PUT(httpbin_both):
r = http('PUT', httpbin_both + '/put', 'foo=bar')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert r.json['json']['foo'] == 'bar'
def test_POST_JSON_data(httpbin_both):
r = http('POST', httpbin_both + '/post', 'foo=bar')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert r.json['json']['foo'] == 'bar'
def test_POST_form(httpbin_both):
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert '"foo": "bar"' in r
def test_POST_form_multiple_values(httpbin_both):
r = http('--form', 'POST', httpbin_both + '/post', 'foo=bar', 'foo=baz')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert r.json['form'] == {'foo': ['bar', 'baz']}
def test_POST_stdin(httpbin_both):
2016-03-03 11:47:12 +01:00
with open(FILE_PATH) as f:
env = MockEnvironment(stdin=f, stdin_isatty=False)
r = http('--form', 'POST', httpbin_both + '/post', env=env)
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert FILE_CONTENT in r
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
def test_form_POST_file_redirected_stdin(httpbin):
"""
<https://github.com/jakubroztocil/httpie/issues/840>
"""
with open(FILE_PATH) as f:
r = http(
'--form',
'POST',
httpbin + '/post',
'file@' + FILE_PATH,
tolerate_error_exit_status=True,
env=MockEnvironment(
stdin=f,
stdin_isatty=False,
),
)
assert r.exit_status == ExitStatus.ERROR
assert 'cannot be mixed' in r.stderr
def test_headers(httpbin_both):
r = http('GET', httpbin_both + '/headers', 'Foo:bar')
2016-03-03 11:47:12 +01:00
assert HTTP_OK in r
assert '"User-Agent": "HTTPie' in r, r
assert '"Foo": "bar"' in r
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
@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
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')
def test_json_input_preserve_order(httpbin_both):
r = http('PATCH', httpbin_both + '/patch',
2016-03-03 11:47:12 +01:00
'order:={"map":{"1":"first","2":"second"}}')
assert HTTP_OK in r
assert r.json['data'] == \
'{"order": {"map": {"1": "first", "2": "second"}}}'
2020-04-16 11:28:21 +02:00
def test_offline():
r = http(
'--offline',
'https://this-should.never-resolve/foo',
)
2020-04-16 11:41:12 +02:00
assert 'GET /foo' in r
2020-04-16 11:28:21 +02:00
def test_offline_download():
"""Absence of response should be handled gracefully with --download"""
r = http(
'--offline',
'--download',
'https://this-should.never-resolve/foo',
)
2020-04-16 11:41:12 +02:00
assert 'GET /foo' in r