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

check custom is_ajax method #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions influxdb_metrics/middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Middlewares for the influxdb_metrics app."""
import inspect
import time

try:
from urllib import parse
except ImportError:
@@ -12,11 +13,13 @@
from tld.exceptions import TldBadUrl, TldDomainNotFound, TldIOError

from .loader import write_points
from .utils import is_ajax as check_if_is_ajax

try:
from django.utils.deprecation import MiddlewareMixin
from django.utils.deprecation import MiddlewareMixin
except ImportError:
MiddlewareMixin = object
MiddlewareMixin = object


class InfluxDBRequestMiddleware(MiddlewareMixin):
"""
@@ -25,6 +28,7 @@ class InfluxDBRequestMiddleware(MiddlewareMixin):
Credits go to: https://github.com/andymckay/django-statsd/blob/master/django_statsd/middleware.py#L24 # NOQA

"""

def process_view(self, request, view_func, view_args, view_kwargs):
view = view_func
if not inspect.isfunction(view_func):
@@ -46,7 +50,7 @@ def process_exception(self, request, exception):
def _record_time(self, request):
if hasattr(request, '_start_time'):
ms = int((time.time() - request._start_time) * 1000)
if request.is_ajax():
if check_if_is_ajax(request):
is_ajax = True
else:
is_ajax = False
12 changes: 12 additions & 0 deletions influxdb_metrics/tests/utils_tests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Tests for the utils module of the influxdb_metrics app."""
from django.http import HttpRequest
from django.test import TestCase

from mock import patch
@@ -61,3 +62,14 @@ def test_method(self):
result = utils.write_points([])
self.assertEqual(result, None, msg=(
'If setting is set, should return immediately'))


class TestIsAjax(TestCase):
def test_ajax_request(self):
request = HttpRequest()
request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
self.assertTrue(is_ajax(request))

def test_non_ajax_request(self):
request = HttpRequest()
self.assertFalse(is_ajax(request))
4 changes: 4 additions & 0 deletions influxdb_metrics/utils.py
Original file line number Diff line number Diff line change
@@ -64,3 +64,7 @@ def process_points(client, data): # pragma: no cover
logger.error(err)
else:
raise err


def is_ajax(request):
return request.META.get('HTTP_X_REQUESTED_WITH', '') == 'XMLHttpRequest'