mirror of
https://github.com/httpie/cli.git
synced 2025-08-17 08:31:14 +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:
@ -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
|
||||
|
Reference in New Issue
Block a user