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.
8 from local_file_system import LocalFileSystem
9 from render_servlet import RenderServlet
10 from server_instance import ServerInstance
11 from servlet import Request, Response
12 from test_util import DisableLogging, ReadFile
15 class _RenderServletDelegate(RenderServlet.Delegate):
16 def CreateServerInstance(self):
17 return ServerInstance.ForTest(LocalFileSystem.Create())
20 class RenderServletTest(unittest.TestCase):
21 def _Render(self, path):
22 return RenderServlet(Request.ForTest(path),
23 _RenderServletDelegate()).Get()
25 def testExtensionAppRedirect(self):
27 Response.Redirect('/extensions/storage.html', permanent=False),
28 self._Render('storage.html'))
30 def testChannelRedirect(self):
32 Response.Redirect('/extensions/storage.html', permanent=True),
33 self._Render('stable/extensions/storage.html'))
35 def testNotFound(self):
36 def create_404_response(real_path):
37 real_404 = self._Render(real_path)
38 self.assertEqual(200, real_404.status)
42 root_404 = create_404_response('404.html')
43 extensions_404 = create_404_response('extensions/404.html')
44 apps_404 = create_404_response('apps/404.html')
45 # Note: would test that root_404 != extensions and apps but it's not
47 self.assertNotEqual(extensions_404, apps_404)
49 self.assertEqual(root_404, self._Render('not_found.html'))
50 self.assertEqual(root_404, self._Render('not_found/not_found.html'))
52 self.assertEqual(extensions_404, self._Render('extensions/not_found.html'))
54 extensions_404, self._Render('extensions/manifest/not_found.html'))
57 self._Render('extensions/manifest/not_found/not_found.html'))
59 self.assertEqual(apps_404, self._Render('apps/not_found.html'))
60 self.assertEqual(apps_404, self._Render('apps/manifest/not_found.html'))
62 apps_404, self._Render('apps/manifest/not_found/not_found.html'))
64 def testSampleFile(self):
65 sample_file = 'extensions/talking_alarm_clock/background.js'
66 response = self._Render('extensions/examples/%s' % sample_file)
67 self.assertEqual(200, response.status)
68 content_type = response.headers.get('content-type')
69 self.assertTrue(content_type == 'application/javascript' or
70 content_type == 'application/x-javascript')
71 self.assertEqual(ReadFile('docs/examples/%s' % sample_file),
72 response.content.ToString())
74 def testSampleZip(self):
75 sample_dir = 'extensions/talking_alarm_clock'
76 response = self._Render('extensions/examples/%s.zip' % sample_dir)
77 self.assertEqual(200, response.status)
78 self.assertEqual('application/zip', response.headers.get('content-type'))
80 def testStaticFile(self):
81 static_file = 'css/site.css'
82 response = self._Render('static/%s' % static_file)
83 self.assertEqual(200, response.status)
84 self.assertEqual('text/css', response.headers.get('content-type'))
85 self.assertEqual(ReadFile('docs/static/%s' % static_file),
86 response.content.ToString())
88 def testHtmlTemplate(self):
89 html_file = 'extensions/storage.html'
90 response = self._Render(html_file)
91 self.assertEqual(200, response.status)
92 self.assertEqual('text/html', response.headers.get('content-type'))
93 # Can't really test rendering all that well.
94 self.assertTrue(len(response.content) >
95 len(ReadFile('docs/templates/public/%s' % html_file)))
97 def testDevelopersGoogleComRedirect(self):
98 def assert_redirect(request_path):
99 response = self._Render(request_path)
100 self.assertEqual(('//developers.google.com/chrome', False),
101 response.GetRedirect())
103 assert_redirect('index.html')
105 def testIndexRedirect(self):
106 response = self._Render('extensions')
107 self.assertEqual(('/extensions/index.html', False),
108 response.GetRedirect())
110 def testOtherRedirectsJsonRedirect(self):
111 response = self._Render('apps/webview_tag.html')
112 self.assertEqual(('/apps/tags/webview.html', False),
113 response.GetRedirect())
116 if __name__ == '__main__':