OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 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. |
| 5 |
| 6 from cStringIO import StringIO |
| 7 import json |
| 8 import unittest |
| 9 from zipfile import ZipFile |
| 10 |
| 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 |
| 17 |
| 18 |
| 19 _HOST = 'https://developer.chrome.com/' |
| 20 |
| 21 |
| 22 _REDIRECTS_JSON = json.dumps({ |
| 23 'oldfile.html': 'storage.html', |
| 24 'index.html': 'https://developers.google.com/chrome', |
| 25 }) |
| 26 |
| 27 |
| 28 # Test file system data which exercises many different mimetypes. |
| 29 _TEST_DATA = { |
| 30 'dir': { |
| 31 'a.txt': 'a.txt content', |
| 32 'b.txt': 'b.txt content', |
| 33 'c': { |
| 34 'd.txt': 'd.txt content', |
| 35 }, |
| 36 }, |
| 37 'dir2': { |
| 38 'dir3': { |
| 39 'a.txt': 'a.txt content', |
| 40 'b.txt': 'b.txt content', |
| 41 'c': { |
| 42 'd.txt': 'd.txt content', |
| 43 }, |
| 44 }, |
| 45 }, |
| 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', |
| 52 } |
| 53 |
| 54 |
| 55 class ContentProviderUnittest(unittest.TestCase): |
| 56 def setUp(self): |
| 57 self._content_provider = self._CreateContentProvider() |
| 58 |
| 59 def _CreateContentProvider(self, supports_zip=False): |
| 60 test_file_system = TestFileSystem(_TEST_DATA) |
| 61 return ContentProvider( |
| 62 'foo', |
| 63 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), |
| 64 test_file_system, |
| 65 # TODO(kalman): Test supports_templates=False. |
| 66 supports_templates=True, |
| 67 supports_zip=supports_zip) |
| 68 |
| 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) |
| 74 |
| 75 def testPlainText(self): |
| 76 self._assertContent( |
| 77 u'a.txt content', 'text/plain', |
| 78 self._content_provider.GetContentAndType(_HOST, 'dir/a.txt').Get()) |
| 79 self._assertContent( |
| 80 u'd.txt content', 'text/plain', |
| 81 self._content_provider.GetContentAndType(_HOST, 'dir/c/d.txt').Get()) |
| 82 self._assertContent( |
| 83 u'read.txt content', 'text/plain', |
| 84 self._content_provider.GetContentAndType(_HOST, 'read.txt').Get()) |
| 85 self._assertContent( |
| 86 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json', |
| 87 self._content_provider.GetContentAndType(_HOST, 'redirects.json').Get()) |
| 88 self._assertContent( |
| 89 u'run.js content', 'application/javascript', |
| 90 self._content_provider.GetContentAndType(_HOST, 'run.js').Get()) |
| 91 self._assertContent( |
| 92 u'site.css content', 'text/css', |
| 93 self._content_provider.GetContentAndType(_HOST, 'site.css').Get()) |
| 94 |
| 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) |
| 101 |
| 102 def testImage(self): |
| 103 self._assertContent( |
| 104 'img.png content', 'image/png', |
| 105 self._content_provider.GetContentAndType(_HOST, 'img.png').Get()) |
| 106 |
| 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() |
| 113 self._assertContent( |
| 114 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip', |
| 115 content_and_type) |
| 116 |
| 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() |
| 123 self._assertContent( |
| 124 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', |
| 125 content_and_type) |
| 126 |
| 127 def testNotFound(self): |
| 128 self.assertRaises( |
| 129 FileNotFoundError, |
| 130 self._content_provider.GetContentAndType(_HOST, 'oops').Get) |
| 131 |
| 132 |
| 133 if __name__ == '__main__': |
| 134 unittest.main() |
OLD | NEW |