Skip to content

Commit faf059a

Browse files
authored
Merge pull request #17 from matthewhegarty/django-3-2-update
updated to work with Django3.2
2 parents a8795f9 + 14acf26 commit faf059a

File tree

7 files changed

+41
-25
lines changed

7 files changed

+41
-25
lines changed

example/db.sqlite3

37 KB
Binary file not shown.

example/requirements.txt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
attrs==17.4.0
2-
Django==2.2.13
3-
pluggy==0.6.0
4-
py==1.5.2
5-
pytest==3.4.0
6-
pytz==2018.3
7-
six==1.11.0
1+
attrs==21.2.0
2+
Django==3.2.5
3+
pluggy==0.13.1
4+
py==1.10.0
5+
pytest==6.2.4
6+
pytz==2021.1
7+
six==1.16.0

requirements.dev.txt

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
Django<=2.0.11
2-
attrs==17.4.0
3-
pluggy==0.6.0
4-
py==1.5.2
5-
pytest==3.4.0
6-
pytest-django==3.1.2
7-
pytz==2018.3
8-
six==1.11.0
1+
Django==3.2.5
2+
attrs==21.2.0
3+
pluggy==0.13.1
4+
py==1.10.0
5+
pytest==6.2.4
6+
pytest-cov==2.12.1
7+
pytest-django==4.4.0
8+
pytz==2021.1
9+
six==1.16.0

server_timing/middleware.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import threading
33
from contextlib import contextmanager
44

5+
import django
56
from django.utils.deprecation import MiddlewareMixin
67

78

@@ -68,6 +69,9 @@ def process_response(self, request, response):
6869
for service in get_services()
6970
]
7071
if services:
71-
response._headers['server-timing'] = ('Server-Timing', ','.join(services))
72+
if django.VERSION >= (3, 2):
73+
response.headers['Server-Timing'] = ','.join(services)
74+
else:
75+
response._headers['Server-Timing'] = ','.join(services)
7276
discard_all_services()
7377
return response

tests/conftest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111
settings.configure(
12+
SECRET_KEY = "local_key",
1213
DEBUG=True,
1314
ROOT_URLCONF='tests.urls',
1415
DATABASES={

tests/test_integration.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
def test_header_complex_header(client):
2-
response = client.get('/complex')
1+
from unittest import skipUnless
2+
from unittest.mock import patch, PropertyMock
3+
4+
import django
5+
6+
7+
@skipUnless(django.VERSION[0] == 3 and django.VERSION[1] >= 2, "response.headers not included until Django 3.2")
8+
def test_header_complex_header_django_32(client):
9+
with patch('server_timing.middleware.TimedService.duration', new_callable=PropertyMock) as mock_duration:
10+
mock_duration.return_value = 10
11+
response = client.get('/complex')
312

413
assert response.has_header('Server-Timing')
5-
assert response._headers['server-timing'] == (
6-
'Server-Timing',
7-
'index;desc="Index View";dur=8,first;desc="First '
8-
'service";dur=3,second;desc="Second service";dur=5'
14+
res = response.headers['server-timing']
15+
target = (
16+
"index;desc=\"Index View\";dur=10,first;"
17+
"desc=\"First service\";dur=10,second;desc=\"Second service\";dur=10"
918
)
19+
assert res == target
1020

1121

1222
def test_header_no_header(client):

tests/urls.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
22

3-
from django.conf.urls import url
43
from django.http import HttpResponse
4+
from django.urls import path
55

66
from server_timing.middleware import TimedService, timed, timed_wrapper
77

@@ -26,6 +26,6 @@ def no_header_view(request):
2626

2727

2828
urlpatterns = [
29-
url('complex', complex_view),
30-
url('no-header', no_header_view),
29+
path('complex', complex_view),
30+
path('no-header', no_header_view),
3131
]

0 commit comments

Comments
 (0)