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 test_file_system import TestFileSystem | 15 from test_file_system import TestFileSystem |
16 from third_party.handlebar import Handlebar | 16 from third_party.handlebar import Handlebar |
17 | 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 ('# Header 1 #', u'<h1 id="header-1">Header 1</h1>'), | |
26 ('1. Foo\n', u'<ol>\n<li>Foo</li>\n</ol>'), | |
27 ('\n', | |
28 '<p><img alt="alt text" src="/path/img.jpg" title="Title" /></p>'), | |
29 ('* Unordered item 1', u'<ul>\n<li>Unordered item 1</li>\n</ul>') | |
30 ) | |
31 | |
32 # Test file system data which exercises many different mimetypes. | 25 # Test file system data which exercises many different mimetypes. |
33 _TEST_DATA = { | 26 _TEST_DATA = { |
34 'dir': { | 27 'dir': { |
35 'a.txt': 'a.txt content', | 28 'a.txt': 'a.txt content', |
36 'b.txt': 'b.txt content', | 29 'b.txt': 'b.txt content', |
37 'c': { | 30 'c': { |
38 'd.txt': 'd.txt content', | 31 'd.txt': 'd.txt content', |
39 }, | 32 }, |
40 }, | 33 }, |
41 'dir2': { | 34 'dir2': { |
42 'dir3': { | 35 'dir3': { |
43 'a.txt': 'a.txt content', | 36 'a.txt': 'a.txt content', |
44 'b.txt': 'b.txt content', | 37 'b.txt': 'b.txt content', |
45 'c': { | 38 'c': { |
46 'd.txt': 'd.txt content', | 39 'd.txt': 'd.txt content', |
47 }, | 40 }, |
48 }, | 41 }, |
49 }, | 42 }, |
50 'img.png': 'img.png content', | 43 'img.png': 'img.png content', |
51 'read.txt': 'read.txt content', | 44 'read.txt': 'read.txt content', |
52 'redirects.json': _REDIRECTS_JSON, | 45 'redirects.json': _REDIRECTS_JSON, |
53 'run.js': 'run.js content', | 46 'run.js': 'run.js content', |
54 'site.css': 'site.css content', | 47 'site.css': 'site.css content', |
55 'storage.html': 'storage.html content', | 48 'storage.html': 'storage.html content', |
56 'markdown.md': '\n'.join(text[0] for text in _MARKDOWN_CONTENT) | |
57 } | 49 } |
58 | 50 |
59 | 51 |
60 class ContentProviderUnittest(unittest.TestCase): | 52 class ContentProviderUnittest(unittest.TestCase): |
61 def setUp(self): | 53 def setUp(self): |
62 self._content_provider = self._CreateContentProvider() | 54 self._content_provider = self._CreateContentProvider() |
63 | 55 |
64 def _CreateContentProvider(self, supports_zip=False): | 56 def _CreateContentProvider(self, supports_zip=False): |
65 test_file_system = TestFileSystem(_TEST_DATA) | 57 test_file_system = TestFileSystem(_TEST_DATA) |
66 return ContentProvider( | 58 return ContentProvider( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 def testZip2ndLevel(self): | 113 def testZip2ndLevel(self): |
122 zip_content_provider = self._CreateContentProvider(supports_zip=True) | 114 zip_content_provider = self._CreateContentProvider(supports_zip=True) |
123 content_and_type = zip_content_provider.GetContentAndType( | 115 content_and_type = zip_content_provider.GetContentAndType( |
124 'dir2/dir3.zip').Get() | 116 'dir2/dir3.zip').Get() |
125 zipfile = ZipFile(StringIO(content_and_type.content)) | 117 zipfile = ZipFile(StringIO(content_and_type.content)) |
126 content_and_type.content = zipfile.namelist() | 118 content_and_type.content = zipfile.namelist() |
127 self._assertContent( | 119 self._assertContent( |
128 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', | 120 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', |
129 content_and_type) | 121 content_and_type) |
130 | 122 |
131 def testMarkdown(self): | |
132 content_and_type = self._content_provider.GetContentAndType( | |
133 'markdown.html').Get() | |
134 content_and_type.content = content_and_type.content.source | |
135 self._assertContent('\n'.join(text[1] for text in _MARKDOWN_CONTENT), | |
136 'text/html', content_and_type) | |
137 | |
138 def testNotFound(self): | 123 def testNotFound(self): |
139 self.assertRaises( | 124 self.assertRaises( |
140 FileNotFoundError, | 125 FileNotFoundError, |
141 self._content_provider.GetContentAndType('oops').Get) | 126 self._content_provider.GetContentAndType('oops').Get) |
142 | 127 |
143 | 128 |
144 if __name__ == '__main__': | 129 if __name__ == '__main__': |
145 unittest.main() | 130 unittest.main() |
OLD | NEW |