def test_json_formatter_with_body_preceded_by_non_json_data(
    data_prefix, json_data, pretty
):
    """Test JSON bodies preceded by non-JSON data."""
    body = data_prefix + json.dumps(json_data)
    content_type = 'application/json;charset=utf8'
    responses.add(
        responses.GET,
        DUMMY_URL,
        body=body,
        content_type=content_type,
    )

    colored_output = pretty in {'all', 'colors'}
    env = MockEnvironment(colors=256) if colored_output else None
    r = http('--pretty', pretty, DUMMY_URL, env=env)

    indent = None if pretty in {'none', 'colors'} else 4
    expected_body = data_prefix + json.dumps(json_data, indent=indent)
    if colored_output:
        fmt = ColorFormatter(
            env, format_options={'json': {'format': True, 'indent': 4}}
        )
        expected_body = fmt.format_body(expected_body, content_type)
        # Check to ensure the non-JSON data prefix is colored only one time,
        # meaning it was correctly handled as a whole.
        assert (
            TEST_PREFIX_TOKEN_COLOR + data_prefix in expected_body
        ), expected_body
    assert expected_body in r
Test JSON bodies preceded by non-JSON data.
tests/test_json.py