Skip to content

Commit

Permalink
Reformat Server-Timing headers to updated W3C spec. (#1211)
Browse files Browse the repository at this point in the history
Fixed basic integration tests. When there was no </body> tag
the toolbar's middleware exited earlier and was never inserted.
  • Loading branch information
tim-schilling authored and matthiask committed Dec 20, 2019
1 parent fd50ce3 commit 9ddf49d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 2 additions & 2 deletions debug_toolbar/middleware.py
Expand Up @@ -112,9 +112,9 @@ def generate_server_timing_header(response, panels):
continue

for key, record in stats.items():
# example: `SQLPanel_sql_time=0; "SQL 0 queries"`
# example: `SQLPanel_sql_time;dur=0;desc="SQL 0 queries"`
data.append(
'{}_{}={}; "{}"'.format(
'{}_{};dur={};desc="{}"'.format(
panel.panel_id, key, record.get("value"), record.get("title")
)
)
Expand Down
16 changes: 16 additions & 0 deletions tests/test_integration.py
@@ -1,4 +1,5 @@
import os
import re
import unittest

import html5lib
Expand Down Expand Up @@ -92,6 +93,7 @@ class DebugToolbarIntegrationTestCase(TestCase):
def test_middleware(self):
response = self.client.get("/execute_sql/")
self.assertEqual(response.status_code, 200)
self.assertContains(response, "djDebug")

@override_settings(DEFAULT_CHARSET="iso-8859-1")
def test_non_utf8_charset(self):
Expand Down Expand Up @@ -245,6 +247,20 @@ def test_incercept_redirects(self):
# Link to LOCATION header.
self.assertIn(b'href="/regular/redirect/"', response.content)

def test_server_timing_headers(self):
response = self.client.get("/execute_sql/")
server_timing = response["Server-Timing"]
expected_partials = [
r'TimerPanel_utime;dur=(\d)*(\.(\d)*)?;desc="User CPU time", ',
r'TimerPanel_stime;dur=(\d)*(\.(\d)*)?;desc="System CPU time", ',
r'TimerPanel_total;dur=(\d)*(\.(\d)*)?;desc="Total CPU time", ',
r'TimerPanel_total_time;dur=(\d)*(\.(\d)*)?;desc="Elapsed time", ',
r'SQLPanel_sql_time;dur=(\d)*(\.(\d)*)?;desc="SQL 1 queries", ',
r'CachePanel_total_time;dur=0;desc="Cache 0 Calls"',
]
for expected in expected_partials:
self.assertTrue(re.compile(expected).search(server_timing))


@unittest.skipIf(webdriver is None, "selenium isn't installed")
@unittest.skipUnless(
Expand Down
8 changes: 4 additions & 4 deletions tests/views.py
@@ -1,13 +1,13 @@
from django.contrib.auth.models import User
from django.http import HttpResponse, HttpResponseRedirect
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.template.response import TemplateResponse
from django.views.decorators.cache import cache_page


def execute_sql(request):
list(User.objects.all())
return HttpResponse()
return render(request, "base.html")


def regular_view(request, title):
Expand All @@ -25,12 +25,12 @@ def new_user(request, username="joe"):

def resolving_view(request, arg1, arg2):
# see test_url_resolving in tests.py
return HttpResponse()
return render(request, "base.html")


@cache_page(60)
def cached_view(request):
return HttpResponse()
return render(request, "base.html")


def regular_jinjia_view(request, title):
Expand Down

0 comments on commit 9ddf49d

Please sign in to comment.