2011-01-14 23:18:21 +00:00
|
|
|
from django.core.handlers.wsgi import WSGIHandler
|
2013-02-18 10:37:26 +00:00
|
|
|
from django.core.signals import request_started, request_finished
|
2013-03-06 10:12:24 +00:00
|
|
|
from django.db import close_old_connections, connection
|
|
|
|
from django.test import RequestFactory, TestCase, TransactionTestCase
|
2012-10-20 21:22:46 +00:00
|
|
|
from django.test.utils import override_settings
|
2011-04-22 12:15:52 +00:00
|
|
|
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
class HandlerTests(TestCase):
|
2011-01-14 23:18:21 +00:00
|
|
|
|
2013-02-18 10:37:26 +00:00
|
|
|
def setUp(self):
|
|
|
|
request_started.disconnect(close_old_connections)
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
request_started.connect(close_old_connections)
|
|
|
|
|
2012-10-20 21:22:46 +00:00
|
|
|
# Mangle settings so the handler will fail
|
|
|
|
@override_settings(MIDDLEWARE_CLASSES=42)
|
2011-01-14 23:18:21 +00:00
|
|
|
def test_lock_safety(self):
|
|
|
|
"""
|
|
|
|
Tests for bug #11193 (errors inside middleware shouldn't leave
|
|
|
|
the initLock locked).
|
|
|
|
"""
|
|
|
|
# Try running the handler, it will fail in load_middleware
|
|
|
|
handler = WSGIHandler()
|
|
|
|
self.assertEqual(handler.initLock.locked(), False)
|
2012-09-07 19:06:23 +00:00
|
|
|
with self.assertRaises(Exception):
|
2011-01-14 23:18:21 +00:00
|
|
|
handler(None, None)
|
|
|
|
self.assertEqual(handler.initLock.locked(), False)
|
|
|
|
|
2011-04-22 12:15:52 +00:00
|
|
|
def test_bad_path_info(self):
|
|
|
|
"""Tests for bug #15672 ('request' referenced before assignment)"""
|
|
|
|
environ = RequestFactory().get('/').environ
|
2012-12-17 09:49:26 +00:00
|
|
|
environ['PATH_INFO'] = '\xed'
|
2011-04-22 12:15:52 +00:00
|
|
|
handler = WSGIHandler()
|
|
|
|
response = handler(environ, lambda *a, **k: None)
|
|
|
|
self.assertEqual(response.status_code, 400)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
|
2013-03-06 10:12:24 +00:00
|
|
|
class TransactionsPerRequestTests(TransactionTestCase):
|
2013-06-04 06:09:29 +00:00
|
|
|
|
|
|
|
available_apps = []
|
2013-03-06 10:12:24 +00:00
|
|
|
urls = 'handlers.urls'
|
|
|
|
|
|
|
|
def test_no_transaction(self):
|
|
|
|
response = self.client.get('/in_transaction/')
|
|
|
|
self.assertContains(response, 'False')
|
|
|
|
|
|
|
|
def test_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
|
|
|
try:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = True
|
|
|
|
response = self.client.get('/in_transaction/')
|
|
|
|
finally:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
|
|
|
self.assertContains(response, 'True')
|
|
|
|
|
|
|
|
def test_no_auto_transaction(self):
|
|
|
|
old_atomic_requests = connection.settings_dict['ATOMIC_REQUESTS']
|
|
|
|
try:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = True
|
|
|
|
response = self.client.get('/not_in_transaction/')
|
|
|
|
finally:
|
|
|
|
connection.settings_dict['ATOMIC_REQUESTS'] = old_atomic_requests
|
|
|
|
self.assertContains(response, 'False')
|
|
|
|
|
2013-05-15 23:14:28 +00:00
|
|
|
|
2012-12-30 14:19:22 +00:00
|
|
|
class SignalsTests(TestCase):
|
2013-02-26 12:19:18 +00:00
|
|
|
urls = 'handlers.urls'
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.signals = []
|
2013-02-18 10:37:26 +00:00
|
|
|
request_started.connect(self.register_started)
|
|
|
|
request_finished.connect(self.register_finished)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def tearDown(self):
|
2013-02-18 10:37:26 +00:00
|
|
|
request_started.disconnect(self.register_started)
|
|
|
|
request_finished.disconnect(self.register_finished)
|
2012-12-30 14:19:22 +00:00
|
|
|
|
|
|
|
def register_started(self, **kwargs):
|
|
|
|
self.signals.append('started')
|
|
|
|
|
|
|
|
def register_finished(self, **kwargs):
|
|
|
|
self.signals.append('finished')
|
|
|
|
|
|
|
|
def test_request_signals(self):
|
|
|
|
response = self.client.get('/regular/')
|
|
|
|
self.assertEqual(self.signals, ['started', 'finished'])
|
|
|
|
self.assertEqual(response.content, b"regular content")
|
|
|
|
|
|
|
|
def test_request_signals_streaming_response(self):
|
|
|
|
response = self.client.get('/streaming/')
|
|
|
|
self.assertEqual(self.signals, ['started'])
|
|
|
|
self.assertEqual(b''.join(response.streaming_content), b"streaming content")
|
|
|
|
self.assertEqual(self.signals, ['started', 'finished'])
|
2013-05-15 23:14:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HandlerSuspiciousOpsTest(TestCase):
|
|
|
|
urls = 'handlers.urls'
|
|
|
|
|
|
|
|
def test_suspiciousop_in_view_returns_400(self):
|
|
|
|
response = self.client.get('/suspicious/')
|
|
|
|
self.assertEqual(response.status_code, 400)
|