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

Set urlconf to None after changing SUBDOMAIN setting #4032

Merged
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: 7 additions & 0 deletions readthedocs/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.sessions.middleware import SessionMiddleware
from django.core.cache import cache
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
from django.core.urlresolvers import set_urlconf, get_urlconf
from django.http import Http404, HttpResponseBadRequest

from readthedocs.core.utils import cname_to_slug
Expand Down Expand Up @@ -130,6 +131,12 @@ def process_request(self, request):
# Normal request.
return None

def process_response(self, request, response):
# Reset URLconf for this thread
# to the original one.
set_urlconf(None)
return response


class SingleVersionMiddleware(object):

Expand Down
27 changes: 26 additions & 1 deletion readthedocs/rtd_tests/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from __future__ import absolute_import

from django.http import Http404
from django.core.cache import cache
from django.core.urlresolvers import get_urlconf, set_urlconf
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings

from django_dynamic_fixture import get, new
from django_dynamic_fixture import get

from corsheaders.middleware import CorsMiddleware
from mock import patch
Expand Down Expand Up @@ -40,6 +42,29 @@ def test_proper_subdomain(self):
self.assertEqual(request.subdomain, True)
self.assertEqual(request.slug, 'pip')

@override_settings(PRODUCTION_DOMAIN='readthedocs.org')
def test_restore_urlconf_after_request(self):
"""
The urlconf attribute for the current thread
should remain intact after each request,
When is set to None it means 'use default from settings'.
"""
set_urlconf(None)
urlconf = get_urlconf()
self.assertIsNone(urlconf)

self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
urlconf = get_urlconf()
self.assertIsNone(urlconf)

self.client.get(self.url)
urlconf = get_urlconf()
self.assertIsNone(urlconf)

self.client.get(self.url, HTTP_HOST='pip.readthedocs.org')
urlconf = get_urlconf()
self.assertIsNone(urlconf)

@override_settings(PRODUCTION_DOMAIN='prod.readthedocs.org')
def test_subdomain_different_length(self):
request = self.factory.get(self.url, HTTP_HOST='pip.prod.readthedocs.org')
Expand Down
1 change: 0 additions & 1 deletion readthedocs/rtd_tests/tests/test_subprojects.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def setUp(self):
relation.save()
fixture.get(Project, slug='sub_alias', language='ya')


@override_settings(
PRODUCTION_DOMAIN='readthedocs.org',
USE_SUBDOMAIN=False,
Expand Down