• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / Products.StandardCacheManagers / 16399753970

17 Mar 2025 07:54AM UTC coverage: 50.611% (-2.0%) from 52.597%
16399753970

push

github

web-flow
Update Python version support. (#13)

* Drop support for Python 3.8.
* Add support for Python 3.13.

13 of 124 branches covered (10.48%)

Branch coverage included in aggregate %.

360 of 613 relevant lines covered (58.73%)

0.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Products/StandardCacheManagers/tests/test_AcceleratedHTTPCacheManager.py
1
##############################################################################
2
#
3
# Copyright (c) 2005 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14
""" Unit tests for AcceleratedCacheManager module.
15
"""
16

17
import unittest
1✔
18

19
from Products.StandardCacheManagers.AcceleratedHTTPCacheManager import \
1✔
20
    AcceleratedHTTPCache
21
from Products.StandardCacheManagers.AcceleratedHTTPCacheManager import \
1✔
22
    AcceleratedHTTPCacheManager
23

24

25
class DummyObject:
1✔
26

27
    def __init__(self, path='/path/to/object', urlpath=None):
1✔
28
        self.path = path
1✔
29
        if urlpath is None:
1✔
30
            self.urlpath = path
1✔
31
        else:
32
            self.urlpath = urlpath
1✔
33

34
    def getPhysicalPath(self):
1✔
35
        return tuple(self.path.split('/'))
1✔
36

37
    def absolute_url_path(self):
1✔
38
        return self.urlpath
1✔
39

40

41
class MockResponse:
1✔
42
    status = '200'
1✔
43
    reason = "who knows, I'm just a mock"
1✔
44

45

46
def MockConnectionClassFactory():
1✔
47
    # Returns both a class that mocks an HTTPConnection,
48
    # and a reference to a data structure where it logs requests.
49
    request_log = []
1✔
50

51
    class MockConnection:
1✔
52
        # Minimal replacement for httplib.HTTPConnection.
53
        def __init__(self, host):
1✔
54
            self.host = host
1✔
55
            self.request_log = request_log
1✔
56

57
        def request(self, method, path):
1✔
58
            self.request_log.append({'method': method,
1✔
59
                                     'host': self.host,
60
                                     'path': path})
61

62
        def getresponse(self):
1✔
63
            return MockResponse()
1✔
64

65
    return MockConnection, request_log
1✔
66

67

68
class AcceleratedHTTPCacheTests(unittest.TestCase):
1✔
69

70
    def _getTargetClass(self):
1✔
71
        return AcceleratedHTTPCache
1✔
72

73
    def _makeOne(self, *args, **kw):
1✔
74
        return self._getTargetClass()(*args, **kw)
1✔
75

76
    def test_PURGE_passes_Host_header(self):
1✔
77
        _TO_NOTIFY = 'localhost:1888'
1✔
78
        cache = self._makeOne()
1✔
79
        cache.notify_urls = ['http://%s' % _TO_NOTIFY]
1✔
80
        cache.connection_factory, requests = MockConnectionClassFactory()
1✔
81
        dummy = DummyObject()
1✔
82
        cache.ZCache_invalidate(dummy)
1✔
83
        self.assertEqual(len(requests), 1)
1✔
84
        result = requests[-1]
1✔
85
        self.assertEqual(result['method'], 'PURGE')
1✔
86
        self.assertEqual(result['host'], _TO_NOTIFY)
1✔
87
        self.assertEqual(result['path'], dummy.path)
1✔
88

89
    def test_multiple_notify(self):
1✔
90
        cache = self._makeOne()
1✔
91
        cache.notify_urls = ['http://foo', 'bar', 'http://baz/bat']
1✔
92
        cache.connection_factory, requests = MockConnectionClassFactory()
1✔
93
        cache.ZCache_invalidate(DummyObject())
1✔
94
        self.assertEqual(len(requests), 3)
1✔
95
        self.assertEqual(requests[0]['host'], 'foo')
1✔
96
        self.assertEqual(requests[1]['host'], 'bar')
1✔
97
        self.assertEqual(requests[2]['host'], 'baz')
1✔
98
        cache.ZCache_invalidate(DummyObject())
1✔
99
        self.assertEqual(len(requests), 6)
1✔
100

101
    def test_vhost_purging_1447(self):
1✔
102
        # Test for http://www.zope.org/Collectors/Zope/1447
103
        cache = self._makeOne()
1✔
104
        cache.notify_urls = ['http://foo.com']
1✔
105
        cache.connection_factory, requests = MockConnectionClassFactory()
1✔
106
        dummy = DummyObject(urlpath='/published/elsewhere')
1✔
107
        cache.ZCache_invalidate(dummy)
1✔
108
        # That should fire off two invalidations,
109
        # one for the physical path and one for the abs. url path.
110
        self.assertEqual(len(requests), 2)
1✔
111
        self.assertEqual(requests[0]['path'], dummy.absolute_url_path())
1✔
112
        self.assertEqual(requests[1]['path'], dummy.path)
1✔
113

114

115
class CacheManagerTests(unittest.TestCase):
1✔
116

117
    def _getTargetClass(self):
1✔
118
        return AcceleratedHTTPCacheManager
1✔
119

120
    def _makeOne(self, *args, **kw):
1✔
121
        return self._getTargetClass()(*args, **kw)
1✔
122

123
    def _makeContext(self):
1✔
124
        from OFS.Folder import Folder
1✔
125
        root = Folder()
1✔
126
        root.getPhysicalPath = lambda: ('', 'some_path')
1✔
127
        cm_id = 'http_cache'
1✔
128
        manager = self._makeOne(cm_id)
1✔
129
        root._setObject(cm_id, manager)
1✔
130
        manager = root[cm_id]
1✔
131
        return root, manager
1✔
132

133
    def test_add(self):
1✔
134
        # ensure __init__ doesn't raise errors.
135
        root, cachemanager = self._makeContext()
1✔
136

137
    def test_ZCacheManager_getCache(self):
1✔
138
        root, cachemanager = self._makeContext()
1✔
139
        cache = cachemanager.ZCacheManager_getCache()
1✔
140
        self.assertIsInstance(cache, AcceleratedHTTPCache)
1✔
141

142
    def test_getSettings(self):
1✔
143
        root, cachemanager = self._makeContext()
1✔
144
        settings = cachemanager.getSettings()
1✔
145
        self.assertIn('anonymous_only', settings)
1✔
146
        self.assertIn('interval', settings)
1✔
147
        self.assertIn('notify_urls', settings)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc