def setUp(self):
        #monkey-patch settings for test purposes
        setattr(settings, 'CACHE_NGINX_USE_LOOKUP_TABLE', True)

        self.factory = RequestFactory()
        # Clear the cache before we do anything.
        self.request = self.factory.get('/', SERVER_NAME="example1.com")
        cache.clear()
        self.cache_key = get_cache_key(self.request.get_host(),
                                       self.request.get_full_path())
        assert not cache.get(self.cache_key)
        self.assertEqual(CachedPageRecord.objects.count(), 0)
    def setUp(self):
        #monkey-patch settings for test purposes
        setattr(settings, 'CACHE_NGINX_USE_LOOKUP_TABLE', True)

        self.factory = RequestFactory()
        # Clear the cache before we do anything.
        self.request = self.factory.get('/', SERVER_NAME="example1.com")
        cache.clear()
        self.cache_key = get_cache_key(
            self.request.get_host(),
            self.request.get_full_path()
        )
        assert not cache.get(self.cache_key)
        self.assertEqual(CachedPageRecord.objects.count(), 0)
    def test_default_args(self):
        def my_view(request):
            return HttpResponse('content')

        # Clear the cache before we do anything.
        request = self.factory.get('/')
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)

        # Cache the view
        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')

        assert cache.get(cache_key)
Beispiel #4
0
    def test_lookup_record_respects_varying_hostname_by_default(self):
        # this one needs a bit more set-up
        self.request = self.factory.get('/', SERVER_NAME="example2.com")
        cache.clear()
        self.cache_key = get_cache_key(self.request.get_host(),
                                       self.request.get_full_path())
        assert not cache.get(self.cache_key)
        self.assertEqual(CachedPageRecord.objects.count(), 0)

        # and on with the test
        my_view_cached = cache_page_nginx(self.my_view)
        self.assertEqual(my_view_cached(self.request).content, 'content')
        assert cache.get(self.cache_key)

        self.assertEqual(CachedPageRecord.objects.get().parent_identifier,
                         "example2.com")
Beispiel #5
0
    def test_lookup_record_not_created_if_disabled_in_settings(self):

        # Monkey-patch settings for test purposes
        setattr(settings, 'CACHE_NGINX_USE_LOOKUP_TABLE', False)

        factory = RequestFactory()
        # Clear the cache before we do anything.
        request = factory.get('/', SERVER_NAME="example1.com")
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)

        self.assertEqual(CachedPageRecord.objects.count(), 0)

        my_view_cached = cache_page_nginx(self.my_view)
        self.assertEqual(my_view_cached(request).content, 'content')

        assert cache.get(cache_key)
        self.assertEqual(CachedPageRecord.objects.count(), 0)
    def test_custom_page_version_function(self):
        """
        Asserts that if there is a custom version function, the returned
        version is considered in the cache key.
        """
        def my_view(request):
            return HttpResponse('content')

        # monkey-patch settings to add a custom page version function
        settings.CACHE_NGINX_PAGE_VERSION_FUNCTION = lambda request: 'custom-version'

        request = self.factory.get('/')
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path(), 'custom-version')
        assert not cache.get(cache_key)

        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')

        assert cache.get(cache_key)

        # get back to the original value to not compromise other tests
        settings.CACHE_NGINX_PAGE_VERSION_FUNCTION = None
    def test_custom_encryptation_function(self):
        """
        Asserts that if there is a custom encryptation function, it will be
        used instead of the default md5.
        """
        def my_view(request):
            return HttpResponse('content')

        # monkey-patch settings to add a encryptation function that
        # in this example do nothing
        settings.CACHE_NGINX_ENCRYPTATION_FUNCTION = lambda raw_key: raw_key

        request = self.factory.get('/')
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)

        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')

        assert cache.get(cache_key)

        # get back to the original value to not compromise other tests
        settings.CACHE_NGINX_ENCRYPTATION_FUNCTION = None
    def test_ssl_requests_can_be_cached(self):

        def my_view(request):
            return HttpResponse('content')

        # Show that is_secure() requests are cached by default
        kwargs = {}
        kwargs["wsgi.url_scheme"] = "https"

        # Clear the cache before we do anything.
        request = self.factory.get('/', **kwargs)
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)
        # Cache the view
        logging.info("settings.CACHE_NGINX_INCLUDE_HTTPS should be True, Got: %s" % settings.CACHE_NGINX_INCLUDE_HTTPS)
        logging.info("Trying to cache a view - should be cached")
        logging.info(getattr(settings, 'CACHE_NGINX_INCLUDE_HTTPS', True))
        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')
        assert cache.get(cache_key)

        # Show that HTTPS header requests are cached by default
        kwargs = {}
        kwargs["HTTP_X_FORWARDED_PROTO"] = "HTTPS"
        # Clear the cache before we do anything.
        request = self.factory.get('/', **kwargs)
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)
        # Cache the view
        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')
        assert cache.get(cache_key)

        kwargs = {}
        kwargs["HTTP_X_FORWARDED_SSL"] = "on"

        # Clear the cache before we do anything.
        request = self.factory.get('/', **kwargs)
        cache.clear()
        cache_key = get_cache_key(request.get_host(), request.get_full_path())
        assert not cache.get(cache_key)
        # Cache the view
        my_view_cached = cache_page_nginx(my_view)
        self.assertEqual(my_view_cached(request).content, 'content')
        assert cache.get(cache_key)