From d809ac74d19342bcc84e4fe043697709b2001cc0 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Tue, 30 Jun 2009 21:02:00 +0000 Subject: - Add a ``reload_resources`` configuration file setting (aka the ``BFG_RELOAD_RESOURCES`` environment variable). When this is set to true, the server never needs to be restarted when moving files between directory resource overrides (esp. for templates currently). - Add a ``reload_all`` configuration file setting (aka the ``BFG_RELOAD_ALL`` environment variable) that implies both ``reload_resources`` and ``reload_templates``. - The ``static`` helper view class now uses a ``PackageURLParser`` in order to allow for the overriding of static resources (CSS / logo files, etc) using the ``resource`` ZCML directive. The ``PackageURLParser`` class was added to a (new) ``static`` module in BFG; it is a subclass of the ``StaticURLParser`` class in ``paste.urlparser``. - The ``repoze.bfg.templating.renderer_from_cache`` function now checks for the ``reload_resources`` setting; if it's true, it does not register a template renderer (it won't use the registry as a template renderer cache). - Add ``pkg_resources`` to the glossary. - Update the "Environment" docs to note the existence of ``reload_resources`` and ``reload_all``. - Use a colon instead of a tab as the separator between package name and relpath to form the "spec" when register a ITemplateRenderer. --- repoze/bfg/tests/fixtures/static/index.html | 1 + repoze/bfg/tests/fixtures/static/subdir/index.html | 1 + repoze/bfg/tests/test_settings.py | 36 +++++ repoze/bfg/tests/test_static.py | 157 +++++++++++++++++++++ repoze/bfg/tests/test_templating.py | 31 +++- repoze/bfg/tests/test_view.py | 26 +++- 6 files changed, 246 insertions(+), 6 deletions(-) create mode 100644 repoze/bfg/tests/fixtures/static/index.html create mode 100644 repoze/bfg/tests/fixtures/static/subdir/index.html create mode 100644 repoze/bfg/tests/test_static.py (limited to 'repoze/bfg/tests') diff --git a/repoze/bfg/tests/fixtures/static/index.html b/repoze/bfg/tests/fixtures/static/index.html new file mode 100644 index 000000000..6498787a5 --- /dev/null +++ b/repoze/bfg/tests/fixtures/static/index.html @@ -0,0 +1 @@ +static diff --git a/repoze/bfg/tests/fixtures/static/subdir/index.html b/repoze/bfg/tests/fixtures/static/subdir/index.html new file mode 100644 index 000000000..bb84fad04 --- /dev/null +++ b/repoze/bfg/tests/fixtures/static/subdir/index.html @@ -0,0 +1 @@ +subdir diff --git a/repoze/bfg/tests/test_settings.py b/repoze/bfg/tests/test_settings.py index 903a23f5d..8319a302d 100644 --- a/repoze/bfg/tests/test_settings.py +++ b/repoze/bfg/tests/test_settings.py @@ -61,6 +61,42 @@ class TestGetOptions(unittest.TestCase): {'BFG_RELOAD_TEMPLATES':'1'}) self.assertEqual(result['reload_templates'], True) + def test_reload_resources(self): + result = self._callFUT({}) + self.assertEqual(result['reload_resources'], False) + result = self._callFUT({'reload_resources':'false'}) + self.assertEqual(result['reload_resources'], False) + result = self._callFUT({'reload_resources':'t'}) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({'reload_resources':'1'}) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({}, {'BFG_RELOAD_RESOURCES':'1'}) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({'reload_resources':'false'}, + {'BFG_RELOAD_RESOURCES':'1'}) + self.assertEqual(result['reload_resources'], True) + + def test_reload_all(self): + result = self._callFUT({}) + self.assertEqual(result['reload_templates'], False) + self.assertEqual(result['reload_resources'], False) + result = self._callFUT({'reload_all':'false'}) + self.assertEqual(result['reload_templates'], False) + self.assertEqual(result['reload_resources'], False) + result = self._callFUT({'reload_all':'t'}) + self.assertEqual(result['reload_templates'], True) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({'reload_all':'1'}) + self.assertEqual(result['reload_templates'], True) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({}, {'BFG_RELOAD_ALL':'1'}) + self.assertEqual(result['reload_templates'], True) + self.assertEqual(result['reload_resources'], True) + result = self._callFUT({'reload_all':'false'}, + {'BFG_RELOAD_ALL':'1'}) + self.assertEqual(result['reload_templates'], True) + self.assertEqual(result['reload_resources'], True) + def test_debug_authorization(self): result = self._callFUT({}) self.assertEqual(result['debug_authorization'], False) diff --git a/repoze/bfg/tests/test_static.py b/repoze/bfg/tests/test_static.py new file mode 100644 index 000000000..7c7b5627c --- /dev/null +++ b/repoze/bfg/tests/test_static.py @@ -0,0 +1,157 @@ +import unittest + +class TestPackageURLParser(unittest.TestCase): + def _getTargetClass(self): + from repoze.bfg.static import PackageURLParser + return PackageURLParser + + def _makeOne(self, *arg, **kw): + return self._getTargetClass()(*arg, **kw) + + + def _makeEnviron(self, **kw): + environ = { + 'wsgi.url_scheme':'http', + 'wsgi.version':(1,0), + 'SERVER_NAME':'example.com', + 'SERVER_PORT':'6543', + 'PATH_INFO':'/', + 'SCRIPT_NAME':'', + 'REQUEST_METHOD':'GET', + } + environ.update(kw) + return environ + + def test_ctor_allargs(self): + inst = self._makeOne('package', 'resource/name', root_resource='root', + cache_max_age=100) + self.assertEqual(inst.package_name, 'package') + self.assertEqual(inst.resource_name, 'resource/name') + self.assertEqual(inst.root_resource, 'root') + self.assertEqual(inst.cache_max_age, 100) + + def test_ctor_defaultargs(self): + inst = self._makeOne('package', 'resource/name') + self.assertEqual(inst.package_name, 'package') + self.assertEqual(inst.resource_name, 'resource/name') + self.assertEqual(inst.root_resource, 'resource/name') + self.assertEqual(inst.cache_max_age, None) + + def test_call_adds_slash_path_info_empty(self): + environ = self._makeEnviron(PATH_INFO='') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('301 Moved Permanently' in body) + self.failUnless('http://example.com:6543/' in body) + + def test_path_info_slash_means_index_html(self): + environ = self._makeEnviron() + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('static' in body) + + def test_resource_out_of_bounds(self): + environ = self._makeEnviron() + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + inst.root_resource = 'abcdef' + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('404 Not Found' in body) + self.failUnless('http://example.com:6543/' in body) + + def test_resource_doesnt_exist(self): + environ = self._makeEnviron(PATH_INFO='/notthere') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('404 Not Found' in body) + self.failUnless('http://example.com:6543/' in body) + + def test_resource_isdir(self): + environ = self._makeEnviron(PATH_INFO='/subdir/') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('subdir' in body) + + def test_resource_is_file(self): + environ = self._makeEnviron(PATH_INFO='/index.html') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('static' in body) + + def test_resource_is_file_with_cache_max_age(self): + environ = self._makeEnviron(PATH_INFO='/index.html') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static', + cache_max_age=600) + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('static' in body) + self.assertEqual(len(sr.headerlist), 8) + header_names = [ x[0] for x in sr.headerlist ] + header_names.sort() + self.assertEqual(header_names, + ['Accept-Ranges', 'Cache-Control', + 'Content-Length', 'Content-Range', + 'Content-Type', 'ETag', 'Expires', 'Last-Modified']) + + def test_resource_is_file_with_no_cache_max_age(self): + environ = self._makeEnviron(PATH_INFO='/index.html') + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + body = response[0] + self.failUnless('static' in body) + self.assertEqual(len(sr.headerlist), 6) + header_names = [ x[0] for x in sr.headerlist ] + header_names.sort() + self.assertEqual(header_names, + ['Accept-Ranges', 'Content-Length', 'Content-Range', + 'Content-Type', 'ETag', 'Last-Modified']) + + def test_if_none_match(self): + class DummyEq(object): + def __eq__(self, other): + return True + dummy_eq = DummyEq() + environ = self._makeEnviron(HTTP_IF_NONE_MATCH=dummy_eq) + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + sr = DummyStartResponse() + response = inst(environ, sr) + self.assertEqual(len(sr.headerlist), 1) + self.assertEqual(sr.status, '304 Not Modified') + self.assertEqual(sr.headerlist[0][0], 'ETag') + self.assertEqual(response[0], '') + + def test_repr(self): + inst = self._makeOne('repoze.bfg.tests', 'fixtures/static') + self.failUnless( + repr(inst).startswith( + '