diff --git a/README.md b/README.md index 145307ec..7520615a 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Will issue the following request: {"name": "John", "email": "john@example.org"} -You can pass other types then just strings using the `field:=value` notation. It allows you to set arbitrary JSON to the data fields: +You can pass other types than just strings using the `field:=value` notation. It allows you to set arbitrary JSON to the data fields: http PUT httpie.org/pies bool:=true list:=[1,2,3] 'object:={"a": "b", "c": "d"}' diff --git a/tests.py b/tests.py index 8aecdfb8..7e2f8c23 100644 --- a/tests.py +++ b/tests.py @@ -1,6 +1,8 @@ import unittest +import argparse from StringIO import StringIO from httpie import __main__ +from httpie import cli TERMINAL_COLOR_END = '\x1b[39m' @@ -17,7 +19,43 @@ def http(*args, **kwargs): return stdout.getvalue() -# TODO: moar! +class TestItemParsing(unittest.TestCase): + + 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: + with self.assertRaises(argparse.ArgumentTypeError): + self.kv(item) + + 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"} + }) + class TestHTTPie(unittest.TestCase):