コード例 #1
0
    def test_custom_api_serializer(self):
        """Confirm that an Api can use a custom serializer"""

        # Origin: https://github.com/django-tastypie/django-tastypie/pull/817

        class JSONSerializer(Serializer):
            formats = ('json', )

        api = Api(serializer_class=JSONSerializer)
        api.register(NoteResource())

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            resp['content-type'],
            'application/json',
            msg="Expected application/json response but received %s" %
            resp['content-type'])

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'application/xml'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(
            resp['content-type'],
            'application/json',
            msg="Expected application/json response but received %s" %
            resp['content-type'])
コード例 #2
0
    def test_top_level_jsonp(self):
        api = Api()
        api.serializer.formats = ['jsonp']
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': 'foo'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'].split(';')[0], 'text/javascript')
        self.assertEqual(
            resp.content.decode('utf-8'),
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})'
        )

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': ''}

        # Regression: We expect this, which is fine, but this used to
        #             be an import error.
        with self.assertRaises(BadRequest):
            api.top_level(request)
コード例 #3
0
ファイル: api.py プロジェクト: Adusei/django-tastypie
    def test_custom_api_serializer(self):
        """Confirm that an Api can use a custom serializer"""

        # Origin: https://github.com/django-tastypie/django-tastypie/pull/817

        class JSONSerializer(Serializer):
            formats = ('json', )

        api = Api(serializer_class=JSONSerializer)
        api.register(NoteResource())

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'], 'application/json',
                         msg="Expected application/json response but received %s" % resp['content-type'])

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'application/xml'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'], 'application/json',
                         msg="Expected application/json response but received %s" % resp['content-type'])
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': 'foo'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'].split(';')[0], 'text/javascript')
        self.assertEqual(
            resp.content,
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})'
        )

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': ''}

        try:
            resp = api.top_level(request)
            self.fail("Broken callback didn't fail!")
        except BadRequest:
            # Regression: We expect this, which is fine, but this used to
            #             be an import error.
            pass
コード例 #5
0
ファイル: api.py プロジェクト: nim65s/django-tastypie
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {"HTTP_ACCEPT": "text/javascript"}
        request.GET = {"callback": "foo"}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["content-type"].split(";")[0], "text/javascript")
        self.assertEqual(
            resp.content.decode("utf-8"),
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})',
        )

        request = HttpRequest()
        request.META = {"HTTP_ACCEPT": "text/javascript"}
        request.GET = {"callback": ""}

        try:
            resp = api.top_level(request)
            self.fail("Broken callback didn't fail!")
        except BadRequest:
            # Regression: We expect this, which is fine, but this used to
            #             be an import error.
            pass
コード例 #6
0
ファイル: api.py プロジェクト: Adusei/django-tastypie
    def test_top_level_include_schema_content(self):
        api = Api()

        note_resource = NoteResource()
        user_resource = UserResource()

        api.register(note_resource)
        api.register(user_resource)

        request = HttpRequest()
        request.GET = {'fullschema': 'true'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)

        content = json.loads(resp.content.decode('utf-8'))

        content['notes']['schema'] = adjust_schema(content['notes']['schema'])
        content['users']['schema'] = adjust_schema(content['users']['schema'])

        dummy_request = HttpRequest()
        dummy_request.method = 'GET'

        notes_schema = adjust_schema(json.loads(note_resource.get_schema(dummy_request).content.decode('utf-8')))
        user_schema = adjust_schema(json.loads(user_resource.get_schema(dummy_request).content.decode('utf-8')))

        self.assertEqual(content['notes']['list_endpoint'], '/api/v1/notes/')
        self.assertEqual(content['notes']['schema'], notes_schema)

        self.assertEqual(content['users']['list_endpoint'], '/api/v1/users/')
        self.assertEqual(content['users']['schema'], user_schema)
コード例 #7
0
    def test_top_level_include_schema_content(self):
        api = Api()

        note_resource = NoteResource()
        user_resource = UserResource()

        api.register(note_resource)
        api.register(user_resource)

        request = HttpRequest()
        request.GET = {'fullschema': 'true'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)

        content = json.loads(resp.content.decode('utf-8'))

        content['notes']['schema'] = adjust_schema(content['notes']['schema'])
        content['users']['schema'] = adjust_schema(content['users']['schema'])

        dummy_request = HttpRequest()
        dummy_request.method = 'GET'

        notes_schema = adjust_schema(json.loads(note_resource.get_schema(dummy_request).content.decode('utf-8')))
        user_schema = adjust_schema(json.loads(user_resource.get_schema(dummy_request).content.decode('utf-8')))

        self.assertEqual(content['notes']['list_endpoint'], '/api/v1/notes/')
        self.assertEqual(content['notes']['schema'], notes_schema)

        self.assertEqual(content['users']['list_endpoint'], '/api/v1/users/')
        self.assertEqual(content['users']['schema'], user_schema)
コード例 #8
0
ファイル: api.py プロジェクト: nim65s/django-tastypie
    def test_top_level_include_schema_content(self):
        api = Api()

        note_resource = NoteResource()
        user_resource = UserResource()

        api.register(note_resource)
        api.register(user_resource)

        request = HttpRequest()
        request.GET = {"fullschema": "true"}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)

        content = json.loads(resp.content.decode("utf-8"))

        content["notes"]["schema"] = adjust_schema(content["notes"]["schema"])
        content["users"]["schema"] = adjust_schema(content["users"]["schema"])

        dummy_request = HttpRequest()
        dummy_request.method = "GET"

        notes_schema = adjust_schema(json.loads(note_resource.get_schema(dummy_request).content.decode("utf-8")))
        user_schema = adjust_schema(json.loads(user_resource.get_schema(dummy_request).content.decode("utf-8")))

        self.assertEqual(content["notes"]["list_endpoint"], "/api/v1/notes/")
        self.assertEqual(content["notes"]["schema"], notes_schema)

        self.assertEqual(content["users"]["list_endpoint"], "/api/v1/users/")
        self.assertEqual(content["users"]["schema"], user_schema)
コード例 #9
0
ファイル: api.py プロジェクト: codysoyland/django-tastypie
 def test_top_level(self):
     api = Api()
     api.register(NoteResource())
     api.register(UserResource())
     request = HttpRequest()
     
     resp = api.top_level(request)
     self.assertEqual(resp.status_code, 200)
     self.assertEqual(resp.content, '{"notes": "/api/v1/notes/", "users": "/api/v1/users/"}')
コード例 #10
0
ファイル: api.py プロジェクト: Adusei/django-tastypie
    def test_top_level(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content.decode('utf-8'), '{"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}')
コード例 #11
0
    def test_top_level(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content.decode('utf-8'), '{"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}}')
コード例 #12
0
ファイル: api.py プロジェクト: Adusei/django-tastypie
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': 'foo'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'].split(';')[0], 'text/javascript')
        self.assertEqual(resp.content.decode('utf-8'), 'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})')

        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': ''}

        # Regression: We expect this, which is fine, but this used to
        #             be an import error.
        with self.assertRaises(BadRequest):
            api.top_level(request)
コード例 #13
0
    def test_jsonp_not_on_by_default(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': 'foo'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'].split(';')[0], 'application/json')
        self.assertFalse("foo" in resp.content.decode('utf-8'))
コード例 #14
0
ファイル: api.py プロジェクト: novel/django-tastypie
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {"HTTP_ACCEPT": "text/javascript"}
        request.GET = {"callback": "foo"}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp["content-type"].split(";")[0], "text/javascript")
        self.assertEqual(
            resp.content,
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})',
        )
コード例 #15
0
ファイル: api.py プロジェクト: tubaman/django-tastypie
    def test_top_level_jsonp(self):
        api = Api()
        api.register(NoteResource())
        api.register(UserResource())
        request = HttpRequest()
        request.META = {'HTTP_ACCEPT': 'text/javascript'}
        request.GET = {'callback': 'foo'}

        resp = api.top_level(request)
        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp['content-type'].split(';')[0], 'text/javascript')
        self.assertEqual(
            resp.content,
            'foo({"notes": {"list_endpoint": "/api/v1/notes/", "schema": "/api/v1/notes/schema/"}, "users": {"list_endpoint": "/api/v1/users/", "schema": "/api/v1/users/schema/"}})'
        )