import cgi


def check_api_v3_error(response, status_code):
    """
    Make sure that ``response`` is a valid error response from
    API v3.

    - check http status code to match ``status_code``

    :param response: a ``requests`` response
    :param status_code: http status code to be checked
    """

    assert not response.ok
    assert response.status_code == status_code

    # Should return json anyways..
    content_type = cgi.parse_header(response.headers['content-type'])
    assert content_type[0] == 'application/json'
    assert content_type[1]['charset'] == 'utf-8'
    data = response.json()

    # This is an error!
    assert data['success'] is False
    assert 'error' in data
    assert 'result' not in data

    return data