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, host
=None):
23 return RenderServlet(Request
.ForTest(path
, headers
=headers
, host
=host
),
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', 'master'):
34 Response
.Redirect('/extensions/storage', permanent
=True),
35 self
._Render
('%s/extensions/storage' % channel
))
37 def testOldHostsRedirect(self
):
39 Response
.Redirect('https://developer.chrome.com/extensions',
41 self
._Render
('/chrome/extensions', host
='code.google.com'))
43 Response
.Redirect('https://developer.chrome.com/extensions',
45 self
._Render
('/chrome/extensions', host
='code.google.com'))
47 Response
.Redirect('https://developer.chrome.com/devtools/docs/network',
49 self
._Render
('/chrome/devtools/docs/network', host
='code.google.com'))
51 def testNotFound(self
):
52 def create_404_response(real_path
):
53 real_404
= self
._Render
(real_path
)
54 self
.assertEqual(200, real_404
.status
)
58 root_404
= create_404_response('404')
59 extensions_404
= create_404_response('extensions/404')
60 apps_404
= create_404_response('apps/404')
62 self
.assertEqual(root_404
, self
._Render
('not_found'))
63 self
.assertEqual(root_404
, self
._Render
('not_found/not_found'))
65 self
.assertEqual(extensions_404
, self
._Render
('extensions/not_found'))
67 extensions_404
, self
._Render
('extensions/manifest/not_found'))
70 self
._Render
('extensions/manifest/not_found/not_found'))
72 self
.assertEqual(apps_404
, self
._Render
('apps/not_found'))
73 self
.assertEqual(apps_404
, self
._Render
('apps/manifest/not_found'))
75 apps_404
, self
._Render
('apps/manifest/not_found/not_found'))
77 def testSampleFile(self
):
78 sample_file
= 'extensions/talking_alarm_clock/background.js'
79 response
= self
._Render
('extensions/examples/%s' % sample_file
)
80 self
.assertEqual(200, response
.status
)
81 self
.assertTrue(response
.headers
['Content-Type'] in (
82 'application/javascript; charset=utf-8',
83 'application/x-javascript; charset=utf-8'))
84 self
.assertEqual(ReadFile('%s%s' % (EXAMPLES
, sample_file
)),
85 response
.content
.ToString())
87 def testSampleZip(self
):
88 sample_dir
= 'extensions/talking_alarm_clock'
89 response
= self
._Render
('extensions/examples/%s.zip' % sample_dir
)
90 self
.assertEqual(200, response
.status
)
91 self
.assertEqual('application/zip', response
.headers
['Content-Type'])
93 def testStaticFile(self
):
94 static_file
= 'css/out/site.css'
95 response
= self
._Render
('static/%s' % static_file
)
96 self
.assertEqual(200, response
.status
)
97 self
.assertEqual('text/css; charset=utf-8',
98 response
.headers
['Content-Type'])
99 self
.assertEqual(ReadFile('%s%s' % (STATIC_DOCS
, static_file
)),
100 response
.content
.ToString())
102 def testHtmlTemplate(self
):
103 html_file
= 'extensions/storage'
104 response
= self
._Render
(html_file
)
105 self
.assertEqual(200, response
.status
)
106 self
.assertEqual('text/html; charset=utf-8',
107 response
.headers
.get('Content-Type'))
108 # Can't really test rendering all that well.
109 self
.assertTrue(len(response
.content
) >
110 len(ReadFile('%s%s.html' % (PUBLIC_TEMPLATES
, html_file
))))
112 def testIndexRender(self
):
113 self
.assertEqual(200, self
._Render
('extensions').status
)
114 self
.assertEqual(('/extensions', False),
115 self
._Render
('extensions/index').GetRedirect())
117 def testOtherRedirectsJsonRedirect(self
):
118 response
= self
._Render
('apps/webview_tag')
119 self
.assertEqual(('/apps/tags/webview', False),
120 response
.GetRedirect())
122 def testDirectories(self
):
123 # Directories should be redirected to a URL that doesn't end in a '/'
124 # whether or not that exists.
125 self
.assertEqual(('/dir', False), self
._Render
('dir/').GetRedirect())
128 def test_path(path
, content_type
):
129 # Render without etag.
130 response
= self
._Render
(path
)
131 self
.assertEqual(200, response
.status
)
132 etag
= response
.headers
.get('ETag')
133 self
.assertTrue(etag
is not None)
135 # Render with an If-None-Match which doesn't match.
136 response
= self
._Render
(path
, headers
={
137 'If-None-Match': '"fake etag"',
139 self
.assertEqual(200, response
.status
)
140 self
.assertEqual(content_type
, response
.headers
.get('Content-Type'))
141 self
.assertEqual(etag
, response
.headers
.get('ETag'))
143 # Render with the correct matching If-None-Match.
144 response
= self
._Render
(path
, headers
={
145 'If-None-Match': etag
,
147 self
.assertEqual(304, response
.status
)
148 self
.assertEqual('Not Modified', response
.content
.ToString())
149 self
.assertEqual(content_type
, response
.headers
.get('Content-Type'))
150 self
.assertEqual(etag
, response
.headers
.get('ETag'))
152 # Test with a static path and a dynamic path.
153 test_path('static/css/out/site.css', 'text/css; charset=utf-8')
154 test_path('extensions/storage', 'text/html; charset=utf-8')
157 if __name__
== '__main__':