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
, headers
=None):
23 return RenderServlet(Request
.ForTest(path
, headers
=headers
),
24 _RenderServletDelegate()).Get()
26 def testExtensionAppRedirect(self
):
28 Response
.Redirect('/apps/storage', permanent
=False),
29 self
._Render
('storage'))
31 def testChannelRedirect(self
):
32 for channel
in ('stable', 'beta', 'dev', 'trunk'):
34 Response
.Redirect('/extensions/storage', permanent
=True),
35 self
._Render
('%s/extensions/storage' % channel
))
37 def testNotFound(self
):
38 def create_404_response(real_path
):
39 real_404
= self
._Render
(real_path
)
40 self
.assertEqual(200, real_404
.status
)
44 root_404
= create_404_response('404')
45 extensions_404
= create_404_response('extensions/404')
46 apps_404
= create_404_response('apps/404')
48 self
.assertEqual(root_404
, self
._Render
('not_found'))
49 self
.assertEqual(root_404
, self
._Render
('not_found/not_found'))
51 self
.assertEqual(extensions_404
, self
._Render
('extensions/not_found'))
53 extensions_404
, self
._Render
('extensions/manifest/not_found'))
56 self
._Render
('extensions/manifest/not_found/not_found'))
58 self
.assertEqual(apps_404
, self
._Render
('apps/not_found'))
59 self
.assertEqual(apps_404
, self
._Render
('apps/manifest/not_found'))
61 apps_404
, self
._Render
('apps/manifest/not_found/not_found'))
63 def testSampleFile(self
):
64 sample_file
= 'extensions/talking_alarm_clock/background.js'
65 response
= self
._Render
('extensions/examples/%s' % sample_file
)
66 self
.assertEqual(200, response
.status
)
67 self
.assertTrue(response
.headers
['Content-Type'] in (
68 'application/javascript; charset=utf-8',
69 'application/x-javascript; charset=utf-8'))
70 self
.assertEqual(ReadFile('%s%s' % (EXAMPLES
, sample_file
)),
71 response
.content
.ToString())
73 def testSampleZip(self
):
74 sample_dir
= 'extensions/talking_alarm_clock'
75 response
= self
._Render
('extensions/examples/%s.zip' % sample_dir
)
76 self
.assertEqual(200, response
.status
)
77 self
.assertEqual('application/zip', response
.headers
['Content-Type'])
79 def testStaticFile(self
):
80 static_file
= 'css/out/site.css'
81 response
= self
._Render
('static/%s' % static_file
)
82 self
.assertEqual(200, response
.status
)
83 self
.assertEqual('text/css; charset=utf-8',
84 response
.headers
['Content-Type'])
85 self
.assertEqual(ReadFile('%s%s' % (STATIC_DOCS
, static_file
)),
86 response
.content
.ToString())
88 def testHtmlTemplate(self
):
89 html_file
= 'extensions/storage'
90 response
= self
._Render
(html_file
)
91 self
.assertEqual(200, response
.status
)
92 self
.assertEqual('text/html; charset=utf-8',
93 response
.headers
.get('Content-Type'))
94 # Can't really test rendering all that well.
95 self
.assertTrue(len(response
.content
) >
96 len(ReadFile('%s%s.html' % (PUBLIC_TEMPLATES
, html_file
))))
98 def testIndexRender(self
):
99 response
= self
._Render
('extensions')
100 self
.assertEqual(200, response
.status
)
101 self
.assertEqual(self
._Render
('extensions/index').content
.ToString(),
102 response
.content
.ToString())
104 def testOtherRedirectsJsonRedirect(self
):
105 response
= self
._Render
('apps/webview_tag')
106 self
.assertEqual(('/apps/tags/webview', False),
107 response
.GetRedirect())
109 def testDirectories(self
):
110 # Directories should be redirected to a URL that doesn't end in a '/'
111 # whether or not that exists.
112 self
.assertEqual(('/dir', False), self
._Render
('dir/').GetRedirect())
115 def test_path(path
, content_type
):
116 # Render without etag.
117 response
= self
._Render
(path
)
118 self
.assertEqual(200, response
.status
)
119 etag
= response
.headers
.get('ETag')
120 self
.assertTrue(etag
is not None)
122 # Render with an If-None-Match which doesn't match.
123 response
= self
._Render
(path
, headers
={
124 'If-None-Match': '"fake etag"',
126 self
.assertEqual(200, response
.status
)
127 self
.assertEqual(content_type
, response
.headers
.get('Content-Type'))
128 self
.assertEqual(etag
, response
.headers
.get('ETag'))
130 # Render with the correct matching If-None-Match.
131 response
= self
._Render
(path
, headers
={
132 'If-None-Match': etag
,
134 self
.assertEqual(304, response
.status
)
135 self
.assertEqual('Not Modified', response
.content
.ToString())
136 self
.assertEqual(content_type
, response
.headers
.get('Content-Type'))
137 self
.assertEqual(etag
, response
.headers
.get('ETag'))
139 # Test with a static path and a dynamic path.
140 test_path('static/css/out/site.css', 'text/css; charset=utf-8')
141 test_path('extensions/storage', 'text/html; charset=utf-8')
144 if __name__
== '__main__':