Home | History | Annotate | Download | only in server2
      1 #!/usr/bin/env python
      2 # Copyright 2013 The Chromium Authors. All rights reserved.
      3 # Use of this source code is governed by a BSD-style license that can be
      4 # found in the LICENSE file.
      5 
      6 import unittest
      7 
      8 from file_system import FileNotFoundError
      9 from link_error_detector import LinkErrorDetector
     10 from servlet import Response
     11 from test_file_system import TestFileSystem
     12 
     13 file_system = TestFileSystem({
     14   'docs': {
     15     'templates': {
     16       'public': {
     17         'apps': {
     18           '404.html': '404',
     19           'index.html': '''
     20             <h1 id="actual-top">Hello</h1>
     21             <a href="#top">world</p>
     22             <a href="#actual-top">!</a>
     23             <a href="broken.json"></a>
     24             <a href="crx.html"></a>
     25             ''',
     26           'crx.html': '''
     27             <a href="index.html#actual-top">back</a>
     28             <a href="broken.html"></a>
     29             <a href="devtools.events.html">do to underscore translation</a>
     30             ''',
     31           'devtools_events.html': '''
     32             <a href=" http://www.google.com/">leading space in href</a>
     33             <a href=" index.html">home</a>
     34             <a href="index.html#invalid"></a>
     35             <a href="fake.html#invalid"></a>
     36             ''',
     37           'unreachable.html': '''
     38             <p>so lonely</p>
     39             <a href="#aoesu"></a>
     40             <a href="invalid.html"></a>
     41             ''',
     42           'devtools_disconnected.html': ''
     43         }
     44       }
     45     },
     46     'static': {},
     47     'examples': {},
     48   }
     49 })
     50 
     51 class LinkErrorDetectorTest(unittest.TestCase):
     52   def _Render(self, path):
     53     try:
     54       return Response(
     55           content=file_system.ReadSingle('docs/templates/public/' + path).Get(),
     56           status=200)
     57     except FileNotFoundError:
     58       return Response(status=404)
     59 
     60   def testGetBrokenLinks(self):
     61     expected_broken_links = set([
     62       (404, 'apps/crx.html', 'apps/broken.html', 'target page not found'),
     63       (404, 'apps/index.html', 'apps/broken.json', 'target page not found'),
     64       (404, 'apps/unreachable.html', 'apps/invalid.html',
     65           'target page not found'),
     66       (404, 'apps/devtools_events.html', 'apps/fake.html#invalid',
     67           'target page not found'),
     68       (200, 'apps/devtools_events.html', 'apps/index.html#invalid',
     69           'target anchor not found'),
     70       (200, 'apps/unreachable.html', '#aoesu', 'target anchor not found')])
     71 
     72     link_error_detector = LinkErrorDetector(
     73         file_system, self._Render, 'templates/public/', ('apps/index.html'))
     74     broken_links = link_error_detector.GetBrokenLinks()
     75 
     76     self.assertEqual(expected_broken_links, set(broken_links))
     77 
     78   def testGetOrphanedPages(self):
     79     expected_orphaned_pages = set([
     80       'apps/unreachable.html',
     81       'apps/devtools_disconnected.html'])
     82 
     83     link_error_detector = LinkErrorDetector(
     84         file_system, self._Render, 'templates/public/', ('apps/crx.html',))
     85     orphaned_pages = link_error_detector.GetOrphanedPages()
     86 
     87     self.assertEqual(expected_orphaned_pages, set(orphaned_pages))
     88 
     89 if __name__ == '__main__':
     90   unittest.main()
     91