# Path: prestans/rest/dictionary_response.py
# class DictionaryResponse(Response):
#     """
#     DictionaryResponse serializes dictionaries using the selected_serializer
#     """
# 
#     @property
#     def body(self):
#         """
#         Overridden response does not support md5, text or json properties. _app_iter
#         is set using rules defined by prestans.
# 
#         body getter will return the validated prestans model.
# 
#         webob does the heavy lifting with headers.
#         """
#         return self._app_iter
# 
#     @body.setter
#     def body(self, value):
# 
#         # value should be a dict
#         if not isinstance(value, dict):
#             raise TypeError("%s is not a dictionary" % value.__class__.__name__)
# 
#         #: _app_iter assigned to value
#         #: we need to serialize the contents before we know the length
#         #: deffer the content_length property to be set by getter
#         self._app_iter = value
# 
#     def __call__(self, environ, start_response):
# 
#         if not isinstance(self.body, dict):
#             raise TypeError("body is not a dict")
# 
#         start_response(self.status, self.headerlist)
# 
#         # attempt serializing via registered serializer
#         body_as_string = self._selected_serializer.dumps(self.body)
# 
#         if not isinstance(body_as_string, str):
#             raise TypeError("%s dumps must return a python str not %s" % (
#                 self._selected_serializer.__class__.__name__,
#                 body_as_string.__class__.__name__)
#             )
# 
#         # set content_length
#         self.content_length = len(body_as_string)
# 
#         return [body_as_string]
# 
# Path: prestans/serializer.py
# class JSON(Base):
# 
#     def dumps(self, serializable_object):
#         
#         import json
#         try:
#             return json.dumps(serializable_object, ensure_ascii=False, sort_keys=True)
#         except Exception as exp:
#             raise exception.SerializationFailedError("JSON: %s" % exp)
# 
#     def handler_body_type(self):
#         return DataCollection
# 
#     def content_type(self):
#         return 'application/json'


import logging
import unittest
from prestans.rest import DictionaryResponse
from prestans.serializer import JSON
        from prestans.serializer import Base
            logger=logging.getLogger(),
            serializers=[JSON()],
            default_serializer=JSON()
        )
        dict_response._set_serializer_by_mime_type("application/json")

        def start_response(status, headerlist):
            pass

        self.assertRaises(TypeError, dict_response.__call__, environ={}, start_response=start_response)

    def test_call_with_dictionary(self):
        dict_response = DictionaryResponse(
            charset="utf-8",
            logger=logging.getLogger(),
            serializers=[JSON()],
            default_serializer=JSON()
        )
        dict_response._set_serializer_by_mime_type("application/json")
        dict_response.body = {"key": "value"}

        def start_response(status, headerlist):
            pass

        response = dict_response({}, start_response)
        self.assertEqual(response, ['{"key": "value"}'])
        self.assertEqual(dict_response.content_length, 16)

    def test_call_serializer_returns_non_string_type_raises_type_error(self):


# Path: prestans/rest/dictionary_response.py
# class DictionaryResponse(Response):
#     """
#     DictionaryResponse serializes dictionaries using the selected_serializer
#     """
# 
#     @property
#     def body(self):
#         """
#         Overridden response does not support md5, text or json properties. _app_iter
#         is set using rules defined by prestans.
# 
#         body getter will return the validated prestans model.
# 
#         webob does the heavy lifting with headers.
#         """
#         return self._app_iter
# 
#     @body.setter
#     def body(self, value):
# 
#         # value should be a dict
#         if not isinstance(value, dict):
#             raise TypeError("%s is not a dictionary" % value.__class__.__name__)
# 
#         #: _app_iter assigned to value
#         #: we need to serialize the contents before we know the length
#         #: deffer the content_length property to be set by getter
#         self._app_iter = value
# 
#     def __call__(self, environ, start_response):
# 
#         if not isinstance(self.body, dict):
#             raise TypeError("body is not a dict")
# 
#         start_response(self.status, self.headerlist)
# 
#         # attempt serializing via registered serializer
#         body_as_string = self._selected_serializer.dumps(self.body)
# 
#         if not isinstance(body_as_string, str):
#             raise TypeError("%s dumps must return a python str not %s" % (
#                 self._selected_serializer.__class__.__name__,
#                 body_as_string.__class__.__name__)
#             )
# 
#         # set content_length
#         self.content_length = len(body_as_string)
# 
#         return [body_as_string]
# 
# Path: prestans/serializer.py
# class JSON(Base):
# 
#     def dumps(self, serializable_object):
#         
#         import json
#         try:
#             return json.dumps(serializable_object, ensure_ascii=False, sort_keys=True)
#         except Exception as exp:
#             raise exception.SerializationFailedError("JSON: %s" % exp)
# 
#     def handler_body_type(self):
#         return DataCollection
# 
#     def content_type(self):
#         return 'application/json'


# Path: tests/rest/test_dictionary_response.py
import logging
import unittest
from prestans.rest import DictionaryResponse
from prestans.serializer import JSON
        from prestans.serializer import Base

            logger=logging.getLogger(),
            serializers=[JSON()],
            default_serializer=JSON()
        )
        dict_response._set_serializer_by_mime_type("application/json")

        def start_response(status, headerlist):
            pass

        self.assertRaises(TypeError, dict_response.__call__, environ={}, start_response=start_response)

    def test_call_with_dictionary(self):
        dict_response = DictionaryResponse(
            charset="utf-8",
            logger=logging.getLogger(),
            serializers=[JSON()],
            default_serializer=JSON()
        )
        dict_response._set_serializer_by_mime_type("application/json")
        dict_response.body = {"key": "value"}

        def start_response(status, headerlist):
            pass

        response = dict_response({}, start_response)
        self.assertEqual(response, ['{"key": "value"}'])
        self.assertEqual(dict_response.content_length, 16)

    def test_call_serializer_returns_non_string_type_raises_type_error(self):


        dict_response = DictionaryResponse(