Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions httpie/output/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ def parse_prefixed_json(data: str) -> Tuple[str, str]:
Return a tuple (data prefix, actual JSON body).

"""
matches = re.findall(PREFIX_REGEX, data)
data_prefix = matches[0] if matches else ''
# Only a leading run is a prefix. findall() would take a match
# from inside the body and cut that many characters off the front.
match = re.match(PREFIX_REGEX, data)
data_prefix = match.group(0) if match else ''
body = data[len(data_prefix):]
return data_prefix, body
9 changes: 9 additions & 0 deletions tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from httpie.cli.exceptions import ParseError
from httpie.cli.nested_json import NestedJSONSyntaxError
from httpie.output.formatters.colors import ColorFormatter
from httpie.output.utils import load_prefixed_json
from httpie.utils import JsonDictPreservingDuplicateKeys

from .fixtures import (
Expand Down Expand Up @@ -55,6 +56,14 @@
}"""


def test_prefixed_json_does_not_chop_inside_the_body():
with pytest.raises(ValueError):
load_prefixed_json('{{"a": 1}')
prefix, obj = load_prefixed_json('while(1);{"a": 1}')
assert prefix == 'while(1);'
assert list(obj.items()) == [('a', 1)]


@pytest.mark.parametrize('data_prefix', TEST_JSON_XXSI_PREFIXES)
@pytest.mark.parametrize('json_data', TEST_JSON_VALUES)
@pytest.mark.parametrize('pretty', PRETTY_MAP.keys())
Expand Down
Loading