Skip to content

Commit

Permalink
Merge pull request #4032 from stsewd/teardown-after-change-config-sub…
Browse files Browse the repository at this point in the history
…domain

Set urlconf to None after changing SUBDOMAIN setting
  • Loading branch information
humitos authored May 30, 2018
2 parents c85d8c9 + 05d2c27 commit dec550f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
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

0 comments on commit dec550f

Please sign in to comment.