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.
9 from extensions_paths
import PUBLIC_TEMPLATES
, SERVER2
10 from local_file_system
import LocalFileSystem
11 from test_file_system
import TestFileSystem
12 from object_store_creator
import ObjectStoreCreator
13 from path_canonicalizer
import PathCanonicalizer
14 from special_paths
import SITE_VERIFICATION_FILE
17 class PathCanonicalizerTest(unittest
.TestCase
):
19 self
._path
_canonicalizer
= PathCanonicalizer(
20 LocalFileSystem
.Create(PUBLIC_TEMPLATES
),
21 ObjectStoreCreator
.ForTest(),
24 def testSpecifyCorrectly(self
):
25 self
._AssertIdentity
('extensions/browserAction')
26 self
._AssertIdentity
('extensions/storage')
27 self
._AssertIdentity
('extensions/blah')
28 self
._AssertIdentity
('extensions/index')
29 self
._AssertIdentity
('extensions/whats_new')
30 self
._AssertIdentity
('apps/storage')
31 self
._AssertIdentity
('apps/bluetooth')
32 self
._AssertIdentity
('apps/blah')
33 self
._AssertIdentity
('apps/tags/webview')
35 def testSpecifyIncorrectly(self
):
36 self
._AssertRedirectWithDefaultExtensions
(
37 'extensions/browserAction', 'apps/browserAction')
38 self
._AssertRedirectWithDefaultExtensions
(
39 'extensions/browserAction', 'apps/extensions/browserAction')
40 self
._AssertRedirectWithDefaultExtensions
(
41 'apps/bluetooth', 'extensions/bluetooth')
42 self
._AssertRedirectWithDefaultExtensions
(
43 'apps/bluetooth', 'extensions/apps/bluetooth')
44 self
._AssertRedirectWithDefaultExtensions
(
45 'extensions/index', 'apps/index')
46 self
._AssertRedirectWithDefaultExtensions
(
47 'extensions/browserAction', 'static/browserAction')
48 self
._AssertRedirectWithDefaultExtensions
(
49 'apps/tags/webview', 'apps/webview')
50 self
._AssertRedirectWithDefaultExtensions
(
51 'apps/tags/webview', 'extensions/webview')
52 self
._AssertRedirectWithDefaultExtensions
(
53 'apps/tags/webview', 'extensions/tags/webview')
55 # These are a little trickier because storage.html is in both directories.
56 # They must canonicalize to the closest match.
57 self
._AssertRedirectWithDefaultExtensions
(
58 'extensions/storage', 'extensions/apps/storage')
59 self
._AssertRedirectWithDefaultExtensions
(
60 'apps/storage', 'apps/extensions/storage')
62 def testUnspecified(self
):
63 self
._AssertRedirectWithDefaultExtensions
(
64 'extensions/browserAction', 'browserAction')
65 self
._AssertRedirectWithDefaultExtensions
(
66 'apps/bluetooth', 'bluetooth')
67 # Default happens to be apps because it's first alphabetically.
68 self
._AssertRedirectWithDefaultExtensions
(
69 'apps/storage', 'storage')
70 # Nonexistent APIs should be left alone.
71 self
._AssertIdentity
('blah.html')
73 def testDirectories(self
):
74 # Directories can be canonicalized too!
75 self
._AssertIdentity
('apps/')
76 self
._AssertIdentity
('apps/tags/')
77 self
._AssertIdentity
('extensions/')
78 # No trailing slash should be treated as files not directories, at least
79 # at least according to PathCanonicalizer.
80 self
._AssertRedirect
('extensions/apps', 'apps')
81 self
._AssertRedirect
('extensions', 'extensions')
82 # Just as tolerant of spelling mistakes.
83 self
._AssertRedirect
('apps/', 'Apps/')
84 self
._AssertRedirect
('apps/tags/', 'Apps/TAGS/')
85 self
._AssertRedirect
('extensions/', 'Extensions/')
86 # Find directories in the correct place.
87 self
._AssertRedirect
('apps/tags/', 'tags/')
88 self
._AssertRedirect
('apps/tags/', 'extensions/tags/')
90 def testSpellingErrors(self
):
91 for spelme
in ('browseraction', 'browseraction.htm', 'BrowserAction',
92 'BrowserAction.html', 'browseraction.html', 'Browseraction',
93 'browser-action', 'Browser.action.html', 'browser_action',
94 'browser-action.html', 'Browser_Action.html'):
95 self
._AssertRedirect
('extensions/browserAction', spelme
)
96 self
._AssertRedirect
('extensions/browserAction', 'extensions/%s' % spelme
)
97 self
._AssertRedirect
('extensions/browserAction', 'apps/%s' % spelme
)
99 def testNonDefaultExtensions(self
):
100 # The only example currently of a file with a non-default extension is
101 # the redirects.json file. That shouldn't have its extension stripped since
102 # it's not in the default extensions.
103 self
._AssertIdentity
('redirects.json')
104 self
._AssertRedirect
('redirects.json', 'redirects')
105 self
._AssertRedirect
('redirects.json', 'redirects.html')
106 self
._AssertRedirect
('redirects.json', 'redirects.js')
107 self
._AssertRedirect
('redirects.json', 'redirects.md')
109 def testSiteVerificationFile(self
):
110 # The site verification file should not redirect.
111 self
._AssertIdentity
(SITE_VERIFICATION_FILE
)
112 self
._AssertRedirect
(SITE_VERIFICATION_FILE
,
113 posixpath
.splitext(SITE_VERIFICATION_FILE
)[0])
115 def testDotSeparated(self
):
116 self
._AssertIdentity
('extensions/devtools_inspectedWindow')
117 self
._AssertRedirect
('extensions/devtools_inspectedWindow',
118 'extensions/devtools.inspectedWindow')
120 def testUnderscoreSeparated(self
):
121 file_system
= TestFileSystem({
133 self
._path
_canonicalizer
= PathCanonicalizer(
135 ObjectStoreCreator
.ForTest(),
137 self
._AssertIdentity
('pepper_stable/c/index')
138 self
._AssertRedirect
('pepper_stable/c/index',
139 'pepper_stable/c/index.html')
141 def _AssertIdentity(self
, path
):
142 self
._AssertRedirect
(path
, path
)
144 def _AssertRedirect(self
, to
, from_
):
145 self
.assertEqual(to
, self
._path
_canonicalizer
.Canonicalize(from_
))
147 def _AssertRedirectWithDefaultExtensions(self
, to
, from_
):
148 for ext
in ('', '.html', '.md'):
149 self
._AssertRedirect
(
150 to
, self
._path
_canonicalizer
.Canonicalize(from_
+ ext
))
153 if __name__
== '__main__':