# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+import posixpath
import unittest
-from server_instance import ServerInstance
+from extensions_paths import PUBLIC_TEMPLATES
+from local_file_system import LocalFileSystem
+from object_store_creator import ObjectStoreCreator
+from path_canonicalizer import PathCanonicalizer
+from special_paths import SITE_VERIFICATION_FILE
class PathCanonicalizerTest(unittest.TestCase):
def setUp(self):
- self._server_instance = ServerInstance.ForLocal()
-
- def _Cze(self, path):
- return self._server_instance.path_canonicalizer.Canonicalize(path)
+ self._path_canonicalizer = PathCanonicalizer(
+ LocalFileSystem.Create(PUBLIC_TEMPLATES),
+ ObjectStoreCreator.ForTest(),
+ ('.html', '.md'))
def testSpecifyCorrectly(self):
- self._AssertIdentity('extensions/browserAction.html')
- self._AssertIdentity('extensions/storage.html')
- self._AssertIdentity('extensions/blah.html')
- self._AssertIdentity('extensions/index.html')
- self._AssertIdentity('extensions/whats_new.html')
- self._AssertIdentity('apps/storage.html')
- self._AssertIdentity('apps/bluetooth.html')
- self._AssertIdentity('apps/blah.html')
- self._AssertIdentity('static/browserAction.html')
- self._AssertIdentity('static/storage.html')
- self._AssertIdentity('static/bluetooth.html')
- self._AssertIdentity('static/blah.html')
+ self._AssertIdentity('extensions/browserAction')
+ self._AssertIdentity('extensions/storage')
+ self._AssertIdentity('extensions/blah')
+ self._AssertIdentity('extensions/index')
+ self._AssertIdentity('extensions/whats_new')
+ self._AssertIdentity('apps/storage')
+ self._AssertIdentity('apps/bluetooth')
+ self._AssertIdentity('apps/blah')
+ self._AssertIdentity('apps/tags/webview')
def testSpecifyIncorrectly(self):
- self._AssertTemporaryRedirect('extensions/browserAction.html',
- 'apps/browserAction.html')
- self._AssertTemporaryRedirect('apps/bluetooth.html',
- 'extensions/bluetooth.html')
- self._AssertTemporaryRedirect('extensions/index.html',
- 'apps/index.html')
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/browserAction', 'apps/browserAction')
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/browserAction', 'apps/extensions/browserAction')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/bluetooth', 'extensions/bluetooth')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/bluetooth', 'extensions/apps/bluetooth')
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/index', 'apps/index')
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/browserAction', 'static/browserAction')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/tags/webview', 'apps/webview')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/tags/webview', 'extensions/webview')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/tags/webview', 'extensions/tags/webview')
+
+ # These are a little trickier because storage.html is in both directories.
+ # They must canonicalize to the closest match.
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/storage', 'extensions/apps/storage')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/storage', 'apps/extensions/storage')
def testUnspecified(self):
- self._AssertTemporaryRedirect('extensions/browserAction.html',
- 'browserAction.html')
- self._AssertTemporaryRedirect('apps/bluetooth.html',
- 'bluetooth.html')
- # Extensions are default for now.
- self._AssertTemporaryRedirect('extensions/storage.html',
- 'storage.html')
+ self._AssertRedirectWithDefaultExtensions(
+ 'extensions/browserAction', 'browserAction')
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/bluetooth', 'bluetooth')
+ # Default happens to be apps because it's first alphabetically.
+ self._AssertRedirectWithDefaultExtensions(
+ 'apps/storage', 'storage')
# Nonexistent APIs should be left alone.
self._AssertIdentity('blah.html')
+ def testDirectories(self):
+ # Directories can be canonicalized too!
+ self._AssertIdentity('apps/')
+ self._AssertIdentity('apps/tags/')
+ self._AssertIdentity('extensions/')
+ # No trailing slash should be treated as files not directories, at least
+ # at least according to PathCanonicalizer.
+ self._AssertRedirect('extensions/apps', 'apps')
+ self._AssertRedirect('extensions', 'extensions')
+ # Just as tolerant of spelling mistakes.
+ self._AssertRedirect('apps/', 'Apps/')
+ self._AssertRedirect('apps/tags/', 'Apps/TAGS/')
+ self._AssertRedirect('extensions/', 'Extensions/')
+ # Find directories in the correct place.
+ self._AssertRedirect('apps/tags/', 'tags/')
+ self._AssertRedirect('apps/tags/', 'extensions/tags/')
+
def testSpellingErrors(self):
for spelme in ('browseraction', 'browseraction.htm', 'BrowserAction',
'BrowserAction.html', 'browseraction.html', 'Browseraction',
'browser-action', 'Browser.action.html', 'browser_action',
'browser-action.html', 'Browser_Action.html'):
- self._AssertTemporaryRedirect('extensions/browserAction.html', spelme)
- self._AssertTemporaryRedirect('extensions/browserAction.html',
- 'extensions/%s' % spelme)
- self._AssertTemporaryRedirect('extensions/browserAction.html',
- 'apps/%s' % spelme)
-
- def testChannelRedirect(self):
- def assert_channel_redirect(channel, path):
- self._AssertPermanentRedirect(path, '%s/%s' % (channel, path))
- for channel in ('stable', 'beta', 'dev', 'trunk'):
- assert_channel_redirect(channel, 'extensions/browserAction.html')
- assert_channel_redirect(channel, 'extensions/storage.html')
- assert_channel_redirect(channel, 'apps/bluetooth.html')
- assert_channel_redirect(channel, 'apps/storage.html')
+ self._AssertRedirect('extensions/browserAction', spelme)
+ self._AssertRedirect('extensions/browserAction', 'extensions/%s' % spelme)
+ self._AssertRedirect('extensions/browserAction', 'apps/%s' % spelme)
+
+ def testNonDefaultExtensions(self):
+ # The only example currently of a file with a non-default extension is
+ # the redirects.json file. That shouldn't have its extension stripped since
+ # it's not in the default extensions.
+ self._AssertIdentity('redirects.json')
+ self._AssertRedirect('redirects.json', 'redirects')
+ self._AssertRedirect('redirects.json', 'redirects.html')
+ self._AssertRedirect('redirects.json', 'redirects.js')
+ self._AssertRedirect('redirects.json', 'redirects.md')
+
+ def testSiteVerificationFile(self):
+ # The site verification file should not redirect.
+ self._AssertIdentity(SITE_VERIFICATION_FILE)
+ self._AssertRedirect(SITE_VERIFICATION_FILE,
+ posixpath.splitext(SITE_VERIFICATION_FILE)[0])
def _AssertIdentity(self, path):
- self._AssertTemporaryRedirect(path, path)
+ self._AssertRedirect(path, path)
+
+ def _AssertRedirect(self, to, from_):
+ self.assertEqual(to, self._path_canonicalizer.Canonicalize(from_))
- def _AssertTemporaryRedirect(self, to, from_):
- result = self._Cze(from_)
- self.assertEqual(to, result.path)
- self.assertFalse(result.permanent)
+ def _AssertRedirectWithDefaultExtensions(self, to, from_):
+ for ext in ('', '.html', '.md'):
+ self._AssertRedirect(
+ to, self._path_canonicalizer.Canonicalize(from_ + ext))
- def _AssertPermanentRedirect(self, to, from_):
- result = self._Cze(from_)
- self.assertEqual(to, result.path)
- self.assertTrue(result.permanent)
if __name__ == '__main__':
unittest.main()