Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Testing improvements

  • Loading branch information...
commit 5330efa60f0c747bacada56db55af583845a69f7 1 parent c94549b
@d0ugal d0ugal authored
View
21 tests/test_urlmiddleware/tests.py
@@ -375,3 +375,24 @@ def test_include_get(self):
response = c.get('/include_test/')
self.assertEqual(404, response.status_code)
+
+
+class UrlConfTestCase(TestCase):
+
+ def test_middleware_func(self):
+
+ from urlmiddleware.conf import middleware, ImproperlyConfigured
+
+ with self.assertRaises(ImproperlyConfigured):
+ middleware(r'', '', prefix="prefix")
+
+ middleware(r'', 'name', prefix="prefix")
+
+ def test_invalid_middleware_object(self):
+
+ from urlmiddleware.conf import ImproperlyConfigured
+
+ c = Client()
+
+ with self.assertRaises(ImproperlyConfigured):
+ c.get('/invalid/')
View
4 tests/test_urlmiddleware/urls.py
@@ -29,3 +29,7 @@
middlewarepatterns += mpatterns('test_urlmiddleware.middleware',
middleware(r'^dotted2/$', 'NoOpMiddleWare6'),
)
+
+middlewarepatterns += mpatterns('',
+ middleware(r'^invalid/$', 'NotARealThing'),
+)
View
22 urlmiddleware/middleware.py
@@ -1,4 +1,3 @@
-from inspect import isclass, isfunction
from django.core.exceptions import ImproperlyConfigured
from django.utils.datastructures import SortedDict
from django.utils.functional import memoize
@@ -31,19 +30,14 @@ def get_matched_middleware(self, path, middleware_method=None):
return []
for middleware_class in middleware_matches:
- invalid_error = "%s is expected to be a callable that accepts" \
- "no arguements." % middleware_class
- if not (isclass(middleware_class) or isfunction(middleware_class)\
- or callable(middleware_class)):
- raise ImproperlyConfigured(invalid_error)
- try:
- mw_instance = middleware_class()
- if middleware_method and not hasattr(mw_instance,
- middleware_method):
- continue
- middleware_instances.append(mw_instance)
- except TypeError:
- raise ImproperlyConfigured(invalid_error)
+
+ if not callable(middleware_class):
+ raise ImproperlyConfigured("%s is expected to be a callable that accepts no arguements." % middleware_class)
+
+ mw_instance = middleware_class()
+ if middleware_method and not hasattr(mw_instance, middleware_method):
+ continue
+ middleware_instances.append(mw_instance)
return middleware_instances
Please sign in to comment.
Something went wrong with that request. Please try again.