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 extensions_paths
import EXAMPLES
, PUBLIC_TEMPLATES
, STATIC_DOCS
9 from local_file_system
import LocalFileSystem
10 from render_servlet
import RenderServlet
11 from server_instance
import ServerInstance
12 from servlet
import Request
, Response
13 from test_util
import ReadFile
16 class _RenderServletDelegate(RenderServlet
.Delegate
):
17 def CreateServerInstance(self
):
18 return ServerInstance
.ForTest(LocalFileSystem
.Create())
21 class RenderServletTest(unittest
.TestCase
):
22 def _Render(self
, path
):
23 return RenderServlet(Request
.ForTest(path
),
24 _RenderServletDelegate()).Get()
26 def testExtensionAppRedirect(self
):
28 Response
.Redirect('/extensions/storage.html', permanent
=False),
29 self
._Render
('storage.html'))
31 def testChannelRedirect(self
):
33 Response
.Redirect('/extensions/storage.html', permanent
=True),
34 self
._Render
('stable/extensions/storage.html'))
36 def testNotFound(self
):
37 def create_404_response(real_path
):
38 real_404
= self
._Render
(real_path
)
39 self
.assertEqual(200, real_404
.status
)
43 root_404
= create_404_response('404.html')
44 extensions_404
= create_404_response('extensions/404.html')
45 apps_404
= create_404_response('apps/404.html')
47 self
.assertEqual(root_404
, self
._Render
('not_found.html'))
48 self
.assertEqual(root_404
, self
._Render
('not_found/not_found.html'))
50 self
.assertEqual(extensions_404
, self
._Render
('extensions/not_found.html'))
52 extensions_404
, self
._Render
('extensions/manifest/not_found.html'))
55 self
._Render
('extensions/manifest/not_found/not_found.html'))
57 self
.assertEqual(apps_404
, self
._Render
('apps/not_found.html'))
58 self
.assertEqual(apps_404
, self
._Render
('apps/manifest/not_found.html'))
60 apps_404
, self
._Render
('apps/manifest/not_found/not_found.html'))
62 def testSampleFile(self
):
63 sample_file
= 'extensions/talking_alarm_clock/background.js'
64 response
= self
._Render
('extensions/examples/%s' % sample_file
)
65 self
.assertEqual(200, response
.status
)
66 self
.assertTrue(response
.headers
['Content-Type'] in (
67 'application/javascript; charset=utf-8',
68 'application/x-javascript; charset=utf-8'))
69 self
.assertEqual(ReadFile('%s/%s' % (EXAMPLES
, sample_file
)),
70 response
.content
.ToString())
72 def testSampleZip(self
):
73 sample_dir
= 'extensions/talking_alarm_clock'
74 response
= self
._Render
('extensions/examples/%s.zip' % sample_dir
)
75 self
.assertEqual(200, response
.status
)
76 self
.assertEqual('application/zip', response
.headers
['Content-Type'])
78 def testStaticFile(self
):
79 static_file
= 'css/out/site.css'
80 response
= self
._Render
('static/%s' % static_file
)
81 self
.assertEqual(200, response
.status
)
82 self
.assertEqual('text/css; charset=utf-8',
83 response
.headers
['Content-Type'])
84 self
.assertEqual(ReadFile('%s/%s' % (STATIC_DOCS
, static_file
)),
85 response
.content
.ToString())
87 def testHtmlTemplate(self
):
88 html_file
= 'extensions/storage.html'
89 response
= self
._Render
(html_file
)
90 self
.assertEqual(200, response
.status
)
91 self
.assertEqual('text/html; charset=utf-8',
92 response
.headers
.get('Content-Type'))
93 # Can't really test rendering all that well.
94 self
.assertTrue(len(response
.content
) >
95 len(ReadFile('%s/%s' % (PUBLIC_TEMPLATES
, 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__':