Skip to content
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

Modernize middleware #296

Merged
merged 3 commits into from
Aug 15, 2018
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
7 changes: 6 additions & 1 deletion project/tests/test_config_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
from silk.config import SilkyConfig
from silk.models import Request

def fake_get_response():
def fake_response():
return 'hello world'
return fake_response


class TestConfigMeta(TestCase):
def _mock_response(self):
Expand All @@ -23,7 +28,7 @@ def _execute_request(self):
delete_all_models(Request)
DataCollector().configure(Request.objects.create())
response = self._mock_response()
SilkyMiddleware()._process_response('', response)
SilkyMiddleware(fake_get_response)._process_response('', response)
self.assertTrue(response.status_code == 200)
objs = Request.objects.all()
self.assertEqual(objs.count(), 1)
Expand Down
19 changes: 12 additions & 7 deletions project/tests/test_silky_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@

from .util import mock_data_collector

def fake_get_response():
def fake_response():
return 'hello world'
return fake_response


class TestApplyDynamicMappings(TestCase):
def test_dynamic_decorator(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'module': 'tests.data.dynamic',
Expand All @@ -27,7 +32,7 @@ def test_dynamic_decorator(self):
self.assertTrue(mock_DataCollector.return_value.register_profile.call_count)

def test_dynamic_context_manager(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'module': 'tests.data.dynamic',
Expand All @@ -45,7 +50,7 @@ def test_dynamic_context_manager(self):
self.assertTrue(mock_DataCollector.return_value.register_profile.call_count)

def test_invalid_dynamic_context_manager(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'module': 'tests.data.dynamic',
Expand All @@ -57,7 +62,7 @@ def test_invalid_dynamic_context_manager(self):
self.assertRaises(IndexError, middleware._apply_dynamic_mappings)

def test_invalid_dynamic_decorator_module(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'module': 'tests.data.dfsdf',
Expand All @@ -67,7 +72,7 @@ def test_invalid_dynamic_decorator_module(self):
self.assertRaises(AttributeError, middleware._apply_dynamic_mappings)

def test_invalid_dynamic_decorator_function_name(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'module': 'tests.data.dynamic',
Expand All @@ -77,7 +82,7 @@ def test_invalid_dynamic_decorator_function_name(self):
self.assertRaises(AttributeError, middleware._apply_dynamic_mappings)

def test_invalid_dynamic_mapping(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [
{
'dfgdf': 'tests.data.dynamic',
Expand All @@ -87,7 +92,7 @@ def test_invalid_dynamic_mapping(self):
self.assertRaises(KeyError, middleware._apply_dynamic_mappings)

def test_no_mappings(self):
middleware = SilkyMiddleware()
middleware = SilkyMiddleware(fake_get_response)
SilkyConfig().SILKY_DYNAMIC_PROFILING = [

]
Expand Down
15 changes: 13 additions & 2 deletions silk/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from django.urls import reverse, NoReverseMatch
from django.db.models.sql.compiler import SQLCompiler
from django.utils import timezone
from django.utils.deprecation import MiddlewareMixin

from silk.collector import DataCollector

Expand Down Expand Up @@ -57,7 +56,19 @@ def process_request(self, request):
return


class SilkyMiddleware(MiddlewareMixin):
class SilkyMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
self.process_request(request)

response = self.get_response(request)

response = self.process_response(request, response)

return response

def _apply_dynamic_mappings(self):
dynamic_profile_configs = config.SILKY_DYNAMIC_PROFILING
for conf in dynamic_profile_configs:
Expand Down