Skip to content

Commit

Permalink
Merge pull request #12 from Polyconseil/qbey/add_dj110_compat
Browse files Browse the repository at this point in the history
middleware: add Django 1.10 compatibility
  • Loading branch information
fmr committed Dec 6, 2016
2 parents e6d7822 + b5b2454 commit b5a6ff1
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
10 changes: 8 additions & 2 deletions cid/middleware.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
from uuid import uuid4
from django.conf import settings

try:
from django.utils.deprecation import MiddlewareMixin
except ImportError: # Django < 1.10
MiddlewareMixin = object

from .locals import set_cid, get_cid


class CidMiddleware(object):
class CidMiddleware(MiddlewareMixin):
"""
Middleware class to extract the correlation id from incoming headers
and add them to outgoing headers
"""

def __init__(self):
def __init__(self, *args, **kwargs):
super(CidMiddleware, self).__init__(*args, **kwargs)
self.cid_request_header = getattr(
settings, 'CID_HEADER', 'X_CORRELATION_ID'
)
Expand Down
9 changes: 6 additions & 3 deletions sandbox/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = patterns(
'',
from .views import ping_view


urlpatterns = (
# Examples:
# url(r'^$', 'sandbox.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),

url(r'^ping/$', ping_view, name='ping'),
url(r'^admin/', include(admin.site.urls)),
)
5 changes: 5 additions & 0 deletions sandbox/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.http import HttpResponse


def ping_view(request, *args, **kwargs):
return HttpResponse('OK')
19 changes: 19 additions & 0 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.urlresolvers import reverse
from django.test import TestCase
from django.test.utils import override_settings
from mock import Mock, patch
Expand Down Expand Up @@ -87,3 +88,21 @@ def test_process_response_with_no_cid(self, get_cid):
middleware = CidMiddleware()
response = middleware.process_response(request, response)
self.assertNotIn('X_CORRELATION_ID', response.keys())

@override_settings(
MIDDLEWARE_CLASSES=('cid.middleware.CidMiddleware', ),
CID_GENERATE=True,
)
def test_integration(self):
"""Assert the middleware works with the Django initialization"""
# First call generates an new Correlation ID
response = self.client.get(reverse('ping')) # comes from the sandbox
cid = response.get('X_CORRELATION_ID')
self.assertIsNotNone(cid)

# Later calls with the header will keep it
response = self.client.get(
reverse('ping'), # comes from the sandbox
**{'X_CORRELATION_ID': cid}
)
self.assertEqual(response['X_CORRELATION_ID'], cid)

0 comments on commit b5a6ff1

Please sign in to comment.