2014-04-24 14:07:31 +02:00
|
|
|
"""
|
|
|
|
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
|
|
|
|
|
|
|
|
"""
|
2020-09-28 12:16:57 +02:00
|
|
|
from io import BytesIO
|
|
|
|
|
2020-04-13 22:06:02 +02:00
|
|
|
from httpie.client import JSON_ACCEPT
|
2021-05-05 14:13:39 +02:00
|
|
|
from .utils import MockEnvironment, http, HTTP_OK
|
|
|
|
from .fixtures import FILE_PATH
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2018-02-22 12:52:57 +01:00
|
|
|
def test_default_headers_case_insensitive(httpbin):
|
|
|
|
"""
|
2023-08-06 14:04:32 +02:00
|
|
|
<https://github.com/httpie/cli/issues/644>
|
2018-02-22 12:52:57 +01:00
|
|
|
"""
|
|
|
|
r = http(
|
|
|
|
'--debug',
|
|
|
|
'--print=H',
|
2024-03-04 18:12:18 +01:00
|
|
|
httpbin + '/post',
|
2018-02-22 12:52:57 +01:00
|
|
|
'CONTENT-TYPE:application/json-patch+json',
|
|
|
|
'a=b',
|
|
|
|
)
|
|
|
|
assert 'CONTENT-TYPE: application/json-patch+json' in r
|
|
|
|
assert 'Content-Type' not in r
|
|
|
|
|
|
|
|
|
2020-04-13 22:12:06 +02:00
|
|
|
# noinspection PyPep8Naming
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestImplicitHTTPMethod:
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_implicit_GET(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http(httpbin + '/get')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_implicit_GET_with_headers(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http(httpbin + '/headers', 'Foo:bar')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-25 12:18:35 +02:00
|
|
|
assert r.json['headers']['Foo'] == 'bar'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_implicit_POST_json(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http(httpbin + '/post', 'hello=world')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-25 12:18:35 +02:00
|
|
|
assert r.json['json'] == {'hello': 'world'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_implicit_POST_form(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--form', httpbin + '/post', 'foo=bar')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-25 12:18:35 +02:00
|
|
|
assert r.json['form'] == {'foo': 'bar'}
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2021-05-24 14:29:54 +02:00
|
|
|
def test_implicit_POST_raw(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--raw', 'foo bar', httpbin + '/post')
|
2021-05-24 14:29:54 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert r.json['data'] == 'foo bar'
|
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_implicit_POST_stdin(self, httpbin):
|
2020-09-28 12:16:57 +02:00
|
|
|
env = MockEnvironment(
|
|
|
|
stdin_isatty=False,
|
|
|
|
stdin=BytesIO(FILE_PATH.read_bytes())
|
|
|
|
)
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--form', httpbin + '/post', env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
|
2014-04-25 11:39:59 +02:00
|
|
|
class TestAutoContentTypeAndAcceptHeaders:
|
2014-04-24 14:07:31 +02:00
|
|
|
"""
|
2020-04-13 22:12:06 +02:00
|
|
|
Test that `Accept` and `Content-Type` correctly default to JSON,
|
|
|
|
but can still be overridden. The same with Content-Type when `--form`
|
|
|
|
`-f` is used.
|
2014-04-24 14:07:31 +02:00
|
|
|
|
|
|
|
"""
|
2014-04-24 15:48:01 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_GET_no_data_no_auto_headers(self, httpbin):
|
2023-08-06 14:04:32 +02:00
|
|
|
# https://github.com/httpie/cli/issues/62
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('GET', httpbin + '/headers')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2014-04-25 12:18:35 +02:00
|
|
|
assert r.json['headers']['Accept'] == '*/*'
|
2016-03-01 16:22:50 +01:00
|
|
|
assert 'Content-Type' not in r.json['headers']
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_POST_no_data_no_auto_headers(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
# JSON headers shouldn't be automatically set for POST with no data.
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('POST', httpbin + '/post')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"Accept": "*/*"' in r
|
|
|
|
assert '"Content-Type": "application/json' not in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_POST_with_data_auto_JSON_headers(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('POST', httpbin + '/post', 'a=b')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2020-04-13 22:06:02 +02:00
|
|
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
2016-07-02 14:18:36 +02:00
|
|
|
assert r.json['headers']['Content-Type'] == 'application/json'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_GET_with_data_auto_JSON_headers(self, httpbin):
|
2014-04-24 14:07:31 +02:00
|
|
|
# JSON headers should automatically be set also for GET with data.
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('POST', httpbin + '/post', 'a=b')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2020-04-13 22:06:02 +02:00
|
|
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
2016-07-02 14:18:36 +02:00
|
|
|
assert r.json['headers']['Content-Type'] == 'application/json'
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2020-04-13 22:12:06 +02:00
|
|
|
def test_POST_explicit_JSON_JSON_ACCEPT(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--json', 'POST', httpbin + '/post')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
2020-04-13 22:06:02 +02:00
|
|
|
assert r.json['headers']['Accept'] == JSON_ACCEPT
|
2014-04-24 14:07:31 +02:00
|
|
|
# Make sure Content-Type gets set even with no data.
|
2023-08-06 14:04:32 +02:00
|
|
|
# https://github.com/httpie/cli/issues/137
|
2014-04-24 14:58:15 +02:00
|
|
|
assert 'application/json' in r.json['headers']['Content-Type']
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_GET_explicit_JSON_explicit_headers(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--json', 'GET', httpbin + '/headers',
|
2014-04-24 15:48:01 +02:00
|
|
|
'Accept:application/xml',
|
|
|
|
'Content-Type:application/xml')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"Accept": "application/xml"' in r
|
|
|
|
assert '"Content-Type": "application/xml"' in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_POST_form_auto_Content_Type(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--form', 'POST', httpbin + '/post')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"Content-Type": "application/x-www-form-urlencoded' in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_POST_form_Content_Type_override(self, httpbin):
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--form', 'POST', httpbin + '/post',
|
2014-04-24 15:48:01 +02:00
|
|
|
'Content-Type:application/xml')
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|
|
|
|
assert '"Content-Type": "application/xml"' in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('GET', httpbin + '/get', env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert 'HTTP/' not in r
|
2014-04-24 14:07:31 +02:00
|
|
|
|
2014-06-28 16:35:57 +02:00
|
|
|
def test_print_overridable_when_stdout_redirected(self, httpbin):
|
2017-12-28 18:17:48 +01:00
|
|
|
env = MockEnvironment(stdin_isatty=True, stdout_isatty=False)
|
2024-03-04 18:12:18 +01:00
|
|
|
r = http('--print=h', 'GET', httpbin + '/get', env=env)
|
2014-04-24 14:58:15 +02:00
|
|
|
assert HTTP_OK in r
|