2012-03-02 03:39:22 +03:00
|
|
|
import unittest
|
2012-03-04 12:50:23 +03:00
|
|
|
import argparse
|
2012-03-02 03:40:40 +03:00
|
|
|
from StringIO import StringIO
|
2012-03-02 20:35:33 +03:00
|
|
|
from httpie import __main__
|
2012-03-04 12:50:23 +03:00
|
|
|
from httpie import cli
|
2012-03-02 03:39:22 +03:00
|
|
|
|
|
|
|
|
2012-03-04 03:36:39 +03:00
|
|
|
TERMINAL_COLOR_END = '\x1b[39m'
|
|
|
|
|
|
|
|
|
2012-03-02 03:40:40 +03:00
|
|
|
def http(*args, **kwargs):
|
2012-03-04 03:36:39 +03:00
|
|
|
http_kwargs = {
|
|
|
|
'stdin_isatty': True,
|
|
|
|
'stdout_isatty': False
|
|
|
|
}
|
|
|
|
http_kwargs.update(kwargs)
|
|
|
|
stdout = http_kwargs.setdefault('stdout', StringIO())
|
2012-03-04 05:13:50 +03:00
|
|
|
__main__.main(args=args, **http_kwargs)
|
2012-03-02 03:40:40 +03:00
|
|
|
return stdout.getvalue()
|
|
|
|
|
|
|
|
|
2012-03-04 13:22:21 +03:00
|
|
|
class BaseTest(unittest.TestCase):
|
|
|
|
|
|
|
|
def assertIn(self, member, container, msg=None):
|
|
|
|
sup = super(BaseTest, self)
|
|
|
|
if hasattr('sup', 'assertIn'):
|
|
|
|
sup.assertIn(member, container, msg)
|
|
|
|
else:
|
|
|
|
self.assert_(member in container, msg)
|
|
|
|
|
|
|
|
def assertNotIn(self, member, container, msg=None):
|
|
|
|
sup = super(BaseTest, self)
|
|
|
|
if hasattr('sup', 'assertNotIn'):
|
|
|
|
sup.assertIn(member, container, msg)
|
|
|
|
else:
|
|
|
|
self.assert_(member not in container, msg)
|
|
|
|
|
|
|
|
def assertDictEqual(self, d1, d2, msg=None):
|
|
|
|
sup = super(BaseTest, self)
|
|
|
|
if hasattr('sup', 'assertDictEqual'):
|
|
|
|
self.assertDictEqual(d1, d2, msg)
|
|
|
|
else:
|
|
|
|
sup.assertEqual(set(d1.keys()), set(d2.keys()), msg)
|
|
|
|
sup.assertEqual(sorted(d1.values()), sorted(d2.values()), msg)
|
|
|
|
|
|
|
|
|
|
|
|
class TestItemParsing(BaseTest):
|
2012-03-04 12:50:23 +03:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.kv = cli.KeyValueType(
|
|
|
|
cli.SEP_HEADERS,
|
|
|
|
cli.SEP_DATA,
|
|
|
|
cli.SEP_DATA_RAW_JSON
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_invalid_items(self):
|
|
|
|
items = ['no-separator']
|
|
|
|
for item in items:
|
2012-03-04 13:22:21 +03:00
|
|
|
self.assertRaises(argparse.ArgumentTypeError,
|
|
|
|
lambda: self.kv(item))
|
2012-03-04 12:50:23 +03:00
|
|
|
|
|
|
|
def test_valid_items(self):
|
|
|
|
headers, data = cli.parse_items([
|
|
|
|
self.kv('string=value'),
|
|
|
|
self.kv('header:value'),
|
|
|
|
self.kv('list:=["a", 1, {}, false]'),
|
|
|
|
self.kv('obj:={"a": "b"}'),
|
|
|
|
self.kv('eh:'),
|
|
|
|
self.kv('ed='),
|
|
|
|
self.kv('bool:=true'),
|
|
|
|
])
|
|
|
|
self.assertDictEqual(headers, {
|
|
|
|
'header': 'value',
|
|
|
|
'eh': ''
|
|
|
|
})
|
|
|
|
self.assertDictEqual(data, {
|
|
|
|
"ed": "",
|
|
|
|
"string": "value",
|
|
|
|
"bool": True,
|
|
|
|
"list": ["a", 1, {}, False],
|
|
|
|
"obj": {"a": "b"}
|
|
|
|
})
|
|
|
|
|
2012-03-02 03:40:40 +03:00
|
|
|
|
2012-03-04 13:22:21 +03:00
|
|
|
class TestHTTPie(BaseTest):
|
2012-03-02 03:40:40 +03:00
|
|
|
|
|
|
|
def test_get(self):
|
|
|
|
http('GET', 'http://httpbin.org/get')
|
|
|
|
|
|
|
|
def test_json(self):
|
|
|
|
response = http('POST', 'http://httpbin.org/post', 'foo=bar')
|
|
|
|
self.assertIn('"foo": "bar"', response)
|
|
|
|
|
|
|
|
def test_form(self):
|
|
|
|
response = http('POST', '--form', 'http://httpbin.org/post', 'foo=bar')
|
|
|
|
self.assertIn('"foo": "bar"', response)
|
|
|
|
|
|
|
|
def test_headers(self):
|
|
|
|
response = http('GET', 'http://httpbin.org/headers', 'Foo:bar')
|
|
|
|
self.assertIn('"User-Agent": "HTTPie', response)
|
|
|
|
self.assertIn('"Foo": "bar"', response)
|
|
|
|
|
|
|
|
|
2012-03-04 13:22:21 +03:00
|
|
|
class TestPrettyFlag(BaseTest):
|
2012-03-04 03:36:39 +03:00
|
|
|
"""Test the --pretty / --ugly flag handling."""
|
|
|
|
|
|
|
|
def test_pretty_enabled_by_default(self):
|
|
|
|
r = http('GET', 'http://httpbin.org/get', stdout_isatty=True)
|
|
|
|
self.assertIn(TERMINAL_COLOR_END, r)
|
|
|
|
|
|
|
|
def test_pretty_enabled_by_default_unless_stdin_redirected(self):
|
|
|
|
r = http('GET', 'http://httpbin.org/get', stdout_isatty=False)
|
|
|
|
self.assertNotIn(TERMINAL_COLOR_END, r)
|
|
|
|
|
|
|
|
def test_force_pretty(self):
|
|
|
|
r = http('GET', '--pretty', 'http://httpbin.org/get', stdout_isatty=False)
|
|
|
|
self.assertIn(TERMINAL_COLOR_END, r)
|
|
|
|
|
|
|
|
def test_force_ugly(self):
|
|
|
|
r = http('GET', '--ugly', 'http://httpbin.org/get', stdout_isatty=True)
|
|
|
|
self.assertNotIn(TERMINAL_COLOR_END, r)
|
|
|
|
|
|
|
|
|
2012-03-02 03:40:40 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|