<s> from __future__ import unicode_literals <EOL> import copy <EOL> import sys <EOL> from django . test import TestCase <EOL> from rest_framework import status <EOL> from rest_framework . decorators import api_view <EOL> from rest_framework . response import Response <EOL> from rest_framework . settings import api_settings <EOL> from rest_framework . test import APIRequestFactory <EOL> from rest_framework . views import APIView <EOL> factory = APIRequestFactory ( ) <EOL> if sys . version_info [ : <NUM_LIT:2> ] >= ( <NUM_LIT:3> , <NUM_LIT:4> ) : <EOL> JSON_ERROR = '<STR_LIT>' <EOL> else : <EOL> JSON_ERROR = '<STR_LIT>' <EOL> class BasicView ( APIView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT:GET>' } ) <EOL> def post ( self , request , * args , ** kwargs ) : <EOL> return Response ( { '<STR_LIT>' : '<STR_LIT:POST>' , '<STR_LIT:data>' : request . data } ) <EOL> @ api_view ( [ '<STR_LIT:GET>' , '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> def basic_view ( request ) : <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return { '<STR_LIT>' : '<STR_LIT:GET>' } <EOL> elif request . method == '<STR_LIT:POST>' : <EOL> return { '<STR_LIT>' : '<STR_LIT:POST>' , '<STR_LIT:data>' : request . data } <EOL> elif request . method == '<STR_LIT>' : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:data>' : request . data } <EOL> elif request . method == '<STR_LIT>' : <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT:data>' : request . data } <EOL> class ErrorView ( APIView ) : <EOL> def get ( self , request , * args , ** kwargs ) : <EOL> raise Exception <EOL> @ api_view ( [ '<STR_LIT:GET>' ] ) <EOL> def error_view ( request ) : <EOL> raise Exception <EOL> def sanitise_json_error ( error_dict ) : <EOL> """<STR_LIT>""" <EOL> ret = copy . copy ( error_dict ) <EOL> chop = len ( JSON_ERROR ) <EOL> ret [ '<STR_LIT>' ] = ret [ '<STR_LIT>' ] [ : chop ] <EOL> return ret <EOL> class ClassBasedViewIntegrationTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . view = BasicView . as_view ( ) <EOL> def test_400_parse_error ( self ) : <EOL> request = factory . post ( '<STR_LIT:/>' , '<STR_LIT>' , content_type = '<STR_LIT:application/json>' ) <EOL> response = self . view ( request ) <EOL> expected = { <EOL> '<STR_LIT>' : JSON_ERROR <EOL> } <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( sanitise_json_error ( response . data ) , expected ) <EOL> class FunctionBasedViewIntegrationTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . view = basic_view <EOL> def test_400_parse_error ( self ) : <EOL> request = factory . post ( '<STR_LIT:/>' , '<STR_LIT>' , content_type = '<STR_LIT:application/json>' ) <EOL> response = self . view ( request ) <EOL> expected = { <EOL> '<STR_LIT>' : JSON_ERROR <EOL> } <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( sanitise_json_error ( response . data ) , expected ) <EOL> class TestCustomExceptionHandler ( TestCase ) : <EOL> def setUp ( self ) : <EOL> self . DEFAULT_HANDLER = api_settings . EXCEPTION_HANDLER <EOL> def exception_handler ( exc , request ) : <EOL> return Response ( '<STR_LIT>' , status = status . HTTP_400_BAD_REQUEST ) <EOL> api_settings . EXCEPTION_HANDLER = exception_handler <EOL> def tearDown ( self ) : <EOL> api_settings . EXCEPTION_HANDLER = self . DEFAULT_HANDLER <EOL> def test_class_based_view_exception_handler ( self ) : <EOL> view = ErrorView . as_view ( ) <EOL> request = factory . get ( '<STR_LIT:/>' , content_type = '<STR_LIT:application/json>' ) <EOL> response = view ( request ) <EOL> expected = '<STR_LIT>' <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , expected ) <EOL> def test_function_based_view_exception_handler ( self ) : <EOL> view = error_view <EOL> request = factory . get ( '<STR_LIT:/>' , content_type = '<STR_LIT:application/json>' ) <EOL> response = view ( request ) <EOL> expected = '<STR_LIT>' <EOL> self . assertEqual ( response . status_code , status . HTTP_400_BAD_REQUEST ) <EOL> self . assertEqual ( response . data , expected ) </s>
