Skip to content

Commit

Permalink
Fixing AJAX request handling
Browse files Browse the repository at this point in the history
DOM tree of a toolbar for AJAX requests was not stored on
the server.
  • Loading branch information
zifot committed Feb 18, 2013
1 parent 404d932 commit 1247cc9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
9 changes: 5 additions & 4 deletions debug_toolbar/middleware.py
Expand Up @@ -126,7 +126,7 @@ def process_response(self, request, response):
__traceback_hide__ = True
ident = thread.get_ident()
toolbar = self.__class__.debug_toolbars.get(ident)
if not toolbar or request.is_ajax():
if not toolbar:
return response
if isinstance(response, HttpResponseRedirect):
if not toolbar.config['INTERCEPT_REDIRECTS']:
Expand All @@ -145,13 +145,14 @@ def process_response(self, request, response):
panel.process_response(request, response)
content = smart_unicode(response.content)
ddt_html = smart_unicode(toolbar.render_toolbar())
if self.tag in content:

if request.is_ajax():
self._handle_ajax_response(toolbar, ddt_html, request, response)
elif self.tag in content:
response.content = replace_insensitive(content, self.tag,
ddt_html + self.tag)
if response.get('Content-Length', None):
response['Content-Length'] = len(response.content)
if request.is_ajax():
self._handle_ajax_response(toolbar, ddt_html, request, response)

del self.__class__.debug_toolbars[ident]
return response
19 changes: 18 additions & 1 deletion tests/tests.py
@@ -1,4 +1,5 @@
import thread
import types

from django.conf import settings
from django.contrib.auth.models import User
Expand Down Expand Up @@ -411,4 +412,20 @@ def test_response_to_ajax_request_stays_unchanged(self):
middleware.process_request(request)
middleware.process_response(request, response)
self.assertEquals(response.content, '<body></body>')


def test_handling_ajax_request(self):
request = request = rf.get('/')
request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
response = self.response
with Settings(INTERNAL_IPS=['127.0.0.1'], DEBUG=True):
middleware = DebugToolbarMiddleware()

def handler_mock(self, toolbar, ddt_html, request, response):
handler_mock.called = True
handler_mock.called = False
middleware._handle_ajax_response = types.MethodType(handler_mock, middleware)

middleware.process_request(request)
middleware.process_response(request, response)
self.assertTrue(handler_mock.called)

0 comments on commit 1247cc9

Please sign in to comment.