def testEverything(self): # All these tests are dependent (see above comment) so lump everything in # the one test. delegate = _TestDelegate( lambda _: MockFileSystem(LocalFileSystem.Create())) # Test that the cron runs successfully. response = CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() self.assertEqual(200, response.status) # Save the file systems created, start with a fresh set for the next run. first_run_file_systems = delegate.file_systems[:] delegate.file_systems[:] = [] # When re-running, all file systems should be Stat()d the same number of # times, but the second round shouldn't have been re-Read() since the # Stats haven't changed. response = CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() self.assertEqual(200, response.status) self.assertEqual(len(first_run_file_systems), len(delegate.file_systems)) for i, second_run_file_system in enumerate(delegate.file_systems): self.assertTrue(*second_run_file_system.CheckAndReset( read_count=0, stat_count=first_run_file_systems[i].GetStatCount()))
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: response = Handler(Request.ForTest('/_cron/stable')).Get() self.assertEqual(200, response.status) self.assertEqual('Success', response.content.ToString()) 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 public_files.iteritems(): if path.endswith('redirects.json'): continue def check_result(response): self.assertEqual( 200, response.status, 'Got %s when rendering %s' % (response.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(response.content) >= len(content), 'Content was "%s" when rendering %s' % (response.content, path)) check_result(Handler(Request.ForTest(path)).Get()) # Samples are internationalized, test some locales. if path.endswith('/samples.html'): for lang in ['en-US', 'es', 'ar']: check_result( Handler( Request.ForTest(path, headers={ 'Accept-Language': '%s;q=0.8' % lang })).Get()) finally: print('Took %s seconds' % (time.time() - start_time))
def Get(self): path = self._request.path if path.startswith('_'): servlet_path = path[1:] if not '/' in servlet_path: servlet_path += '/' servlet_name, servlet_path = servlet_path.split('/', 1) if servlet_name == _FORCE_CRON_TARGET: queue = taskqueue.Queue() queue.purge() time.sleep(2) queue.add(taskqueue.Task(url='/_cron')) return Response.Ok('Cron job started.') if servlet_name == 'enqueue': queue = taskqueue.Queue() queue.add(taskqueue.Task(url='/%s' % servlet_path)) return Response.Ok('Task enqueued.') servlet = _SERVLETS.get(servlet_name) if servlet is None: return Response.NotFound('"%s" servlet not found' % servlet_path) else: servlet_path = path servlet = _DEFAULT_SERVLET return servlet( Request(servlet_path, self._request.host, self._request.headers, self._request.arguments)).Get()
def get(self): profile_mode = self.request.get('profile') if profile_mode: import cProfile, pstats, StringIO pr = cProfile.Profile() pr.enable() try: request = Request(self.request.path, self.request.url[:-len(self.request.path)], self.request.headers) response = Handler(request).Get() finally: if profile_mode: pr.disable() s = StringIO.StringIO() pstats.Stats(pr, stream=s).sort_stats(profile_mode).print_stats() self.response.out.write(s.getvalue()) self.response.headers['Content-Type'] = 'text/plain' self.response.status = 200 else: self.response.out.write(response.content.ToString()) self.response.headers.update(response.headers) self.response.status = response.status
def testCodeGoogleRedirect(self): response = Handler( Request('chrome/extensions/storage.html', 'http://code.google.com', {})).Get() self.assertEqual(302, response.status) self.assertEqual('http://developer.chrome.com/extensions/storage.html', response.headers.get('Location'))
def Get(self): if (not IsDevServer() and not fnmatch( urlparse(self._request.host).netloc, '*.appspot.com')): # Only allow patches on appspot URLs; it doesn't matter if appspot.com is # XSS'ed, but it matters for chrome.com. redirect_host = 'https://chrome-apps-doc.appspot.com' logging.info('Redirecting from XSS-able host %s to %s' % (self._request.host, redirect_host)) return Response.Redirect('%s/_patch/%s' % (redirect_host, self._request.path)) path_with_issue = self._request.path.lstrip('/') if '/' in path_with_issue: issue, path_without_issue = path_with_issue.split('/', 1) else: return Response.NotFound( 'Malformed URL. It should look like ' + 'https://developer.chrome.com/_patch/12345/extensions/...') try: response = RenderServlet( Request(path_without_issue, self._request.host, self._request.headers), _PatchServletDelegate(issue, self._delegate)).Get() # Disable cache for patched content. response.headers.pop('cache-control', None) except RietveldPatcherError as e: response = Response.NotFound(e.message, {'Content-Type': 'text/plain'}) redirect_url, permanent = response.GetRedirect() if redirect_url is not None: response = Response.Redirect( '/_patch/%s%s' % (issue, redirect_url), permanent) return response
def get(self): path, request, response = (self.request.path.lstrip('/'), self.request, self.response) if path in ['favicon.ico', 'robots.txt']: response.set_status(404) return if self._RedirectSpecialCases(path): return if self._RedirectFromCodeDotGoogleDotCom(path): return if path.startswith('_'): servlet_path = path[1:] if servlet_path.find('/') == -1: servlet_path += '/' servlet_name, servlet_path = servlet_path.split('/', 1) servlet = _SERVLETS.get(servlet_name) if servlet is None: response.out.write('"%s" servlet not found' % servlet_path) response.set_status(404) return else: servlet_path = path servlet = RenderServlet servlet_response = servlet(Request(servlet_path, request.headers)).Get() response.out.write(servlet_response.content.ToString()) response.headers.update(servlet_response.headers) response.status = servlet_response.status
def testSampleZip(self): sample_dir = 'extensions/talking_alarm_clock' request = Request.ForTest('extensions/examples/%s.zip' % sample_dir) response = RenderServlet(request, _RenderServletDelegate()).Get() self.assertEqual(200, response.status) self.assertEqual('application/zip', response.headers.get('content-type'))
def setUp(self): server_instance = ServerInstance.ForTest( file_system=TestFileSystem(_TEST_FS)) # Don't randomize the owners to avoid testing issues. self._owners_ds = OwnersDataSource(server_instance, Request.ForTest('/'), randomize=False)
def Get(self): path = self._request.path redirect = self._RedirectSpecialCases() if redirect is None: redirect = self._RedirectFromCodeDotGoogleDotCom() if redirect is not None: return redirect if path.startswith('_'): servlet_path = path[1:] if servlet_path.find('/') == -1: servlet_path += '/' servlet_name, servlet_path = servlet_path.split('/', 1) servlet = _SERVLETS.get(servlet_name) if servlet is None: return Response.NotFound('"%s" servlet not found' % servlet_path) else: servlet_path = path servlet = _DEFAULT_SERVLET return servlet( Request(servlet_path, self._request.host, self._request.headers)).Get()
def testWithDifferentBasePath(self): file_system = TestFileSystem({ 'chrome_sidenav.json': json.dumps([ { 'href': '/H1.html' }, { 'href': '/H2.html' }, { 'href': '/base/path/H2.html' }, { 'href': 'https://qualified/X1.html' }, { 'href': 'H3.html', 'items': [{ 'href': 'H4.html' }] }, ]) }, relative_to=JSON_TEMPLATES) expected = [ {'href': '/base/path/H1.html', 'level': 2, 'related': True}, {'href': '/base/path/H2.html', 'level': 2, 'selected': True, 'related': True}, {'href': '/base/path/base/path/H2.html', 'level': 2, 'related': True}, {'href': 'https://qualified/X1.html', 'level': 2, 'related': True}, {'items': [ {'href': '/base/path/H4.html', 'level': 3} ], 'href': '/base/path/H3.html', 'level': 2, 'related': True} ] server_instance = ServerInstance.ForTest(file_system, base_path='/base/path/') sidenav_data_source = SidenavDataSource(server_instance, Request.ForTest('/H2.html')) log_output = CaptureLogging( lambda: self.assertEqual(expected, sidenav_data_source.get('chrome'))) self.assertEqual(2, len(log_output))
def _HandleRequest(self): profile_mode = self.request.get('profile') if profile_mode: import cProfile, pstats, StringIO pr = cProfile.Profile() pr.enable() try: response = None arguments = {} for argument in self.request.arguments(): arguments[argument] = self.request.get(argument) request = Request(self.request.path, self.request.url[:-len(self.request.path)], self.request.headers, arguments) response = Handler(request).Get() except Exception as e: logging.exception(e) finally: if profile_mode: pr.disable() s = StringIO.StringIO() pstats.Stats(pr, stream=s).sort_stats(profile_mode).print_stats() self.response.out.write(s.getvalue()) self.response.headers['Content-Type'] = 'text/plain' self.response.status = 200 elif response: self.response.out.write(response.content.ToString()) self.response.headers.update(response.headers) self.response.status = response.status else: self.response.out.write('Internal server error') self.response.status = 500
def get(self): request = Request(self.request.path, self.request.url[:-len(self.request.path)], self.request.headers) response = Handler(request).Get() self.response.out.write(response.content.ToString()) self.response.headers.update(response.headers) self.response.status = response.status
def Render(self, path, headers=None, servlet=RenderServlet): '''Renders |path|, returning a tuple of (status, contents, headers). ''' headers = headers or {} # TODO(kalman): do this via a LocalFileSystem not this fake AppEngine stuff. ConfigureFakeFetchers(os.path.join(self._base_dir, 'docs')) url_path = urlparse.urlparse(path.replace(os.sep, '/')).path return servlet(Request(url_path, headers)).Get()
def testStaticFile(self): static_file = 'css/site.css' request = Request.ForTest('static/%s' % static_file) response = RenderServlet(request, _RenderServletDelegate()).Get() self.assertEqual(200, response.status) self.assertEqual('text/css', response.headers.get('content-type')) self.assertEqual(ReadFile('docs/static/%s' % static_file), response.content.ToString())
def testGetAPINodeAvailability(self): def assertEquals(node, actual): node_availabilities = { 'tabs.Tab': None, 'tabs.fakeTabsProperty1': None, 'tabs.get': None, 'tabs.onUpdated': None, 'tabs.InjectDetails': 25, 'tabs.fakeTabsProperty2': 15, 'tabs.getCurrent': 19, 'tabs.onActivated': 30 } self.assertEquals(node_availabilities[node], actual) model_dict = CreateJSCView( self._api_models.GetContentScriptAPIs().Get(), self._api_models.GetModel('tabs').Get(), self._avail_finder, self._json_cache, _FakeTemplateCache(), _FakeFeaturesBundle(), None, 'extensions', [], Request.ForTest('')) # Test nodes that have the same availability as their parent. # Test type. assertEquals('tabs.Tab', model_dict['types'][0]['availability']) # Test property. assertEquals('tabs.fakeTabsProperty1', model_dict['properties'][1]['availability']) # Test function. assertEquals('tabs.get', model_dict['functions'][1]['availability']) # Test event. assertEquals('tabs.onUpdated', model_dict['events'][1]['availability']) # Test nodes with varying availabilities. # Test type. assertEquals('tabs.InjectDetails', model_dict['types'][1]['availability']['version']) # Test property. assertEquals('tabs.fakeTabsProperty2', model_dict['properties'][3]['availability']['version']) # Test function. assertEquals('tabs.getCurrent', model_dict['functions'][0]['availability']['version']) # Test event. assertEquals('tabs.onActivated', model_dict['events'][0]['availability']['version']) # Test a node that became deprecated. self.assertEquals( { 'scheduled': None, 'version': 26, 'partial': 'motemplate chrome/common/extensions/docs/templates/' + 'private/intro_tables/deprecated_message.html' }, model_dict['types'][2]['availability'])
def DISABLED_testToDict(self): fake_avail_finder = _FakeAvailabilityFinder(self._fake_availability) expected_json = self._LoadJSON('expected_tester.json') dict_ = CreateJSCView(self._api_models.GetContentScriptAPIs().Get(), self._api_models.GetModel('tester').Get(), fake_avail_finder, self._json_cache, _FakeTemplateCache(), self._features_bundle, None, 'extensions', [], Request.ForTest('')) self.assertEquals(expected_json, dict_)
def testHtmlTemplate(self): html_file = 'extensions/storage.html' request = Request.ForTest(html_file) response = RenderServlet(request, _RenderServletDelegate()).Get() self.assertEqual(200, response.status) self.assertEqual('text/html', response.headers.get('content-type')) # Can't really test rendering all that well. self.assertTrue( len(response.content) > len( ReadFile('docs/templates/public/%s' % html_file)))
def testSampleFile(self): sample_file = 'extensions/talking_alarm_clock/background.js' request = Request.ForTest('extensions/examples/%s' % sample_file) response = RenderServlet(request, _RenderServletDelegate()).Get() self.assertEqual(200, response.status) content_type = response.headers.get('content-type') self.assertTrue(content_type == 'application/javascript' or content_type == 'application/x-javascript') self.assertEqual(ReadFile('docs/examples/%s' % sample_file), response.content.ToString())
def testEverything(self): # All these tests are dependent (see above comment) so lump everything in # the one test. delegate = _TestDelegate( lambda _: MockFileSystem(LocalFileSystem.Create())) # Test that the cron runs successfully. response = CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() self.assertEqual(1, len(delegate.file_systems)) self.assertEqual(200, response.status) # When re-running, all file systems should be Stat()d the same number of # times, but the second round shouldn't have been re-Read() since the # Stats haven't changed. response = CronServlet(Request.ForTest('trunk'), delegate_for_test=delegate).Get() self.assertEqual(2, len(delegate.file_systems)) self.assertTrue(*delegate.file_systems[1].CheckAndReset( read_count=0, stat_count=delegate.file_systems[0].GetStatCount()))
def is_redirect(from_host, from_path, to_url): response = PatchServlet(Request.ForTest(from_path, host=from_host), _PatchServletDelegate()).Get() redirect_url, _ = response.GetRedirect() if redirect_url is None: return (False, '%s/%s did not cause a redirect' % (from_host, from_path)) if redirect_url != to_url: return (False, '%s/%s redirected to %s not %s' % (from_host, from_path, redirect_url, to_url)) return (True, '%s/%s redirected to %s' % (from_host, from_path, redirect_url))
def testSidenavDataSource(self): file_system = MockFileSystem( TestFileSystem( { 'chrome_sidenav.json': json.dumps([{ 'title': 'H1', 'href': 'H1.html', 'items': [{ 'title': 'H2', 'href': '/H2.html' }] }]) }, relative_to=JSON_TEMPLATES)) expected = [{ 'level': 2, 'child_selected': True, 'title': 'H1', 'href': '/H1.html', 'items': [{ 'level': 3, 'selected': True, 'related': True, 'title': 'H2', 'href': '/H2.html', 'parent': { 'href': '/H1.html', 'title': 'H1' } }] }] sidenav_data_source = SidenavDataSource( ServerInstance.ForTest(file_system), Request.ForTest('/H2.html')) self.assertTrue(*file_system.CheckAndReset()) log_output = CaptureLogging(lambda: self.assertEqual( expected, sidenav_data_source.get('chrome'))) self.assertEqual(1, len(log_output)) self.assertTrue(log_output[0].msg.startswith( 'Paths in sidenav must be qualified.')) # Test that only a single file is read when creating the sidenav, so that # we can be confident in the compiled_file_system.SingleFile annotation. self.assertTrue(*file_system.CheckAndReset( read_count=1, stat_count=1, read_resolve_count=1))
def testCreateId(self): fake_avail_finder = _FakeAvailabilityFinder(self._fake_availability) dict_ = CreateJSCView(self._api_models.GetContentScriptAPIs().Get(), self._api_models.GetModel('tester').Get(), fake_avail_finder, self._json_cache, _FakeTemplateCache(), self._features_bundle, None, 'extensions', [], Request.ForTest('')) self.assertEquals('type-TypeA', dict_['types'][0]['id']) self.assertEquals('property-TypeA-b', dict_['types'][0]['properties'][0]['id']) self.assertEquals('method-get', dict_['functions'][0]['id']) self.assertEquals('event-EventA', dict_['events'][0]['id'])
def testIntro(self): intro_data_source = IntroDataSource(self._server_instance, Request.ForTest('')) intro_data = intro_data_source.get('test_intro') article_data = intro_data_source.get('test_article') expected_intro = 'you<h2>first</h2><h3>inner</h3><h2>second</h2>' # Article still has the header. expected_article = '<h1>hi</h1>' + expected_intro self.assertEqual(expected_intro, intro_data.Render().text) self.assertEqual(expected_article, article_data.Render().text)
def Get(self): path = self._request.path if path.startswith('_'): servlet_path = path[1:] if not '/' in servlet_path: servlet_path += '/' servlet_name, servlet_path = servlet_path.split('/', 1) servlet = _SERVLETS.get(servlet_name) if servlet is None: return Response.NotFound('"%s" servlet not found' % servlet_path) else: servlet_path = path servlet = _DEFAULT_SERVLET return servlet( Request(servlet_path, self._request.host, self._request.headers)).Get()
def testGetAPIAvailability(self): api_availabilities = { 'bluetooth': 31, 'contextMenus': 'master', 'jsonStableAPI': 20, 'idle': 5, 'input.ime': 18, 'tabs': 18 } for api_name, availability in api_availabilities.iteritems(): model_dict = CreateJSCView( self._api_models.GetContentScriptAPIs().Get(), self._api_models.GetModel(api_name).Get(), self._avail_finder, self._json_cache, _FakeTemplateCache(), _FakeFeaturesBundle(), None, 'extensions', [], Request.ForTest('')) self.assertEquals( availability, model_dict['introList'][1]['content'][0]['version'])
def run_cron_for_dir(d, path_prefix=''): success = True start_time = time.time() files = [ f for f in server_instance.content_cache.GetFromFileListing(d) if not f.endswith('/') ] logging.info('cron/%s: rendering %s files from %s...' % (channel, len(files), d)) try: for i, f in enumerate(files): error = None path = '%s%s' % (path_prefix, f) try: response = RenderServlet( Request(path, self._request.headers)).Get( server_instance=server_instance) if response.status != 200: error = 'Got %s response' % response.status except DeadlineExceededError: logging.error( 'cron/%s: deadline exceeded rendering %s (%s of %s): %s' % (channel, path, i + 1, len(files), traceback.format_exc())) raise except error: pass if error: logging.error('cron/%s: error rendering %s: %s' % (channel, path, error)) success = False finally: logging.info( 'cron/%s: rendering %s files from %s took %s seconds' % (channel, len(files), d, time.time() - start_time)) return success
def testAddRules(self): fake_avail_finder = _FakeAvailabilityFinder(self._fake_availability) dict_ = CreateJSCView( self._api_models.GetContentScriptAPIs().Get(), self._api_models.GetModel('add_rules_tester').Get(), fake_avail_finder, self._json_cache, _FakeTemplateCache(), self._features_bundle, self._FakeLoadAddRulesSchema(), 'extensions', [], Request.ForTest('')) # Check that the first event has the addRulesFunction defined. self.assertEquals('add_rules_tester', dict_['name']) self.assertEquals('rules', dict_['events'][0]['name']) self.assertEquals( 'notable_name_to_check_for', dict_['events'][0]['byName']['addRules']['parameters'][0]['name']) # Check that the second event has addListener defined. self.assertEquals('noRules', dict_['events'][1]['name']) self.assertEquals('add_rules_tester', dict_['name']) self.assertEquals('noRules', dict_['events'][1]['name']) self.assertEquals( 'callback', dict_['events'][0]['byName']['addListener'] ['parameters'][0]['name'])
def _Render(self, path): return RenderServlet(Request.ForTest(path), _RenderServletDelegate()).Get()
def get_via_render_servlet(path): request = Request(path, self._request.host, self._request.headers) delegate = _SingletonRenderServletDelegate(server_instance) return RenderServlet(request, delegate).Get()