1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
#!/usr/bin/env python
# Copyright 2013 The Chromium Authors. All rights reserved.
# 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 extensions_paths import PUBLIC_TEMPLATES, SERVER2
from local_file_system import LocalFileSystem
from test_file_system import TestFileSystem
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._path_canonicalizer = PathCanonicalizer(
LocalFileSystem.Create(PUBLIC_TEMPLATES),
ObjectStoreCreator.ForTest(),
('.html', '.md'))
def testSpecifyCorrectly(self):
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._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._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._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 testDotSeparated(self):
self._AssertIdentity('extensions/devtools_inspectedWindow')
self._AssertRedirect('extensions/devtools_inspectedWindow',
'extensions/devtools.inspectedWindow')
def testUnderscoreSeparated(self):
file_system = TestFileSystem({
'pepper_dev': {
'c': {
'index.html': ''
}
},
'pepper_stable': {
'c': {
'index.html': ''
}
}
})
self._path_canonicalizer = PathCanonicalizer(
file_system,
ObjectStoreCreator.ForTest(),
('.html', '.md'))
self._AssertIdentity('pepper_stable/c/index')
self._AssertRedirect('pepper_stable/c/index',
'pepper_stable/c/index.html')
def _AssertIdentity(self, path):
self._AssertRedirect(path, path)
def _AssertRedirect(self, to, from_):
self.assertEqual(to, self._path_canonicalizer.Canonicalize(from_))
def _AssertRedirectWithDefaultExtensions(self, to, from_):
for ext in ('', '.html', '.md'):
self._AssertRedirect(
to, self._path_canonicalizer.Canonicalize(from_ + ext))
if __name__ == '__main__':
unittest.main()
|