• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2013 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5from extensions_paths import PUBLIC_TEMPLATES
6from instance_servlet import (
7    InstanceServlet, InstanceServletRenderServletDelegate)
8from link_error_detector import LinkErrorDetector, StringifyBrokenLinks
9from servlet import Request, Response, Servlet
10
11
12class BrokenLinkTester(object):
13  '''Run link error detector tests.
14  '''
15  def __init__(self, server_instance, renderer):
16    self.link_error_detector = LinkErrorDetector(
17        server_instance.host_file_system_provider.GetTrunk(),
18        renderer,
19        PUBLIC_TEMPLATES,
20        root_pages=('extensions/index.html', 'apps/about_apps.html'))
21
22  def TestBrokenLinks(self):
23    broken_links = self.link_error_detector.GetBrokenLinks()
24    return (
25        len(broken_links),
26        'Warning: Found %d broken links:\n%s' % (
27            len(broken_links), StringifyBrokenLinks(broken_links)))
28
29  def TestOrphanedPages(self):
30    orphaned_pages = self.link_error_detector.GetOrphanedPages()
31    return (
32        len(orphaned_pages),
33        'Warning: Found %d orphaned pages:\n%s' % (
34            len(orphaned_pages), '\n'.join(orphaned_pages)))
35
36
37class TestServlet(Servlet):
38  '''Runs tests against the live server. Supports running all broken link
39  detection tests, in parts or all at once.
40  '''
41  def __init__(self, request, delegate_for_test=None):
42    Servlet.__init__(self, request)
43    self._delegate = delegate_for_test or InstanceServlet.Delegate()
44
45  def Get(self):
46    link_error_tests = ('broken_links', 'orphaned_pages', 'link_errors')
47
48    if not self._request.path in link_error_tests:
49      return Response.NotFound('Test %s not found. Available tests are: %s' % (
50          self._request.path, ','.join(link_error_tests)))
51
52    constructor = InstanceServlet.GetConstructor(self._delegate)
53    def renderer(path):
54      return constructor(Request(path, '', self._request.headers)).Get()
55
56    link_tester = BrokenLinkTester(
57        InstanceServletRenderServletDelegate(
58            self._delegate).CreateServerInstance(),
59        renderer)
60    if self._request.path == 'broken_links':
61      errors, content = link_tester.TestBrokenLinks()
62    elif self._request.path == 'orphaned_pages':
63      errors, content = link_tester.TestOrphanedPages()
64    else:
65      link_errors, link_content = link_tester.TestBrokenLinks()
66      orphaned_errors, orphaned_content = link_tester.TestOrphanedPages()
67      errors = link_errors + orphaned_errors
68      content = "%s\n%s" % (link_content, orphaned_content)
69
70    if errors:
71      return Response.InternalError(content=content)
72
73    return Response.Ok(content="%s test passed." % self._request.path)
74