2011-02-19 13:26:27 +03:00
|
|
|
from django.conf.urls.defaults import patterns, url
|
|
|
|
from django.test import TestCase
|
2011-04-25 07:50:28 +04:00
|
|
|
from django.utils import simplejson as json
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2012-02-22 00:47:55 +04:00
|
|
|
from djangorestframework.renderers import JSONRenderer
|
2012-02-23 20:02:16 +04:00
|
|
|
from djangorestframework.reverse import reverse, reverse_lazy
|
2011-05-24 13:27:24 +04:00
|
|
|
from djangorestframework.views import View
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
|
2012-02-23 20:02:16 +04:00
|
|
|
class ReverseView(View):
|
2012-02-22 00:47:55 +04:00
|
|
|
"""
|
|
|
|
Mock resource which simply returns a URL, so that we can ensure
|
|
|
|
that reversed URLs are fully qualified.
|
|
|
|
"""
|
|
|
|
renderers = (JSONRenderer, )
|
2011-02-19 13:26:27 +03:00
|
|
|
|
2011-04-11 20:13:11 +04:00
|
|
|
def get(self, request):
|
2012-02-23 20:02:16 +04:00
|
|
|
return reverse('reverse', request=request)
|
|
|
|
|
|
|
|
|
|
|
|
class LazyView(View):
|
|
|
|
"""
|
|
|
|
Mock resource which simply returns a URL, so that we can ensure
|
|
|
|
that reversed URLs are fully qualified.
|
|
|
|
"""
|
|
|
|
renderers = (JSONRenderer, )
|
|
|
|
|
|
|
|
def get(self, request):
|
|
|
|
return reverse_lazy('lazy', request=request)
|
|
|
|
|
2011-02-19 13:26:27 +03:00
|
|
|
|
|
|
|
urlpatterns = patterns('',
|
2012-02-23 20:02:16 +04:00
|
|
|
url(r'^reverse$', ReverseView.as_view(), name='reverse'),
|
|
|
|
url(r'^lazy$', LazyView.as_view(), name='lazy'),
|
2011-02-19 13:26:27 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class ReverseTests(TestCase):
|
2012-02-22 00:47:55 +04:00
|
|
|
"""
|
|
|
|
Tests for fully qualifed URLs when using `reverse`.
|
|
|
|
"""
|
2011-02-19 13:26:27 +03:00
|
|
|
urls = 'djangorestframework.tests.reverse'
|
|
|
|
|
|
|
|
def test_reversed_urls_are_fully_qualified(self):
|
2012-02-23 20:02:16 +04:00
|
|
|
response = self.client.get('/reverse')
|
|
|
|
self.assertEqual(json.loads(response.content),
|
|
|
|
'http://testserver/reverse')
|
|
|
|
|
|
|
|
#def test_reversed_lazy_urls_are_fully_qualified(self):
|
|
|
|
# response = self.client.get('/lazy')
|
|
|
|
# self.assertEqual(json.loads(response.content),
|
|
|
|
# 'http://testserver/lazy')
|