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 json
      7 import unittest
      8 
      9 from compiled_file_system import CompiledFileSystem
     10 from object_store_creator import ObjectStoreCreator
     11 from redirector import Redirector
     12 from test_file_system import TestFileSystem
     13 from third_party.json_schema_compiler.json_parse import Parse
     14 
     15 HOST = 'http://localhost/'
     16 
     17 file_system = TestFileSystem({
     18   'redirects.json': json.dumps({
     19     '': '/index.html',
     20     'home': 'index.html',
     21     'index.html': 'http://something.absolute.com/'
     22   }),
     23   'apps': {
     24     'redirects.json': json.dumps({
     25       '': '../index.html',
     26       'index.html': 'about_apps.html',
     27       'foo.html': '/bar.html',
     28     })
     29   },
     30   'extensions': {
     31     'redirects.json': json.dumps({
     32       'manifest': 'manifest.html'
     33     }),
     34     'manifest': {
     35       'redirects.json': json.dumps({
     36         '': '../manifest.html',
     37         'more-info': 'http://lmgtfy.com'
     38       })
     39     }
     40   }
     41 })
     42 
     43 class RedirectorTest(unittest.TestCase):
     44   def setUp(self):
     45     self._redirector = Redirector(
     46         CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()),
     47         file_system)
     48 
     49   def testExternalRedirection(self):
     50     self.assertEqual(
     51         'http://something.absolute.com/',
     52         self._redirector.Redirect(HOST, 'index.html'))
     53     self.assertEqual(
     54         'http://lmgtfy.com',
     55         self._redirector.Redirect(HOST, 'extensions/manifest/more-info'))
     56 
     57   def testAbsoluteRedirection(self):
     58     self.assertEqual(
     59         '/index.html', self._redirector.Redirect(HOST, ''))
     60     self.assertEqual(
     61         '/bar.html', self._redirector.Redirect(HOST, 'apps/foo.html'))
     62 
     63   def testRelativeRedirection(self):
     64     self.assertEqual(
     65         'apps/about_apps.html',
     66         self._redirector.Redirect(HOST, 'apps/index.html'))
     67     self.assertEqual(
     68         'extensions/manifest.html',
     69         self._redirector.Redirect(HOST, 'extensions/manifest/'))
     70     self.assertEqual(
     71         'extensions/manifest.html',
     72         self._redirector.Redirect(HOST, 'extensions/manifest'))
     73     self.assertEqual(
     74         'index.html', self._redirector.Redirect(HOST, 'apps/'))
     75     self.assertEqual(
     76         'index.html', self._redirector.Redirect(HOST, 'home'))
     77 
     78   def testNotFound(self):
     79     self.assertEqual(
     80         None, self._redirector.Redirect(HOST, 'not/a/real/path'))
     81     self.assertEqual(
     82         None, self._redirector.Redirect(HOST, 'public/apps/okay.html'))
     83 
     84   def testOldHosts(self):
     85     self.assertEqual(
     86         'https://developer.chrome.com/',
     87         self._redirector.Redirect('http://code.google.com', ''))
     88     self.assertEqual(
     89         'https://developer.chrome.com/',
     90         self._redirector.Redirect('https://code.google.com', ''))
     91 
     92   def testCron(self):
     93     self._redirector.Cron().Get()
     94 
     95     expected_paths = set([
     96       'redirects.json',
     97       'apps/redirects.json',
     98       'extensions/redirects.json',
     99       'extensions/manifest/redirects.json'
    100     ])
    101 
    102     for path in expected_paths:
    103       self.assertEqual(
    104           Parse(file_system.ReadSingle(path).Get()),
    105           # Access the cache's object store to see what files were hit during
    106           # the cron run. Returns strings parsed as JSON.
    107           # TODO(jshumway): Make a non hack version of this check.
    108           self._redirector._cache._file_object_store.Get(
    109               path).Get()._cache_data)
    110 
    111 if __name__ == '__main__':
    112   unittest.main()
    113