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.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)
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
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
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)
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)
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/"}')
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/"}}')
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)
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'))
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/"}})', )
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/"}})' )