I'm trying to write some unit tests on a Django
(I'm using Django==1.9.12
) application that uses django-tls
, but I always get this exception: RuntimeError: no object bound to request
. My code looks like this:
settings.py
:
As in django-tls doc I added the line 'tls.TLSRequestMiddleware'
to my MIDDLEWARE_CLASSES
:
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'tls.TLSRequestMiddleware',
]
I wrote a method to test it on a module called utils.py
:
from tls import request
def print_tls_found_by_django_tls():
"""
Just print the request instance found by django-tls.
:return:
"""
print(request)
On my test_utils.py
I have tried to follow the Werkzeug test utilities because Werkzeug
is what django-tls
uses to discover the request
instance. So I coded something like this for testing that:
from werkzeug.test import EnvironBuilder
from django.test import TestCase
from example_app.utils import print_tls_found_by_django_tls
class TestUtils(TestCase):
def setUp(self):
self.build_tls_request()
def test_print_tls_found_by_django_tls(self):
print_tls_found_by_django_tls()
def build_tls_request(self):
return EnvironBuilder(method='GET').get_request()
but then the exception is there:
(venv) vladir@vladir:~/work/project_example_all/example/example_app$ coverage run ./manage.py test example_app.tests.TestUtils.test_print_tls_found_by_django_tls
Creating test database for alias 'default'...
E
======================================================================
ERROR: test_print_tls_found_by_django_tls (affiliate_tracking.tests.test_utils.TestUtils)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vladir/work/project_example_all/example/venv/lib/python3.5/site-packages/werkzeug/local.py", line 70, in __getattr__
return self.__storage__[self.__ident_func__()][name]
KeyError: 140239354230528
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vladir/work/project_example_all/example/venv/lib/python3.5/site-packages/werkzeug/local.py", line 308, in _get_current_object
return getattr(self.__local, self.__name__)
File "/home/vladir/work/project_example_all/example/venv/lib/python3.5/site-packages/werkzeug/local.py", line 72, in __getattr__
raise AttributeError(name)
AttributeError: request
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/vladir/work/project_example_all/example/example_app/affiliate_tracking/tests/test_utils.py", line 15, in test_print_tls_found_by_django_tls
print_tls_found_by_django_tls()
File "/home/vladir/work/project_example_all/example/example_app/affiliate_tracking/utils.py", line 67, in print_tls_found_by_django_tls
print(request)
File "/home/vladir/work/project_example_all/example/venv/lib/python3.5/site-packages/werkzeug/local.py", line 366, in <lambda>
__str__ = lambda x: str(x._get_current_object())
File "/home/vladir/work/project_example_all/example/venv/lib/python3.5/site-packages/werkzeug/local.py", line 310, in _get_current_object
raise RuntimeError('no object bound to %s' % self.__name__)
RuntimeError: no object bound to request
----------------------------------------------------------------------
Ran 1 test in 0.002s
FAILED (errors=1)
Destroying test database for alias 'default'...
I would appreciate any help or clue you could give me to find a solution to this. I have tried several things but without any luck. Thanks in advance mates.