| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 from cStringIO import StringIO | 6 from cStringIO import StringIO |
| 7 import json | 7 import json |
| 8 import unittest | 8 import unittest |
| 9 from zipfile import ZipFile | 9 from zipfile import ZipFile |
| 10 | 10 |
| 11 from compiled_file_system import CompiledFileSystem | 11 from compiled_file_system import CompiledFileSystem |
| 12 from content_provider import ContentProvider | 12 from content_provider import ContentProvider |
| 13 from file_system import FileNotFoundError | 13 from file_system import FileNotFoundError |
| 14 from object_store_creator import ObjectStoreCreator | 14 from object_store_creator import ObjectStoreCreator |
| 15 from path_canonicalizer import PathCanonicalizer |
| 15 from test_file_system import TestFileSystem | 16 from test_file_system import TestFileSystem |
| 16 from third_party.handlebar import Handlebar | 17 from third_party.handlebar import Handlebar |
| 17 | 18 |
| 18 _REDIRECTS_JSON = json.dumps({ | 19 _REDIRECTS_JSON = json.dumps({ |
| 19 'oldfile.html': 'storage.html', | 20 'oldfile.html': 'storage.html', |
| 20 'index.html': 'https://developers.google.com/chrome', | 21 'index.html': 'https://developers.google.com/chrome', |
| 21 }) | 22 }) |
| 22 | 23 |
| 23 | 24 |
| 24 _MARKDOWN_CONTENT = ( | 25 _MARKDOWN_CONTENT = ( |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 }, | 41 }, |
| 41 'dir2': { | 42 'dir2': { |
| 42 'dir3': { | 43 'dir3': { |
| 43 'a.txt': 'a.txt content', | 44 'a.txt': 'a.txt content', |
| 44 'b.txt': 'b.txt content', | 45 'b.txt': 'b.txt content', |
| 45 'c': { | 46 'c': { |
| 46 'd.txt': 'd.txt content', | 47 'd.txt': 'd.txt content', |
| 47 }, | 48 }, |
| 48 }, | 49 }, |
| 49 }, | 50 }, |
| 51 'dir.txt': 'dir.txt content', |
| 50 'img.png': 'img.png content', | 52 'img.png': 'img.png content', |
| 51 'read.txt': 'read.txt content', | 53 'read.txt': 'read.txt content', |
| 52 'redirects.json': _REDIRECTS_JSON, | 54 'redirects.json': _REDIRECTS_JSON, |
| 53 'run.js': 'run.js content', | 55 'run.js': 'run.js content', |
| 54 'site.css': 'site.css content', | 56 'site.css': 'site.css content', |
| 55 'storage.html': 'storage.html content', | 57 'storage.html': 'storage.html content', |
| 56 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT) | 58 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT) |
| 57 } | 59 } |
| 58 | 60 |
| 59 | 61 |
| 60 class ContentProviderUnittest(unittest.TestCase): | 62 class ContentProviderUnittest(unittest.TestCase): |
| 61 def setUp(self): | 63 def setUp(self): |
| 62 self._content_provider = self._CreateContentProvider() | 64 self._content_provider = self._CreateContentProvider() |
| 63 | 65 |
| 64 def _CreateContentProvider(self, supports_zip=False): | 66 def _CreateContentProvider(self, supports_zip=False): |
| 67 object_store_creator = ObjectStoreCreator.ForTest() |
| 65 test_file_system = TestFileSystem(_TEST_DATA) | 68 test_file_system = TestFileSystem(_TEST_DATA) |
| 66 return ContentProvider( | 69 return ContentProvider( |
| 67 'foo', | 70 'foo', |
| 68 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), | 71 CompiledFileSystem.Factory(object_store_creator), |
| 69 test_file_system, | 72 test_file_system, |
| 73 object_store_creator, |
| 74 default_extensions=('.html', '.md'), |
| 70 # TODO(kalman): Test supports_templates=False. | 75 # TODO(kalman): Test supports_templates=False. |
| 71 supports_templates=True, | 76 supports_templates=True, |
| 72 supports_zip=supports_zip) | 77 supports_zip=supports_zip) |
| 73 | 78 |
| 74 def _assertContent(self, content, content_type, content_and_type): | 79 def _assertContent(self, content, content_type, content_and_type): |
| 75 # Assert type so that str is differentiated from unicode. | 80 # Assert type so that str is differentiated from unicode. |
| 76 self.assertEqual(type(content), type(content_and_type.content)) | 81 self.assertEqual(type(content), type(content_and_type.content)) |
| 77 self.assertEqual(content, content_and_type.content) | 82 self.assertEqual(content, content_and_type.content) |
| 78 self.assertEqual(content_type, content_and_type.content_type) | 83 self.assertEqual(content_type, content_and_type.content_type) |
| 79 | 84 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 def testZip2ndLevel(self): | 126 def testZip2ndLevel(self): |
| 122 zip_content_provider = self._CreateContentProvider(supports_zip=True) | 127 zip_content_provider = self._CreateContentProvider(supports_zip=True) |
| 123 content_and_type = zip_content_provider.GetContentAndType( | 128 content_and_type = zip_content_provider.GetContentAndType( |
| 124 'dir2/dir3.zip').Get() | 129 'dir2/dir3.zip').Get() |
| 125 zipfile = ZipFile(StringIO(content_and_type.content)) | 130 zipfile = ZipFile(StringIO(content_and_type.content)) |
| 126 content_and_type.content = zipfile.namelist() | 131 content_and_type.content = zipfile.namelist() |
| 127 self._assertContent( | 132 self._assertContent( |
| 128 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', | 133 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', |
| 129 content_and_type) | 134 content_and_type) |
| 130 | 135 |
| 136 def testCanonicalZipPaths(self): |
| 137 # Without supports_zip the path is canonicalized as a file. |
| 138 self.assertEqual( |
| 139 'dir.txt', |
| 140 self._content_provider.GetCanonicalPath('dir.zip')) |
| 141 self.assertEqual( |
| 142 'dir.txt', |
| 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) |
| 147 self.assertEqual( |
| 148 'dir.zip', |
| 149 zip_content_provider.GetCanonicalPath('dir.zip')) |
| 150 self.assertEqual( |
| 151 'dir.zip', |
| 152 zip_content_provider.GetCanonicalPath('diR.zip')) |
| 153 |
| 131 def testMarkdown(self): | 154 def testMarkdown(self): |
| 132 content_and_type = self._content_provider.GetContentAndType( | 155 content_and_type = self._content_provider.GetContentAndType( |
| 133 'markdown.html').Get() | 156 'markdown').Get() |
| 134 content_and_type.content = content_and_type.content.source | 157 content_and_type.content = content_and_type.content.source |
| 135 self._assertContent('\n'.join(text[1] for text in _MARKDOWN_CONTENT), | 158 self._assertContent('\n'.join(text[1] for text in _MARKDOWN_CONTENT), |
| 136 'text/html', content_and_type) | 159 'text/html', content_and_type) |
| 137 | 160 |
| 138 def testNotFound(self): | 161 def testNotFound(self): |
| 139 self.assertRaises( | 162 self.assertRaises( |
| 140 FileNotFoundError, | 163 FileNotFoundError, |
| 141 self._content_provider.GetContentAndType('oops').Get) | 164 self._content_provider.GetContentAndType('oops').Get) |
| 142 | 165 |
| 143 | 166 |
| 144 if __name__ == '__main__': | 167 if __name__ == '__main__': |
| 145 unittest.main() | 168 unittest.main() |
| OLD | NEW |