1
0
mirror of https://github.com/httpie/cli.git synced 2024-11-24 08:22:22 +02:00
httpie-cli/tests/test_defaults.py

107 lines
3.9 KiB
Python
Raw Normal View History

2014-04-24 15:07:31 +03:00
"""
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
"""
from tests import TestEnvironment, http, httpbin, HTTP_OK
from tests.fixtures import FILE_PATH
2014-04-24 15:07:31 +03:00
class TestImplicitHTTPMethod:
2014-04-24 15:07:31 +03:00
def test_implicit_GET(self):
r = http(httpbin('/get'))
assert HTTP_OK in r
2014-04-24 15:07:31 +03:00
def test_implicit_GET_with_headers(self):
2014-04-24 16:48:01 +03:00
r = http(httpbin('/headers'), 'Foo:bar')
assert HTTP_OK in r
assert '"Foo": "bar"' in r
2014-04-24 15:07:31 +03:00
def test_implicit_POST_json(self):
2014-04-24 16:48:01 +03:00
r = http(httpbin('/post'), 'hello=world')
assert HTTP_OK in r
assert r'\"hello\": \"world\"' in r
2014-04-24 15:07:31 +03:00
def test_implicit_POST_form(self):
2014-04-24 16:48:01 +03:00
r = http('--form', httpbin('/post'), 'foo=bar')
assert HTTP_OK in r
assert '"foo": "bar"' in r
2014-04-24 15:07:31 +03:00
def test_implicit_POST_stdin(self):
with open(FILE_PATH) as f:
2014-04-24 16:48:01 +03:00
env = TestEnvironment(stdin_isatty=False, stdin=f)
r = http('--form', httpbin('/post'), env=env)
assert HTTP_OK in r
2014-04-24 15:07:31 +03:00
class TestAutoContentTypeAndAcceptHeaders:
2014-04-24 15:07:31 +03:00
"""
Test that Accept and Content-Type correctly defaults to JSON,
but can still be overridden. The same with Content-Type when --form
-f is used.
"""
2014-04-24 16:48:01 +03:00
2014-04-24 15:07:31 +03:00
def test_GET_no_data_no_auto_headers(self):
# https://github.com/jkbr/httpie/issues/62
2014-04-24 16:48:01 +03:00
r = http('GET', httpbin('/headers'))
assert HTTP_OK in r
assert '"Accept": "*/*"' in r
assert '"Content-Type": "application/json' not in r
2014-04-24 15:07:31 +03:00
def test_POST_no_data_no_auto_headers(self):
# JSON headers shouldn't be automatically set for POST with no data.
2014-04-24 16:48:01 +03:00
r = http('POST', httpbin('/post'))
assert HTTP_OK in r
assert '"Accept": "*/*"' in r
assert '"Content-Type": "application/json' not in r
2014-04-24 15:07:31 +03:00
def test_POST_with_data_auto_JSON_headers(self):
2014-04-24 16:48:01 +03:00
r = http('POST', httpbin('/post'), 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r
assert '"Content-Type": "application/json; charset=utf-8' in r
2014-04-24 15:07:31 +03:00
def test_GET_with_data_auto_JSON_headers(self):
# JSON headers should automatically be set also for GET with data.
2014-04-24 16:48:01 +03:00
r = http('POST', httpbin('/post'), 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r
assert '"Content-Type": "application/json; charset=utf-8' in r
2014-04-24 15:07:31 +03:00
def test_POST_explicit_JSON_auto_JSON_accept(self):
2014-04-24 16:48:01 +03:00
r = http('--json', 'POST', httpbin('/post'))
assert HTTP_OK in r
assert r.json['headers']['Accept'] == 'application/json'
2014-04-24 15:07:31 +03:00
# Make sure Content-Type gets set even with no data.
# https://github.com/jkbr/httpie/issues/137
assert 'application/json' in r.json['headers']['Content-Type']
2014-04-24 15:07:31 +03:00
def test_GET_explicit_JSON_explicit_headers(self):
2014-04-24 16:48:01 +03:00
r = http('--json', 'GET', httpbin('/headers'),
'Accept:application/xml',
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Accept": "application/xml"' in r
assert '"Content-Type": "application/xml"' in r
2014-04-24 15:07:31 +03:00
def test_POST_form_auto_Content_Type(self):
2014-04-24 16:48:01 +03:00
r = http('--form', 'POST', httpbin('/post'))
assert HTTP_OK in r
assert '"Content-Type": "application/x-www-form-urlencoded' in r
2014-04-24 15:07:31 +03:00
def test_POST_form_Content_Type_override(self):
2014-04-24 16:48:01 +03:00
r = http('--form', 'POST', httpbin('/post'),
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Content-Type": "application/xml"' in r
2014-04-24 15:07:31 +03:00
def test_print_only_body_when_stdout_redirected_by_default(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('GET', httpbin('/get'), env=env)
assert 'HTTP/' not in r
2014-04-24 15:07:31 +03:00
def test_print_overridable_when_stdout_redirected(self):
2014-04-24 16:48:01 +03:00
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=h', 'GET', httpbin('/get'), env=env)
assert HTTP_OK in r