httpie-cli/tests/test_defaults.py

107 lines
4.1 KiB
Python
Raw Normal View History

2014-04-24 14:07:31 +02:00
"""
Tests for the provided defaults regarding HTTP method, and --json vs. --form.
"""
from utils import TestEnvironment, http, HTTP_OK, no_content_type
from fixtures import FILE_PATH
2014-04-24 14:07:31 +02:00
class TestImplicitHTTPMethod:
def test_implicit_GET(self, httpbin):
r = http(httpbin.url + '/get')
assert HTTP_OK in r
2014-04-24 14:07:31 +02:00
def test_implicit_GET_with_headers(self, httpbin):
r = http(httpbin.url + '/headers', 'Foo:bar')
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
def test_implicit_POST_json(self, httpbin):
r = http(httpbin.url + '/post', 'hello=world')
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
def test_implicit_POST_form(self, httpbin):
r = http('--form', httpbin.url + '/post', 'foo=bar')
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
def test_implicit_POST_stdin(self, httpbin):
2014-04-24 14:07:31 +02:00
with open(FILE_PATH) as f:
2014-04-24 15:48:01 +02:00
env = TestEnvironment(stdin_isatty=False, stdin=f)
r = http('--form', httpbin.url + '/post', env=env)
assert HTTP_OK in r
2014-04-24 14:07:31 +02:00
class TestAutoContentTypeAndAcceptHeaders:
2014-04-24 14:07:31 +02: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 15:48:01 +02:00
def test_GET_no_data_no_auto_headers(self, httpbin):
2015-07-03 18:55:45 +02:00
# https://github.com/jkbrzt/httpie/issues/62
r = http('GET', httpbin.url + '/headers')
assert HTTP_OK in r
2014-04-25 12:18:35 +02:00
assert r.json['headers']['Accept'] == '*/*'
assert no_content_type(r.json['headers'])
2014-04-24 14:07:31 +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.
r = http('POST', httpbin.url + '/post')
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
def test_POST_with_data_auto_JSON_headers(self, httpbin):
r = http('POST', httpbin.url + '/post', 'a=b')
assert HTTP_OK in r
assert '"Accept": "application/json"' in r
assert '"Content-Type": "application/json' in r
2014-04-24 14:07:31 +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.
r = http('POST', httpbin.url + '/post', 'a=b')
assert HTTP_OK in r
2014-04-26 17:16:11 +02:00
assert '"Accept": "application/json"' in r, r
assert '"Content-Type": "application/json' in r
2014-04-24 14:07:31 +02:00
def test_POST_explicit_JSON_auto_JSON_accept(self, httpbin):
r = http('--json', 'POST', httpbin.url + '/post')
assert HTTP_OK in r
assert r.json['headers']['Accept'] == 'application/json'
2014-04-24 14:07:31 +02:00
# Make sure Content-Type gets set even with no data.
2015-07-03 18:55:45 +02:00
# https://github.com/jkbrzt/httpie/issues/137
assert 'application/json' in r.json['headers']['Content-Type']
2014-04-24 14:07:31 +02:00
def test_GET_explicit_JSON_explicit_headers(self, httpbin):
r = http('--json', 'GET', httpbin.url + '/headers',
2014-04-24 15:48:01 +02:00
'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 14:07:31 +02:00
def test_POST_form_auto_Content_Type(self, httpbin):
r = http('--form', 'POST', httpbin.url + '/post')
assert HTTP_OK in r
assert '"Content-Type": "application/x-www-form-urlencoded' in r
2014-04-24 14:07:31 +02:00
def test_POST_form_Content_Type_override(self, httpbin):
r = http('--form', 'POST', httpbin.url + '/post',
2014-04-24 15:48:01 +02:00
'Content-Type:application/xml')
assert HTTP_OK in r
assert '"Content-Type": "application/xml"' in r
2014-04-24 14:07:31 +02:00
def test_print_only_body_when_stdout_redirected_by_default(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('GET', httpbin.url + '/get', env=env)
assert 'HTTP/' not in r
2014-04-24 14:07:31 +02:00
def test_print_overridable_when_stdout_redirected(self, httpbin):
2014-04-24 15:48:01 +02:00
env = TestEnvironment(stdin_isatty=True, stdout_isatty=False)
r = http('--print=h', 'GET', httpbin.url + '/get', env=env)
assert HTTP_OK in r