Permalink
Browse files

basic test cases for subdomain middleware

  • Loading branch information...
1 parent 244e1ff commit 3fec51ec8324acf7a66dd042337e4d901a83f01d @tkaemming committed Apr 26, 2012
Showing with 80 additions and 8 deletions.
  1. +1 −1 Makefile
  2. +5 −3 subdomains/middleware.py
  3. +1 −0 tests/example/settings.py
  4. +61 −2 tests/example/tests.py
  5. +7 −2 tests/example/urls.py
  6. +5 −0 tests/example/views.py
View
@@ -9,6 +9,6 @@ check:
pep8 --repeat --show-source ./
test:
- python tests/manage.py test --verbosity=2
+ PYTHONPATH=".:${PYTHONPATH}" python tests/manage.py test --verbosity=2
.PHONY: check install install-dev test
View
@@ -6,8 +6,6 @@
from django.contrib.sites.models import Site
from subdomains.exceptions import IncorrectSiteException
-from subdomains.settings import (USE_SUBDOMAIN_EXCEPTION,
- REMOVE_WWW_FROM_DOMAIN)
class SubdomainMiddleware(object):
@@ -19,8 +17,12 @@ def process_request(self, request):
"""
site = Site.objects.get_current()
domain = site.domain
+
+ REMOVE_WWW_FROM_DOMAIN = getattr(settings, 'REMOVE_WWW_FROM_DOMAIN',
+ False)
if REMOVE_WWW_FROM_DOMAIN and domain.startswith("www."):
domain = domain.replace("www.", "", 1)
+
pattern = r'^(?:(?P<subdomain>.*?)\.)?%s(?::.*)?$' % re.escape(domain)
matches = re.match(pattern, request.get_host())
@@ -32,7 +34,7 @@ def process_request(self, request):
error = 'The current host, %s, does not match the Site instance ' \
'specified by SITE_ID.' % request.get_host()
- if USE_SUBDOMAIN_EXCEPTION:
+ if getattr(settings, 'USE_SUBDOMAIN_EXCEPTION', False):
raise IncorrectSiteException(error)
else:
warnings.warn('%s The URLconf for this host will fall back to '
@@ -15,6 +15,7 @@
)
ROOT_URLCONF = 'example.urls'
+SUBDOMAIN_URLCONFS = {}
# Python dotted path to the WSGI application used by Django's runserver.
WSGI_APPLICATION = 'example.wsgi.application'
View
@@ -1,5 +1,64 @@
+import warnings
+
+from django.contrib.sites.models import Site
from django.test import TestCase
+from django.test.client import RequestFactory
+
+from subdomains.middleware import SubdomainMiddleware
+
+
+class SubdomainMiddlewareTestCase(TestCase):
+ def setUp(self):
+ self.site = Site.objects.get_current()
+ self.middleware = SubdomainMiddleware()
+
+ def test_subdomain_attribute(self):
+ def subdomain(subdomain):
+ """
+ Returns the request.subdomain for the provided subdomain.
+ """
+ if subdomain is not None:
+ host = '%s.%s' % (subdomain, self.site.domain)
+ else:
+ host = '%s' % self.site.domain
+ request = RequestFactory().get('/', HTTP_HOST=host)
+ self.middleware.process_request(request)
+ return request.subdomain
+
+ self.assertEqual(subdomain(None), None)
+ self.assertEqual(subdomain('www'), 'www')
+ self.assertEqual(subdomain('www.subdomain'), 'www.subdomain')
+ self.assertEqual(subdomain('subdomain'), 'subdomain')
+ self.assertEqual(subdomain('another.subdomain'), 'another.subdomain')
+
+ def test_www_domain(self):
+ def host(host):
+ """
+ Returns the subdomain for the provided HTTP Host.
+ """
+ request = RequestFactory().get('/', HTTP_HOST=host)
+ self.middleware.process_request(request)
+ return request.subdomain
+
+ self.site.domain = 'www.example.com'
+ self.site.save()
+
+ with self.settings(REMOVE_WWW_FROM_DOMAIN=False):
+ self.assertEqual(host('www.example.com'), None)
+
+ # Squelch the subdomain warning for cleaner test output, since we
+ # already know that this is an invalid subdomain.
+ with warnings.catch_warnings(record=True) as warnlist:
+ self.assertEqual(host('www.subdomain.example.com'), None)
+
+ # Trick pyflakes into not warning us about variable usage.
+ warnlist
+ self.assertEqual(host('subdomain.www.example.com'), 'subdomain')
+ self.assertEqual(host('www.subdomain.www.example.com'),
+ 'www.subdomain')
-class SubdomainTestCase(TestCase):
- pass
+ with self.settings(REMOVE_WWW_FROM_DOMAIN=True):
+ self.assertEqual(host('www.example.com'), 'www')
+ self.assertEqual(host('subdomain.www.example.com'),
+ 'subdomain.www')
View
@@ -1,3 +1,8 @@
-from django.conf.urls import patterns
+from django.conf.urls import patterns, url
-urlpatterns = patterns('',)
+from example.views import view
+
+
+urlpatterns = patterns('',
+ url(regex=r'^$', view=view),
+)
View
@@ -0,0 +1,5 @@
+from django.http import HttpResponse
+
+
+def view(request):
+ return HttpResponse()

0 comments on commit 3fec51e

Please sign in to comment.