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.
5 from extensions_paths
import PUBLIC_TEMPLATES
6 from instance_servlet
import (
7 InstanceServlet
, InstanceServletRenderServletDelegate
)
8 from link_error_detector
import LinkErrorDetector
, StringifyBrokenLinks
9 from servlet
import Request
, Response
, Servlet
12 class BrokenLinkTester(object):
13 '''Run link error detector tests.
15 def __init__(self
, server_instance
, renderer
):
16 self
.link_error_detector
= LinkErrorDetector(
17 server_instance
.host_file_system_provider
.GetMaster(),
20 root_pages
=('extensions/index.html', 'apps/about_apps.html'))
22 def TestBrokenLinks(self
):
23 broken_links
= self
.link_error_detector
.GetBrokenLinks()
26 'Warning: Found %d broken links:\n%s' % (
27 len(broken_links
), StringifyBrokenLinks(broken_links
)))
29 def TestOrphanedPages(self
):
30 orphaned_pages
= self
.link_error_detector
.GetOrphanedPages()
33 'Warning: Found %d orphaned pages:\n%s' % (
34 len(orphaned_pages
), '\n'.join(orphaned_pages
)))
37 class TestServlet(Servlet
):
38 '''Runs tests against the live server. Supports running all broken link
39 detection tests, in parts or all at once.
41 def __init__(self
, request
, delegate_for_test
=None):
42 Servlet
.__init
__(self
, request
)
43 self
._delegate
= delegate_for_test
or InstanceServlet
.Delegate()
46 link_error_tests
= ('broken_links', 'orphaned_pages', 'link_errors')
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
)))
52 constructor
= InstanceServlet
.GetConstructor(self
._delegate
)
54 return constructor(Request(path
, '', self
._request
.headers
)).Get()
56 link_tester
= BrokenLinkTester(
57 InstanceServletRenderServletDelegate(
58 self
._delegate
).CreateServerInstance(),
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()
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
)
71 return Response
.InternalError(content
=content
)
73 return Response
.Ok(content
="%s test passed." % self
._request
.path
)