def _Render(base_dir, path): renderer = LocalRenderer(base_dir) response = renderer.Render(path) while response.status in [301, 302]: redirect = response.headers['Location'].lstrip('/') sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) response = renderer.Render(redirect) return response
def _Render(base_dir, path): renderer = LocalRenderer(base_dir) content, status, headers = renderer.Render(path, always_online=True) while status in [301, 302]: redirect = headers['Location'].lstrip('/') sys.stderr.write('<!-- Redirected %s to %s -->\n' % (path, redirect)) content, status, headers = renderer.Render(redirect, always_online=True) return (content, status, headers)
def do_GET(self): response = LocalRenderer.Render(self.path) self.send_response(response.status) for k, v in response.headers.iteritems(): self.send_header(k, v) self.end_headers() self.wfile.write(response.content.ToString())
def do_GET(self): # Rewrite paths that would otherwise be served from app.yaml. self.path = { '/robots.txt': '../../server2/robots.txt', '/favicon.ico': '../../server2/chrome-32.ico', '/apple-touch-icon-precomposed.png': '../../server2/chrome-128.png' }.get(self.path, self.path) response = LocalRenderer.Render(self.path) self.send_response(response.status) for k, v in response.headers.iteritems(): self.send_header(k, v) self.end_headers() self.wfile.write(response.content.ToString())
def testExplicitFiles(self): '''Tests just the files in _EXPLICIT_TEST_FILES. ''' if _EXPLICIT_TEST_FILES is None: return for filename in _EXPLICIT_TEST_FILES: print('Rendering %s...' % filename) start_time = time.time() try: response = LocalRenderer.Render(_ToPosixPath(filename)) self.assertEqual(200, response.status) self.assertTrue(response.content != '') finally: print('Took %s seconds' % (time.time() - start_time))
def do_GET(self): # Sanitize path to guarantee that it stays within the server. if not posixpath.abspath(self.path.lstrip('/')).startswith( posixpath.abspath('')): return # Rewrite paths that would otherwise be served from app.yaml. self.path = { '/robots.txt': '../../server2/robots.txt', '/favicon.ico': '../../server2/chrome-32.ico', '/apple-touch-icon-precomposed.png': '../../server2/chrome-128.png' }.get(self.path, self.path) response = LocalRenderer.Render(self.path, headers=dict(self.headers)) self.protocol_version = 'HTTP/1.1' self.send_response(response.status) for k, v in response.headers.iteritems(): self.send_header(k, v) self.end_headers() self.wfile.write(response.content.ToString())
if opts.render: if opts.render.find('#') >= 0: (path, iterations) = opts.render.rsplit('#', 1) extra_iterations = int(iterations) - 1 else: path = opts.render extra_iterations = 0 if opts.stat: import cProfile, pstats, StringIO pr = cProfile.Profile() pr.enable() elif opts.time: start_time = time.time() response = LocalRenderer.Render(path) if response.status != 200: print('Error status: %s' % response.status) exit(1) for _ in range(extra_iterations): LocalRenderer.Render(path) if opts.stat: pr.disable() s = StringIO.StringIO() pstats.Stats(pr, stream=s).sort_stats(opts.stat).print_stats() print(s.getvalue()) elif opts.time: print('Took %s seconds' % (time.time() - start_time)) else:
def setUp(self): self._renderer = LocalRenderer(_BASE_PATH)
class IntegrationTest(unittest.TestCase): def setUp(self): self._renderer = LocalRenderer(_BASE_PATH) def testCronAndPublicFiles(self): '''Runs cron then requests every public file. Cron needs to be run first because the public file requests are offline. ''' if _EXPLICIT_TEST_FILES is not None: return print('Running cron...') start_time = time.time() try: render_content, render_status, _ = self._renderer.Render( '/cron/stable') self.assertEqual(200, render_status) self.assertEqual('Success', render_content) finally: print('Took %s seconds' % (time.time() - start_time)) public_files = _GetPublicFiles() print('Rendering %s public files...' % len(public_files.keys())) start_time = time.time() try: for path, content in _GetPublicFiles().iteritems(): def check_result(render_content, render_status, _): self.assertEqual( 200, render_status, 'Got %s when rendering %s' % (render_status, path)) # This is reaaaaally rough since usually these will be tiny templates # that render large files. At least it'll catch zero-length responses. self.assertTrue( len(render_content) >= len(content), 'Content was "%s" when rendering %s' % (render_content, path)) check_result(*self._renderer.Render(path)) # Samples are internationalized, test some locales. if path.endswith('/samples.html'): for lang in ['en-US', 'es', 'ar']: check_result(*self._renderer.Render( path, headers={'Accept-Language': '%s;q=0.8' % lang})) finally: print('Took %s seconds' % (time.time() - start_time)) def testExplicitFiles(self): '''Tests just the files in _EXPLICIT_TEST_FILES. ''' if _EXPLICIT_TEST_FILES is None: return for filename in _EXPLICIT_TEST_FILES: print('Rendering %s...' % filename) start_time = time.time() try: render_content, render_status, _ = self._renderer.Render( filename, always_online=True) self.assertEqual(200, render_status) self.assertTrue(render_content != '') finally: print('Took %s seconds' % (time.time() - start_time)) @DisableLogging('warning') def testFileNotFound(self): render_content, render_status, _ = self._renderer.Render( '/extensions/notfound.html', always_online=True) self.assertEqual(404, render_status)
def testSiteVerificationFile(self): response = LocalRenderer.Render('/' + SITE_VERIFICATION_FILE) self.assertEqual(200, response.status)
def testFileNotFound(self): response = LocalRenderer.Render('/extensions/notfound') self.assertEqual(404, response.status)