Added error catching
This commit is contained in:
parent
44bfa68abf
commit
2620ccac96
2 changed files with 12 additions and 5 deletions
|
@ -6,6 +6,10 @@ class ReverserTestCase(APITestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super().setUp()
|
super().setUp()
|
||||||
|
|
||||||
def test_thing(self):
|
def test_reverser(self):
|
||||||
response = self.client.post(reverse('reverser'), data={'ident': 'tests:root'})
|
response = self.client.post(reverse('reverser'), data={'ident': 'tests:root'})
|
||||||
self.assertEqual(response.data, '/tests/')
|
self.assertEqual(response.data, reverse('tests:root'))
|
||||||
|
|
||||||
|
def test_invalid(self):
|
||||||
|
response = self.client.post(reverse('reverser'), data={'ident': 'tests:not_a_thing'})
|
||||||
|
self.assertEqual(response.status_code, 404)
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
from rest_framework.views import APIView
|
from rest_framework.views import APIView
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework.renderers import JSONRenderer
|
from rest_framework.renderers import JSONRenderer
|
||||||
from django.core.urlresolvers import reverse
|
from django.core.urlresolvers import reverse, NoReverseMatch
|
||||||
from .serializers import ReverserInputSerializer
|
from .serializers import ReverserInputSerializer
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,5 +11,8 @@ class Reverser(APIView):
|
||||||
def post(self, request, format=None):
|
def post(self, request, format=None):
|
||||||
input_serializer = ReverserInputSerializer(data=request.data)
|
input_serializer = ReverserInputSerializer(data=request.data)
|
||||||
input_serializer.is_valid(raise_exception=True)
|
input_serializer.is_valid(raise_exception=True)
|
||||||
url = reverse(input_serializer.validated_data['ident'])
|
try:
|
||||||
return Response(url)
|
url = reverse(input_serializer.validated_data['ident'])
|
||||||
|
except NoReverseMatch:
|
||||||
|
return Response(status=404)
|
||||||
|
return Response(url, 302)
|
||||||
|
|
Reference in a new issue