mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
parent
3570ff734e
commit
bacb097ac3
@ -16,7 +16,9 @@ from django.conf import settings
|
||||
from django.contrib.auth import authenticate, login
|
||||
from django.core.handlers.base import BaseHandler
|
||||
from django.core.handlers.wsgi import WSGIRequest
|
||||
from django.core.signals import got_request_exception
|
||||
from django.core.signals import (request_started, request_finished,
|
||||
got_request_exception)
|
||||
from django.db import close_connection
|
||||
from django.http import SimpleCookie, HttpRequest, QueryDict
|
||||
from django.template import TemplateDoesNotExist
|
||||
from django.test import signals
|
||||
@ -76,7 +78,9 @@ def closing_iterator_wrapper(iterable, close):
|
||||
for item in iterable:
|
||||
yield item
|
||||
finally:
|
||||
close()
|
||||
request_finished.disconnect(close_connection)
|
||||
close() # will fire request_finished
|
||||
request_finished.connect(close_connection)
|
||||
|
||||
|
||||
class ClientHandler(BaseHandler):
|
||||
@ -91,14 +95,13 @@ class ClientHandler(BaseHandler):
|
||||
|
||||
def __call__(self, environ):
|
||||
from django.conf import settings
|
||||
from django.core import signals
|
||||
|
||||
# Set up middleware if needed. We couldn't do this earlier, because
|
||||
# settings weren't available.
|
||||
if self._request_middleware is None:
|
||||
self.load_middleware()
|
||||
|
||||
signals.request_started.send(sender=self.__class__)
|
||||
request_started.send(sender=self.__class__)
|
||||
request = WSGIRequest(environ)
|
||||
# sneaky little hack so that we can easily get round
|
||||
# CsrfViewMiddleware. This makes life easier, and is probably
|
||||
@ -112,7 +115,9 @@ class ClientHandler(BaseHandler):
|
||||
response.streaming_content = closing_iterator_wrapper(
|
||||
response.streaming_content, response.close)
|
||||
else:
|
||||
response.close()
|
||||
request_finished.disconnect(close_connection)
|
||||
response.close() # will fire request_finished
|
||||
request_finished.connect(close_connection)
|
||||
|
||||
return response
|
||||
|
||||
|
@ -641,8 +641,6 @@ class TransactionTestCase(SimpleTestCase):
|
||||
else:
|
||||
content = response.content
|
||||
content = content.decode(response._charset)
|
||||
# Avoid ResourceWarning about unclosed files.
|
||||
response.close()
|
||||
if html:
|
||||
content = assert_and_parse_html(self, content, None,
|
||||
"Response's content is not valid HTML:")
|
||||
|
@ -4,8 +4,6 @@ from xml.dom.minidom import parseString, Node
|
||||
|
||||
from django.conf import settings, UserSettingsHolder
|
||||
from django.core import mail
|
||||
from django.core.signals import request_finished
|
||||
from django.db import close_connection
|
||||
from django.test.signals import template_rendered, setting_changed
|
||||
from django.template import Template, loader, TemplateDoesNotExist
|
||||
from django.template.loaders import cached
|
||||
@ -70,10 +68,8 @@ def setup_test_environment():
|
||||
"""Perform any global pre-test setup. This involves:
|
||||
|
||||
- Installing the instrumented test renderer
|
||||
- Setting the email backend to the locmem email backend.
|
||||
- Set the email backend to the locmem email backend.
|
||||
- Setting the active locale to match the LANGUAGE_CODE setting.
|
||||
- Disconnecting the request_finished signal to avoid closing
|
||||
the database connection within tests.
|
||||
"""
|
||||
Template.original_render = Template._render
|
||||
Template._render = instrumented_test_render
|
||||
@ -85,8 +81,6 @@ def setup_test_environment():
|
||||
|
||||
deactivate()
|
||||
|
||||
request_finished.disconnect(close_connection)
|
||||
|
||||
|
||||
def teardown_test_environment():
|
||||
"""Perform any global post-test teardown. This involves:
|
||||
|
@ -56,6 +56,5 @@ class SignalsTests(TestCase):
|
||||
def test_request_signals_streaming_response(self):
|
||||
response = self.client.get('/streaming/')
|
||||
self.assertEqual(self.signals, ['started'])
|
||||
# Avoid self.assertContains, because it explicitly calls response.close()
|
||||
self.assertEqual(b''.join(response.streaming_content), b"streaming content")
|
||||
self.assertEqual(self.signals, ['started', 'finished'])
|
||||
|
Loading…
Reference in New Issue
Block a user