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 the differnet mimetypes and different | |
Jeffrey Yasskin
2013/11/04 21:21:18
sp: differnet
not at google - send to devlin
2013/11/04 23:34:49
Done.
| |
29 # types of redirects. | |
Jeffrey Yasskin
2013/11/04 21:21:18
Why are redirects relevant to ContentProvider?
not at google - send to devlin
2013/11/04 23:34:49
Heh, I guess they were at some point. Not any more
| |
30 _TEST_DATA = { | |
31 'dir': { | |
32 'a.txt': 'a.txt content', | |
33 'b.txt': 'b.txt content', | |
34 'c': { | |
35 'd.txt': 'd.txt content', | |
36 }, | |
37 }, | |
38 'dir2': { | |
39 'dir3': { | |
40 'a.txt': 'a.txt content', | |
41 'b.txt': 'b.txt content', | |
42 'c': { | |
43 'd.txt': 'd.txt content', | |
44 }, | |
45 }, | |
46 }, | |
47 'img.png': 'img.png content', | |
48 'read.txt': 'read.txt content', | |
49 'redirects.json': _REDIRECTS_JSON, | |
50 'run.js': 'run.js content', | |
51 'site.css': 'site.css content', | |
52 'storage.html': 'storage.html content', | |
53 } | |
54 | |
55 | |
56 class ContentProviderUnittest(unittest.TestCase): | |
57 def setUp(self): | |
58 self._content_provider = self._CreateContentProvider() | |
59 | |
60 def _CreateContentProvider(self, supports_zip=False): | |
61 test_file_system = TestFileSystem(_TEST_DATA) | |
62 return ContentProvider( | |
63 'foo', | |
64 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()), | |
65 test_file_system, | |
66 test_file_system, | |
67 # TODO(kalman): Test supports_templates=False (json: supportsTemplates). | |
Jeffrey Yasskin
2013/11/04 21:21:18
What's the "json:" note?
not at google - send to devlin
2013/11/04 23:34:49
That is appears as "supportsTemplates: false" in t
| |
68 supports_templates=True, | |
69 supports_zip=supports_zip) | |
70 | |
71 def _assertContent(self, content, content_type, content_info): | |
72 # Assert type so that str is differentiated from unicode. | |
73 self.assertEqual(type(content), type(content_info.content)) | |
74 self.assertEqual(content, content_info.content) | |
75 self.assertEqual(content_type, content_info.content_type) | |
76 | |
77 def _assertRedirect(self, redirect, permanent, content_info): | |
Jeffrey Yasskin
2013/11/04 21:21:18
This isn't used.
not at google - send to devlin
2013/11/04 23:34:49
Ah. In a past life ContentProvider did redirects (
| |
78 self.assertEqual(None, content_info.content) | |
79 self.assertEqual(None, content_info.content_type) | |
80 self.assertEqual(redirect, content_info.redirect) | |
81 self.assertEqual(permanent, content_info.permanent) | |
82 | |
83 def testPlainText(self): | |
84 self._assertContent( | |
85 u'a.txt content', 'text/plain', | |
86 self._content_provider.GetContentInfo(_HOST, 'dir/a.txt').Get()) | |
87 self._assertContent( | |
88 u'd.txt content', 'text/plain', | |
89 self._content_provider.GetContentInfo(_HOST, 'dir/c/d.txt').Get()) | |
90 self._assertContent( | |
91 u'read.txt content', 'text/plain', | |
92 self._content_provider.GetContentInfo(_HOST, 'read.txt').Get()) | |
93 self._assertContent( | |
94 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json', | |
95 self._content_provider.GetContentInfo(_HOST, 'redirects.json').Get()) | |
96 self._assertContent( | |
97 u'run.js content', 'application/javascript', | |
98 self._content_provider.GetContentInfo(_HOST, 'run.js').Get()) | |
99 self._assertContent( | |
100 u'site.css content', 'text/css', | |
101 self._content_provider.GetContentInfo(_HOST, 'site.css').Get()) | |
102 | |
103 def testTemplate(self): | |
104 content_info = self._content_provider.GetContentInfo( | |
105 _HOST, 'storage.html').Get() | |
106 self.assertEqual(Handlebar, type(content_info.content)) | |
107 content_info.content = content_info.content.source | |
108 self._assertContent(u'storage.html content', 'text/html', content_info) | |
109 | |
110 def testImage(self): | |
111 self._assertContent( | |
112 'img.png content', 'image/png', | |
113 self._content_provider.GetContentInfo(_HOST, 'img.png').Get()) | |
114 | |
115 def testZipTopLevel(self): | |
116 zip_content_provider = self._CreateContentProvider(supports_zip=True) | |
117 content_info = zip_content_provider.GetContentInfo(_HOST, 'dir.zip').Get() | |
118 zipfile = ZipFile(StringIO(content_info.content)) | |
119 content_info.content = zipfile.namelist() | |
120 self._assertContent( | |
121 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip', | |
122 content_info) | |
123 | |
124 def testZip2ndLevel(self): | |
125 zip_content_provider = self._CreateContentProvider(supports_zip=True) | |
126 content_info = zip_content_provider.GetContentInfo( | |
127 _HOST, 'dir2/dir3.zip').Get() | |
128 zipfile = ZipFile(StringIO(content_info.content)) | |
129 content_info.content = zipfile.namelist() | |
130 self._assertContent( | |
131 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip', | |
132 content_info) | |
133 | |
134 def testNotFound(self): | |
135 self.assertRaises(FileNotFoundError, | |
136 self._content_provider.GetContentInfo(_HOST, 'oops').Get) | |
137 | |
138 | |
139 if __name__ == '__main__': | |
140 unittest.main() | |
OLD | NEW |