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.
6 from cStringIO import StringIO
9 from zipfile import ZipFile
11 from compiled_file_system import CompiledFileSystem
12 from content_provider import ContentProvider
13 from file_system import FileNotFoundError
14 from object_store_creator import ObjectStoreCreator
15 from path_canonicalizer import PathCanonicalizer
16 from test_file_system import TestFileSystem
17 from third_party.handlebar import Handlebar
19 _REDIRECTS_JSON = json.dumps({
20 'oldfile.html': 'storage.html',
21 'index.html': 'https://developers.google.com/chrome',
26 ('# Header 1 #', u'<h1 id="header-1">Header 1</h1>'),
27 ('1. Foo\n', u'<ol>\n<li>Foo</li>\n</ol>'),
28 ('![alt text](/path/img.jpg "Title")\n',
29 '<p><img alt="alt text" src="/path/img.jpg" title="Title" /></p>'),
30 ('* Unordered item 1', u'<ul>\n<li>Unordered item 1</li>\n</ul>')
33 # Test file system data which exercises many different mimetypes.
36 'a.txt': 'a.txt content',
37 'b.txt': 'b.txt content',
39 'd.txt': 'd.txt content',
44 'a.txt': 'a.txt content',
45 'b.txt': 'b.txt content',
47 'd.txt': 'd.txt content',
51 'dir.txt': 'dir.txt content',
52 'img.png': 'img.png content',
53 'read.txt': 'read.txt content',
54 'redirects.json': _REDIRECTS_JSON,
55 'run.js': 'run.js content',
56 'site.css': 'site.css content',
57 'storage.html': 'storage.html content',
58 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT)
62 class ContentProviderUnittest(unittest.TestCase):
64 self._content_provider = self._CreateContentProvider()
66 def _CreateContentProvider(self, supports_zip=False):
67 object_store_creator = ObjectStoreCreator.ForTest()
68 test_file_system = TestFileSystem(_TEST_DATA)
69 return ContentProvider(
71 CompiledFileSystem.Factory(object_store_creator),
74 default_extensions=('.html', '.md'),
75 # TODO(kalman): Test supports_templates=False.
76 supports_templates=True,
77 supports_zip=supports_zip)
79 def _assertContent(self, content, content_type, content_and_type):
80 # Assert type so that str is differentiated from unicode.
81 self.assertEqual(type(content), type(content_and_type.content))
82 self.assertEqual(content, content_and_type.content)
83 self.assertEqual(content_type, content_and_type.content_type)
85 def testPlainText(self):
87 u'a.txt content', 'text/plain',
88 self._content_provider.GetContentAndType('dir/a.txt').Get())
90 u'd.txt content', 'text/plain',
91 self._content_provider.GetContentAndType('dir/c/d.txt').Get())
93 u'read.txt content', 'text/plain',
94 self._content_provider.GetContentAndType('read.txt').Get())
96 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json',
97 self._content_provider.GetContentAndType('redirects.json').Get())
99 u'run.js content', 'application/javascript',
100 self._content_provider.GetContentAndType('run.js').Get())
102 u'site.css content', 'text/css',
103 self._content_provider.GetContentAndType('site.css').Get())
105 def testTemplate(self):
106 content_and_type = self._content_provider.GetContentAndType(
107 'storage.html').Get()
108 self.assertEqual(Handlebar, type(content_and_type.content))
109 content_and_type.content = content_and_type.content.source
110 self._assertContent(u'storage.html content', 'text/html', content_and_type)
114 'img.png content', 'image/png',
115 self._content_provider.GetContentAndType('img.png').Get())
117 def testZipTopLevel(self):
118 zip_content_provider = self._CreateContentProvider(supports_zip=True)
119 content_and_type = zip_content_provider.GetContentAndType('dir.zip').Get()
120 zipfile = ZipFile(StringIO(content_and_type.content))
121 content_and_type.content = zipfile.namelist()
123 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip',
126 def testZip2ndLevel(self):
127 zip_content_provider = self._CreateContentProvider(supports_zip=True)
128 content_and_type = zip_content_provider.GetContentAndType(
129 'dir2/dir3.zip').Get()
130 zipfile = ZipFile(StringIO(content_and_type.content))
131 content_and_type.content = zipfile.namelist()
133 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip',
136 def testCanonicalZipPaths(self):
137 # Without supports_zip the path is canonicalized as a file.
140 self._content_provider.GetCanonicalPath('dir.zip'))
143 self._content_provider.GetCanonicalPath('diR.zip'))
144 # With supports_zip the path is canonicalized as the zip file which
145 # corresponds to the canonical directory.
146 zip_content_provider = self._CreateContentProvider(supports_zip=True)
149 zip_content_provider.GetCanonicalPath('dir.zip'))
152 zip_content_provider.GetCanonicalPath('diR.zip'))
154 def testMarkdown(self):
155 content_and_type = self._content_provider.GetContentAndType(
157 content_and_type.content = content_and_type.content.source
158 self._assertContent('\n'.join(text[1] for text in _MARKDOWN_CONTENT),
159 'text/html', content_and_type)
161 def testNotFound(self):
164 self._content_provider.GetContentAndType('oops').Get)
167 if __name__ == '__main__':