You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2026-04-24 19:53:55 +02:00
Add nested JSON syntax to the HTTPie DSL (#1224)
* Add support for nested JSON syntax (#1169) Co-authored-by: Batuhan Taskaya <isidentical@gmail.com> Co-authored-by: Jakub Roztocil <jakub@roztocil.co> * minor improvements * unpack top level lists * Write more docs * doc style changes * fix double quotes Co-authored-by: Mickaël Schoentgen <contact@tiger-222.fr> Co-authored-by: Jakub Roztocil <jakub@roztocil.co>
This commit is contained in:
+5
-1
@@ -79,6 +79,8 @@ class TestItemParsing:
|
||||
self.key_value_arg('Empty-Header;'),
|
||||
self.key_value_arg('list:=["a", 1, {}, false]'),
|
||||
self.key_value_arg('obj:={"a": "b"}'),
|
||||
self.key_value_arg(r'nested\[2\][a][]=1'),
|
||||
self.key_value_arg('nested[2][a][]:=1'),
|
||||
self.key_value_arg('ed='),
|
||||
self.key_value_arg('bool:=true'),
|
||||
self.key_value_arg('file@' + FILE_PATH_ARG),
|
||||
@@ -105,7 +107,9 @@ class TestItemParsing:
|
||||
'ed': '',
|
||||
'string': 'value',
|
||||
'bool': True,
|
||||
'list': ['a', 1, {}, False],
|
||||
'list': ['a', 1, load_json_preserve_order_and_dupe_keys('{}'), False],
|
||||
'nested[2]': {'a': ['1']},
|
||||
'nested': [None, None, {'a': [1]}],
|
||||
'obj': load_json_preserve_order_and_dupe_keys('{"a": "b"}'),
|
||||
'string-embed': FILE_CONTENT
|
||||
}
|
||||
|
||||
@@ -152,3 +152,60 @@ def test_complex_json_arguments_with_non_json(httpbin, request_type, value):
|
||||
)
|
||||
|
||||
cm.match('Can\'t use complex JSON value types')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('input_json, expected_json', [
|
||||
# Examples taken from https://www.w3.org/TR/html-json-forms/
|
||||
(
|
||||
['bottle-on-wall:=1', 'bottle-on-wall:=2', 'bottle-on-wall:=3'],
|
||||
{'bottle-on-wall': [1, 2, 3]},
|
||||
),
|
||||
(
|
||||
['pet[species]=Dahut', 'pet[name]:="Hypatia"', 'kids[1]=Thelma', 'kids[0]:="Ashley"'],
|
||||
{'pet': {'species': 'Dahut', 'name': 'Hypatia'}, 'kids': ['Ashley', 'Thelma']},
|
||||
),
|
||||
(
|
||||
['pet[0][species]=Dahut', 'pet[0][name]=Hypatia', 'pet[1][species]=Felis Stultus', 'pet[1][name]:="Billie"'],
|
||||
{'pet': [{'species': 'Dahut', 'name': 'Hypatia'}, {'species': 'Felis Stultus', 'name': 'Billie'}]},
|
||||
),
|
||||
(
|
||||
['wow[such][deep][3][much][power][!]=Amaze'],
|
||||
{'wow': {'such': {'deep': [None, None, None, {'much': {'power': {'!': 'Amaze'}}}]}}},
|
||||
),
|
||||
(
|
||||
['mix=scalar', 'mix[0]=array 1', 'mix[2]:="array 2"', 'mix[key]:="key key"', 'mix[car]=car key'],
|
||||
{'mix': {'': 'scalar', '0': 'array 1', '2': 'array 2', 'key': 'key key', 'car': 'car key'}},
|
||||
),
|
||||
(
|
||||
['highlander[]=one'],
|
||||
{'highlander': ['one']},
|
||||
),
|
||||
(
|
||||
['error[good]=BOOM!', 'error[bad:="BOOM BOOM!"'],
|
||||
{'error': {'good': 'BOOM!'}, 'error[bad': 'BOOM BOOM!'},
|
||||
),
|
||||
(
|
||||
['special[]:=true', 'special[]:=false', 'special[]:="true"', 'special[]:=null'],
|
||||
{'special': [True, False, 'true', None]},
|
||||
),
|
||||
(
|
||||
[r'\[\]:=1', r'escape\[d\]:=1', r'escaped\[\]:=1', r'e\[s\][c][a][p]\[ed\][]:=1'],
|
||||
{'[]': 1, 'escape[d]': 1, 'escaped[]': 1, 'e[s]': {'c': {'a': {'p': {'[ed]': [1]}}}}},
|
||||
),
|
||||
(
|
||||
['[]:=1', '[]=foo'],
|
||||
[1, 'foo'],
|
||||
),
|
||||
(
|
||||
[']:=1', '[]1:=1', '[1]]:=1'],
|
||||
{']': 1, '[]1': 1, '[1]]': 1},
|
||||
),
|
||||
])
|
||||
def test_nested_json_syntax(input_json, expected_json, httpbin_both):
|
||||
r = http(httpbin_both + '/post', *input_json)
|
||||
assert r.json['json'] == expected_json
|
||||
|
||||
|
||||
def test_nested_json_sparse_array(httpbin_both):
|
||||
r = http(httpbin_both + '/post', 'test[0]:=1', 'test[100]:=1')
|
||||
assert len(r.json['json']['test']) == 101
|
||||
|
||||
@@ -228,7 +228,7 @@ def http(
|
||||
# noinspection PyUnresolvedReferences
|
||||
"""
|
||||
Run HTTPie and capture stderr/out and exit status.
|
||||
Content writtent to devnull will be captured only if
|
||||
Content written to devnull will be captured only if
|
||||
env.devnull is set manually.
|
||||
|
||||
Invoke `httpie.core.main()` with `args` and `kwargs`,
|
||||
|
||||
Reference in New Issue
Block a user