"""
Utilities for testing.

:copyright: Copyright 2013-2016, Dorian Pula <dorian.pula@amber-penguin-software.ca>
:license: AGPL v3+
"""

import json

from werkzeug import wrappers


def convert_response_into_json(response: wrappers.Response) -> dict:
    """
    Converts a Flask Response into a working dict.
    :param response: The response to convert.
    :return: A dictionary representing the JSON.
    """
    content_type = response.content_type.lower()
    if content_type != "application/json":
        raise ValueError(f"Expected a JSON response.  Got \"{content_type}\" instead.")
    json_text = response.get_data(as_text=True)
    return json.loads(json_text)
