Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Router doesn't work if prefix is blank, though project urls.py handle…
…s prefix
  • Loading branch information
c17r committed Sep 14, 2016
commit a556b9cb426f2893cb6807299a94044d585b7c2c
49 changes: 49 additions & 0 deletions tests/test_routers.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import unicode_literals

import json
from collections import namedtuple

from django.conf.urls import include, url
Expand Down Expand Up @@ -47,6 +48,21 @@ class MockViewSet(viewsets.ModelViewSet):
serializer_class = None


class EmptyPrefixSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = RouterTestModel
fields = ('uuid', 'text')


class EmptyPrefixViewSet(viewsets.ModelViewSet):
queryset = [RouterTestModel(id=1, uuid='111', text='First'), RouterTestModel(id=2, uuid='222', text='Second')]
serializer_class = EmptyPrefixSerializer

def get_object(self, *args, **kwargs):
index = int(self.kwargs['pk']) - 1
return self.queryset[index]


notes_router = SimpleRouter()
notes_router.register(r'notes', NoteViewSet)

Expand All @@ -56,11 +72,19 @@ class MockViewSet(viewsets.ModelViewSet):
namespaced_router = DefaultRouter()
namespaced_router.register(r'example', MockViewSet, base_name='example')

empty_prefix_router = SimpleRouter()
empty_prefix_router.register(r'', EmptyPrefixViewSet, base_name='empty_prefix')
empty_prefix_urls = [
url(r'^', include(empty_prefix_router.urls)),
]

urlpatterns = [
url(r'^non-namespaced/', include(namespaced_router.urls)),
url(r'^namespaced/', include(namespaced_router.urls, namespace='example')),
url(r'^example/', include(notes_router.urls)),
url(r'^example2/', include(kwarged_notes_router.urls)),

url(r'^empty-prefix/', include(empty_prefix_urls)),
]


Expand Down Expand Up @@ -384,3 +408,28 @@ def test_list_and_detail_route_decorators(self):

def test_inherited_list_and_detail_route_decorators(self):
self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet)


@override_settings(ROOT_URLCONF='tests.test_routers')
class TestEmptyPrefix(TestCase):
def test_empty_prefix_list(self):
response = self.client.get('/empty-prefix/')
self.assertEqual(200, response.status_code)
self.assertEqual(
json.loads(response.content.decode('utf-8')),
[
{'uuid': '111', 'text': 'First'},
{'uuid': '222', 'text': 'Second'}
]
)

def test_empty_prefix_detail(self):
response = self.client.get('/empty-prefix/1/')
self.assertEqual(200, response.status_code)
self.assertEqual(
json.loads(response.content.decode('utf-8')),
{
'uuid': '111',
'text': 'First'
}
)