| 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 import unittest | 6 import unittest |
| 7 | 7 |
| 8 from extensions_paths import EXAMPLES, PUBLIC_TEMPLATES, STATIC_DOCS | 8 from extensions_paths import EXAMPLES, PUBLIC_TEMPLATES, STATIC_DOCS |
| 9 from local_file_system import LocalFileSystem | 9 from local_file_system import LocalFileSystem |
| 10 from render_servlet import RenderServlet | 10 from render_servlet import RenderServlet |
| 11 from server_instance import ServerInstance | 11 from server_instance import ServerInstance |
| 12 from servlet import Request, Response | 12 from servlet import Request, Response |
| 13 from test_util import ReadFile | 13 from test_util import ReadFile |
| 14 | 14 |
| 15 | 15 |
| 16 class _RenderServletDelegate(RenderServlet.Delegate): | 16 class _RenderServletDelegate(RenderServlet.Delegate): |
| 17 def CreateServerInstance(self): | 17 def CreateServerInstance(self): |
| 18 return ServerInstance.ForTest(LocalFileSystem.Create()) | 18 return ServerInstance.ForTest(LocalFileSystem.Create()) |
| 19 | 19 |
| 20 | 20 |
| 21 class RenderServletTest(unittest.TestCase): | 21 class RenderServletTest(unittest.TestCase): |
| 22 def _Render(self, path): | 22 def _Render(self, path): |
| 23 return RenderServlet(Request.ForTest(path), | 23 return RenderServlet(Request.ForTest(path), |
| 24 _RenderServletDelegate()).Get() | 24 _RenderServletDelegate()).Get() |
| 25 | 25 |
| 26 def testExtensionAppRedirect(self): | 26 def testExtensionAppRedirect(self): |
| 27 self.assertEqual( | 27 self.assertEqual( |
| 28 Response.Redirect('/apps/storage.html', permanent=False), | 28 Response.Redirect('/apps/storage', permanent=False), |
| 29 self._Render('storage.html')) | 29 self._Render('storage')) |
| 30 | 30 |
| 31 def testChannelRedirect(self): | 31 def testChannelRedirect(self): |
| 32 for channel in ('stable', 'beta', 'dev', 'trunk'): | 32 for channel in ('stable', 'beta', 'dev', 'trunk'): |
| 33 self.assertEqual( | 33 self.assertEqual( |
| 34 Response.Redirect('/extensions/storage.html', permanent=True), | 34 Response.Redirect('/extensions/storage', permanent=True), |
| 35 self._Render('%s/extensions/storage.html' % channel)) | 35 self._Render('%s/extensions/storage' % channel)) |
| 36 | 36 |
| 37 def testNotFound(self): | 37 def testNotFound(self): |
| 38 def create_404_response(real_path): | 38 def create_404_response(real_path): |
| 39 real_404 = self._Render(real_path) | 39 real_404 = self._Render(real_path) |
| 40 self.assertEqual(200, real_404.status) | 40 self.assertEqual(200, real_404.status) |
| 41 real_404.status = 404 | 41 real_404.status = 404 |
| 42 return real_404 | 42 return real_404 |
| 43 | 43 |
| 44 root_404 = create_404_response('404.html') | 44 root_404 = create_404_response('404') |
| 45 extensions_404 = create_404_response('extensions/404.html') | 45 extensions_404 = create_404_response('extensions/404') |
| 46 apps_404 = create_404_response('apps/404.html') | 46 apps_404 = create_404_response('apps/404') |
| 47 | 47 |
| 48 self.assertEqual(root_404, self._Render('not_found.html')) | 48 self.assertEqual(root_404, self._Render('not_found')) |
| 49 self.assertEqual(root_404, self._Render('not_found/not_found.html')) | 49 self.assertEqual(root_404, self._Render('not_found/not_found')) |
| 50 | 50 |
| 51 self.assertEqual(extensions_404, self._Render('extensions/not_found.html')) | 51 self.assertEqual(extensions_404, self._Render('extensions/not_found')) |
| 52 self.assertEqual( | 52 self.assertEqual( |
| 53 extensions_404, self._Render('extensions/manifest/not_found.html')) | 53 extensions_404, self._Render('extensions/manifest/not_found')) |
| 54 self.assertEqual( | 54 self.assertEqual( |
| 55 extensions_404, | 55 extensions_404, |
| 56 self._Render('extensions/manifest/not_found/not_found.html')) | 56 self._Render('extensions/manifest/not_found/not_found')) |
| 57 | 57 |
| 58 self.assertEqual(apps_404, self._Render('apps/not_found.html')) | 58 self.assertEqual(apps_404, self._Render('apps/not_found')) |
| 59 self.assertEqual(apps_404, self._Render('apps/manifest/not_found.html')) | 59 self.assertEqual(apps_404, self._Render('apps/manifest/not_found')) |
| 60 self.assertEqual( | 60 self.assertEqual( |
| 61 apps_404, self._Render('apps/manifest/not_found/not_found.html')) | 61 apps_404, self._Render('apps/manifest/not_found/not_found')) |
| 62 | 62 |
| 63 def testSampleFile(self): | 63 def testSampleFile(self): |
| 64 sample_file = 'extensions/talking_alarm_clock/background.js' | 64 sample_file = 'extensions/talking_alarm_clock/background.js' |
| 65 response = self._Render('extensions/examples/%s' % sample_file) | 65 response = self._Render('extensions/examples/%s' % sample_file) |
| 66 self.assertEqual(200, response.status) | 66 self.assertEqual(200, response.status) |
| 67 self.assertTrue(response.headers['Content-Type'] in ( | 67 self.assertTrue(response.headers['Content-Type'] in ( |
| 68 'application/javascript; charset=utf-8', | 68 'application/javascript; charset=utf-8', |
| 69 'application/x-javascript; charset=utf-8')) | 69 'application/x-javascript; charset=utf-8')) |
| 70 self.assertEqual(ReadFile('%s%s' % (EXAMPLES, sample_file)), | 70 self.assertEqual(ReadFile('%s%s' % (EXAMPLES, sample_file)), |
| 71 response.content.ToString()) | 71 response.content.ToString()) |
| 72 | 72 |
| 73 def testSampleZip(self): | 73 def testSampleZip(self): |
| 74 sample_dir = 'extensions/talking_alarm_clock' | 74 sample_dir = 'extensions/talking_alarm_clock' |
| 75 response = self._Render('extensions/examples/%s.zip' % sample_dir) | 75 response = self._Render('extensions/examples/%s.zip' % sample_dir) |
| 76 self.assertEqual(200, response.status) | 76 self.assertEqual(200, response.status) |
| 77 self.assertEqual('application/zip', response.headers['Content-Type']) | 77 self.assertEqual('application/zip', response.headers['Content-Type']) |
| 78 | 78 |
| 79 def testStaticFile(self): | 79 def testStaticFile(self): |
| 80 static_file = 'css/out/site.css' | 80 static_file = 'css/out/site.css' |
| 81 response = self._Render('static/%s' % static_file) | 81 response = self._Render('static/%s' % static_file) |
| 82 self.assertEqual(200, response.status) | 82 self.assertEqual(200, response.status) |
| 83 self.assertEqual('text/css; charset=utf-8', | 83 self.assertEqual('text/css; charset=utf-8', |
| 84 response.headers['Content-Type']) | 84 response.headers['Content-Type']) |
| 85 self.assertEqual(ReadFile('%s%s' % (STATIC_DOCS, static_file)), | 85 self.assertEqual(ReadFile('%s%s' % (STATIC_DOCS, static_file)), |
| 86 response.content.ToString()) | 86 response.content.ToString()) |
| 87 | 87 |
| 88 def testHtmlTemplate(self): | 88 def testHtmlTemplate(self): |
| 89 html_file = 'extensions/storage.html' | 89 html_file = 'extensions/storage' |
| 90 response = self._Render(html_file) | 90 response = self._Render(html_file) |
| 91 self.assertEqual(200, response.status) | 91 self.assertEqual(200, response.status) |
| 92 self.assertEqual('text/html; charset=utf-8', | 92 self.assertEqual('text/html; charset=utf-8', |
| 93 response.headers.get('Content-Type')) | 93 response.headers.get('Content-Type')) |
| 94 # Can't really test rendering all that well. | 94 # Can't really test rendering all that well. |
| 95 self.assertTrue(len(response.content) > | 95 self.assertTrue(len(response.content) > |
| 96 len(ReadFile('%s%s' % (PUBLIC_TEMPLATES, html_file)))) | 96 len(ReadFile('%s%s.html' % (PUBLIC_TEMPLATES, html_file)))) |
| 97 | 97 |
| 98 def testDevelopersGoogleComRedirect(self): | 98 def testDevelopersGoogleComRedirect(self): |
| 99 def assert_redirect(request_path): | 99 def assert_redirect(request_path): |
| 100 response = self._Render(request_path) | 100 response = self._Render(request_path) |
| 101 self.assertEqual(('//developers.google.com/chrome', False), | 101 self.assertEqual(('//developers.google.com/chrome', False), |
| 102 response.GetRedirect()) | 102 response.GetRedirect()) |
| 103 assert_redirect('') | 103 assert_redirect('') |
| 104 assert_redirect('index.html') | 104 assert_redirect('index') |
| 105 | 105 |
| 106 def testIndexRedirect(self): | 106 def testIndexRedirect(self): |
| 107 response = self._Render('extensions') | 107 response = self._Render('extensions') |
| 108 self.assertEqual(('/extensions/index.html', False), | 108 self.assertEqual(('/extensions/index', False), |
| 109 response.GetRedirect()) | 109 response.GetRedirect()) |
| 110 | 110 |
| 111 def testOtherRedirectsJsonRedirect(self): | 111 def testOtherRedirectsJsonRedirect(self): |
| 112 response = self._Render('apps/webview_tag.html') | 112 response = self._Render('apps/webview_tag') |
| 113 self.assertEqual(('/apps/tags/webview.html', False), | 113 self.assertEqual(('/apps/tags/webview', False), |
| 114 response.GetRedirect()) | 114 response.GetRedirect()) |
| 115 | 115 |
| 116 | 116 |
| 117 if __name__ == '__main__': | 117 if __name__ == '__main__': |
| 118 unittest.main() | 118 unittest.main() |
| OLD | NEW |