Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / path_canonicalizer_test.py
1 #!/usr/bin/env python
2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
5
6 import posixpath
7 import unittest
8
9 from extensions_paths import PUBLIC_TEMPLATES
10 from local_file_system import LocalFileSystem
11 from object_store_creator import ObjectStoreCreator
12 from path_canonicalizer import PathCanonicalizer
13 from special_paths import SITE_VERIFICATION_FILE
14
15
16 class PathCanonicalizerTest(unittest.TestCase):
17   def setUp(self):
18     self._path_canonicalizer = PathCanonicalizer(
19         LocalFileSystem.Create(PUBLIC_TEMPLATES),
20         ObjectStoreCreator.ForTest(),
21         ('.html', '.md'))
22
23   def testSpecifyCorrectly(self):
24     self._AssertIdentity('extensions/browserAction')
25     self._AssertIdentity('extensions/storage')
26     self._AssertIdentity('extensions/blah')
27     self._AssertIdentity('extensions/index')
28     self._AssertIdentity('extensions/whats_new')
29     self._AssertIdentity('apps/storage')
30     self._AssertIdentity('apps/bluetooth')
31     self._AssertIdentity('apps/blah')
32     self._AssertIdentity('apps/tags/webview')
33
34   def testSpecifyIncorrectly(self):
35     self._AssertRedirectWithDefaultExtensions(
36         'extensions/browserAction', 'apps/browserAction')
37     self._AssertRedirectWithDefaultExtensions(
38         'extensions/browserAction', 'apps/extensions/browserAction')
39     self._AssertRedirectWithDefaultExtensions(
40         'apps/bluetooth', 'extensions/bluetooth')
41     self._AssertRedirectWithDefaultExtensions(
42         'apps/bluetooth', 'extensions/apps/bluetooth')
43     self._AssertRedirectWithDefaultExtensions(
44         'extensions/index', 'apps/index')
45     self._AssertRedirectWithDefaultExtensions(
46         'extensions/browserAction', 'static/browserAction')
47     self._AssertRedirectWithDefaultExtensions(
48         'apps/tags/webview', 'apps/webview')
49     self._AssertRedirectWithDefaultExtensions(
50         'apps/tags/webview', 'extensions/webview')
51     self._AssertRedirectWithDefaultExtensions(
52         'apps/tags/webview', 'extensions/tags/webview')
53
54     # These are a little trickier because storage.html is in both directories.
55     # They must canonicalize to the closest match.
56     self._AssertRedirectWithDefaultExtensions(
57         'extensions/storage', 'extensions/apps/storage')
58     self._AssertRedirectWithDefaultExtensions(
59         'apps/storage', 'apps/extensions/storage')
60
61   def testUnspecified(self):
62     self._AssertRedirectWithDefaultExtensions(
63         'extensions/browserAction', 'browserAction')
64     self._AssertRedirectWithDefaultExtensions(
65         'apps/bluetooth', 'bluetooth')
66     # Default happens to be apps because it's first alphabetically.
67     self._AssertRedirectWithDefaultExtensions(
68         'apps/storage', 'storage')
69     # Nonexistent APIs should be left alone.
70     self._AssertIdentity('blah.html')
71
72   def testDirectories(self):
73     # Directories can be canonicalized too!
74     self._AssertIdentity('apps/')
75     self._AssertIdentity('apps/tags/')
76     self._AssertIdentity('extensions/')
77     # No trailing slash should be treated as files not directories, at least
78     # at least according to PathCanonicalizer.
79     self._AssertRedirect('extensions/apps', 'apps')
80     self._AssertRedirect('extensions', 'extensions')
81     # Just as tolerant of spelling mistakes.
82     self._AssertRedirect('apps/', 'Apps/')
83     self._AssertRedirect('apps/tags/', 'Apps/TAGS/')
84     self._AssertRedirect('extensions/', 'Extensions/')
85     # Find directories in the correct place.
86     self._AssertRedirect('apps/tags/', 'tags/')
87     self._AssertRedirect('apps/tags/', 'extensions/tags/')
88
89   def testSpellingErrors(self):
90     for spelme in ('browseraction', 'browseraction.htm', 'BrowserAction',
91                    'BrowserAction.html', 'browseraction.html', 'Browseraction',
92                    'browser-action', 'Browser.action.html', 'browser_action',
93                    'browser-action.html', 'Browser_Action.html'):
94       self._AssertRedirect('extensions/browserAction', spelme)
95       self._AssertRedirect('extensions/browserAction', 'extensions/%s' % spelme)
96       self._AssertRedirect('extensions/browserAction', 'apps/%s' % spelme)
97
98   def testNonDefaultExtensions(self):
99     # The only example currently of a file with a non-default extension is
100     # the redirects.json file. That shouldn't have its extension stripped since
101     # it's not in the default extensions.
102     self._AssertIdentity('redirects.json')
103     self._AssertRedirect('redirects.json', 'redirects')
104     self._AssertRedirect('redirects.json', 'redirects.html')
105     self._AssertRedirect('redirects.json', 'redirects.js')
106     self._AssertRedirect('redirects.json', 'redirects.md')
107
108   def testSiteVerificationFile(self):
109     # The site verification file should not redirect.
110     self._AssertIdentity(SITE_VERIFICATION_FILE)
111     self._AssertRedirect(SITE_VERIFICATION_FILE,
112                          posixpath.splitext(SITE_VERIFICATION_FILE)[0])
113
114   def _AssertIdentity(self, path):
115     self._AssertRedirect(path, path)
116
117   def _AssertRedirect(self, to, from_):
118     self.assertEqual(to, self._path_canonicalizer.Canonicalize(from_))
119
120   def _AssertRedirectWithDefaultExtensions(self, to, from_):
121     for ext in ('', '.html', '.md'):
122       self._AssertRedirect(
123           to, self._path_canonicalizer.Canonicalize(from_ + ext))
124
125
126 if __name__ == '__main__':
127   unittest.main()