def testGetRedirectChain(self): loading_trace = test_utils.LoadingTraceFromEvents( [TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.SECOND_REDIRECT_REQUEST, TestRequests.REDIRECTED_REQUEST]) request_dependencies_lens = RequestDependencyLens(loading_trace) whole_chain = [TestRequests.FIRST_REDIRECT_REQUEST, TestRequests.SECOND_REDIRECT_REQUEST, TestRequests.REDIRECTED_REQUEST] chain = request_dependencies_lens.GetRedirectChain( TestRequests.FIRST_REDIRECT_REQUEST) self.assertListEqual(whole_chain, chain) chain = request_dependencies_lens.GetRedirectChain( TestRequests.SECOND_REDIRECT_REQUEST) self.assertListEqual(whole_chain[1:], chain) chain = request_dependencies_lens.GetRedirectChain( TestRequests.REDIRECTED_REQUEST) self.assertEquals(whole_chain[2:], chain)
def ExtractDiscoverableUrls(loading_trace_path, subresource_discoverer): """Extracts discoverable resource urls from a loading trace according to a sub-resource discoverer. Args: loading_trace_path: The loading trace's path. subresource_discoverer: The sub-resources discoverer that should white-list the resources to keep in cache for the NoState-Prefetch benchmarks. Returns: A set of urls. """ assert subresource_discoverer in SUBRESOURCE_DISCOVERERS, \ 'unknown prefetch simulation {}'.format(subresource_discoverer) # Load trace and related infos. logging.info('loading %s' % loading_trace_path) trace = LoadingTrace.FromJsonFile(loading_trace_path) dependencies_lens = RequestDependencyLens(trace) first_resource_request = trace.request_track.GetFirstResourceRequest() # Build the list of discovered requests according to the desired simulation. discovered_requests = [] if subresource_discoverer == EMPTY_CACHE_DISCOVERER: pass elif subresource_discoverer == FULL_CACHE_DISCOVERER: discovered_requests = trace.request_track.GetEvents() elif subresource_discoverer == REDIRECTED_MAIN_DISCOVERER: discovered_requests = \ [dependencies_lens.GetRedirectChain(first_resource_request)[-1]] elif subresource_discoverer == PARSER_DISCOVERER: discovered_requests = PrefetchSimulationView.ParserDiscoverableRequests( first_resource_request, dependencies_lens) elif subresource_discoverer == HTML_PRELOAD_SCANNER_DISCOVERER: discovered_requests = PrefetchSimulationView.PreloadedRequests( first_resource_request, dependencies_lens, trace) else: assert False whitelisted_urls = set() logging.info('white-listing %s' % first_resource_request.url) for request in _FilterOutDataAndIncompleteRequests(discovered_requests): logging.info('white-listing %s' % request.url) whitelisted_urls.add(request.url) return whitelisted_urls