Skip to content

Reformat Server-Timing headers to updated W3C spec. #1211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This caused the test to fail until the views returned a response containing </body>.


@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
Original file line number Diff line number Diff line change
@@ -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