# Path: hyperadmin/mediatypes/json.py
# class JSON(DataTap):
#     recognized_media_types = [
#         'application/json'
#     ]
# 
#     def __init__(self, api_request, **kwargs):
#         kwargs.setdefault('datatap_class', JSONDataTap)
#         super(JSON, self).__init__(api_request, **kwargs)
# 
# class JSONP(JSON):
#     recognized_media_types = [
#         'text/javascript'
#     ]
# 
#     def get_jsonp_callback(self):
#         #TODO make configurable
#         return self.api_request.params['callback']
# 
#     def serialize(self, content_type, link, state):
#         if self.detect_redirect(link):
#             return self.handle_redirect(link, content_type)
#         content = self.get_content(link, state)
#         callback = self.get_jsonp_callback()
#         return http.HttpResponse(u'%s(%s)' % (callback, content), content_type)


from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson as json
from hyperadmin.mediatypes.json import JSON, JSONP
from common import MediaTypeTestCase




class JsonTestCase(MediaTypeTestCase):
    def get_adaptor(self):
        self.api_request = self.get_api_request()
        return JSON(self.api_request)
    
    def test_queryset_serialize(self):
        endpoint = self.resource.endpoints['list']
        endpoint = endpoint.fork(api_request=self.api_request)
        
        link = endpoint.link_prototypes['list'].get_link()
        state = endpoint.state
        
        response = self.adaptor.serialize(content_type='application/json', link=link, state=state)
        data = json.loads(response.content)
        self.assertEqual(len(data), ContentType.objects.count())
    
    def test_model_instance_serialize(self):
        instance = ContentType.objects.all()[0]
        

# Path: hyperadmin/mediatypes/json.py
# class JSON(DataTap):
#     recognized_media_types = [
#         'application/json'
#     ]
# 
#     def __init__(self, api_request, **kwargs):
#         kwargs.setdefault('datatap_class', JSONDataTap)
#         super(JSON, self).__init__(api_request, **kwargs)
# 
# class JSONP(JSON):
#     recognized_media_types = [
#         'text/javascript'
#     ]
# 
#     def get_jsonp_callback(self):
#         #TODO make configurable
#         return self.api_request.params['callback']
# 
#     def serialize(self, content_type, link, state):
#         if self.detect_redirect(link):
#             return self.handle_redirect(link, content_type)
#         content = self.get_content(link, state)
#         callback = self.get_jsonp_callback()
#         return http.HttpResponse(u'%s(%s)' % (callback, content), content_type)


# Path: hyperadmin/tests/mediatypes/test_json.py
from django.contrib.contenttypes.models import ContentType
from django.utils import simplejson as json
from hyperadmin.mediatypes.json import JSON, JSONP
from common import MediaTypeTestCase





class JsonTestCase(MediaTypeTestCase):
    def get_adaptor(self):
        self.api_request = self.get_api_request()
        return JSON(self.api_request)
    
    def test_queryset_serialize(self):
        endpoint = self.resource.endpoints['list']
        endpoint = endpoint.fork(api_request=self.api_request)
        
        link = endpoint.link_prototypes['list'].get_link()
        state = endpoint.state
        
        response = self.adaptor.serialize(content_type='application/json', link=link, state=state)
        data = json.loads(response.content)
        self.assertEqual(len(data), ContentType.objects.count())
    
    def test_model_instance_serialize(self):
        instance = ContentType.objects.all()[0]
        

        endpoint = self.resource.endpoints['detail']