Added argument parsing tests.

This commit is contained in:
Jakub Roztočil 2012-03-04 10:50:23 +01:00
parent f5d5ec22af
commit 2195280a70
2 changed files with 40 additions and 2 deletions

View File

@ -34,7 +34,7 @@ Will issue the following request:
{"name": "John", "email": "john@example.org"} {"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"}' http PUT httpie.org/pies bool:=true list:=[1,2,3] 'object:={"a": "b", "c": "d"}'

View File

@ -1,6 +1,8 @@
import unittest import unittest
import argparse
from StringIO import StringIO from StringIO import StringIO
from httpie import __main__ from httpie import __main__
from httpie import cli
TERMINAL_COLOR_END = '\x1b[39m' TERMINAL_COLOR_END = '\x1b[39m'
@ -17,7 +19,43 @@ def http(*args, **kwargs):
return stdout.getvalue() 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): class TestHTTPie(unittest.TestCase):