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 test_file_system import TestFileSystem
16 from third_party.handlebar import Handlebar
19 _HOST = 'https://developer.chrome.com/'
22 _REDIRECTS_JSON = json.dumps({
23 'oldfile.html': 'storage.html',
24 'index.html': 'https://developers.google.com/chrome',
28 # Test file system data which exercises many different mimetypes.
31 'a.txt': 'a.txt content',
32 'b.txt': 'b.txt content',
34 'd.txt': 'd.txt content',
39 'a.txt': 'a.txt content',
40 'b.txt': 'b.txt content',
42 'd.txt': 'd.txt content',
46 'img.png': 'img.png content',
47 'read.txt': 'read.txt content',
48 'redirects.json': _REDIRECTS_JSON,
49 'run.js': 'run.js content',
50 'site.css': 'site.css content',
51 'storage.html': 'storage.html content',
55 class ContentProviderUnittest(unittest.TestCase):
57 self._content_provider = self._CreateContentProvider()
59 def _CreateContentProvider(self, supports_zip=False):
60 test_file_system = TestFileSystem(_TEST_DATA)
61 return ContentProvider(
63 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()),
65 # TODO(kalman): Test supports_templates=False.
66 supports_templates=True,
67 supports_zip=supports_zip)
69 def _assertContent(self, content, content_type, content_and_type):
70 # Assert type so that str is differentiated from unicode.
71 self.assertEqual(type(content), type(content_and_type.content))
72 self.assertEqual(content, content_and_type.content)
73 self.assertEqual(content_type, content_and_type.content_type)
75 def testPlainText(self):
77 u'a.txt content', 'text/plain',
78 self._content_provider.GetContentAndType(_HOST, 'dir/a.txt').Get())
80 u'd.txt content', 'text/plain',
81 self._content_provider.GetContentAndType(_HOST, 'dir/c/d.txt').Get())
83 u'read.txt content', 'text/plain',
84 self._content_provider.GetContentAndType(_HOST, 'read.txt').Get())
86 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json',
87 self._content_provider.GetContentAndType(_HOST, 'redirects.json').Get())
89 u'run.js content', 'application/javascript',
90 self._content_provider.GetContentAndType(_HOST, 'run.js').Get())
92 u'site.css content', 'text/css',
93 self._content_provider.GetContentAndType(_HOST, 'site.css').Get())
95 def testTemplate(self):
96 content_and_type = self._content_provider.GetContentAndType(
97 _HOST, 'storage.html').Get()
98 self.assertEqual(Handlebar, type(content_and_type.content))
99 content_and_type.content = content_and_type.content.source
100 self._assertContent(u'storage.html content', 'text/html', content_and_type)
104 'img.png content', 'image/png',
105 self._content_provider.GetContentAndType(_HOST, 'img.png').Get())
107 def testZipTopLevel(self):
108 zip_content_provider = self._CreateContentProvider(supports_zip=True)
109 content_and_type = zip_content_provider.GetContentAndType(
110 _HOST, 'dir.zip').Get()
111 zipfile = ZipFile(StringIO(content_and_type.content))
112 content_and_type.content = zipfile.namelist()
114 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip',
117 def testZip2ndLevel(self):
118 zip_content_provider = self._CreateContentProvider(supports_zip=True)
119 content_and_type = zip_content_provider.GetContentAndType(
120 _HOST, 'dir2/dir3.zip').Get()
121 zipfile = ZipFile(StringIO(content_and_type.content))
122 content_and_type.content = zipfile.namelist()
124 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip',
127 def testNotFound(self):
130 self._content_provider.GetContentAndType(_HOST, 'oops').Get)
133 if __name__ == '__main__':