web/test/unittest_views_apacherewrite.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    18 """
       
    19 
       
    20 """
       
    21 from logilab.common.testlib import TestCase, unittest_main
       
    22 
       
    23 from cubicweb.web.views.apacherewrite import *
       
    24 
       
    25 class ApacheURLRewriteTC(TestCase):
       
    26 
       
    27     def test(self):
       
    28         class MyAppRules(ApacheURLRewrite):
       
    29             rules = [
       
    30                 RewriteCond('logilab\.fr', match='host',
       
    31                             rules=[('/(.*)', r'http://www.logilab.fr/\1')],
       
    32                             action='redirect'),
       
    33                 RewriteCond('(www)\.logilab\.fr', match='host', action='stop'),
       
    34                 RewriteCond('/(data|json)/', match='path', action='stop'),
       
    35                 RewriteCond('(?P<cat>.*)\.logilab\.fr', match='host',
       
    36                             rules=[('/(.*)', r'/m_%(cat)s/\1')]),
       
    37                 ]
       
    38         urlrewriter = MyAppRules()
       
    39         req = None # not used in the above rules, so keep a simple TestCase here
       
    40         try:
       
    41             urlrewriter.rewrite('logilab.fr', '/whatever', req)
       
    42             self.fail('redirect exception expected')
       
    43         except Redirect as ex:
       
    44             self.assertEqual(ex.location, 'http://www.logilab.fr/whatever')
       
    45         self.assertEqual(urlrewriter.rewrite('www.logilab.fr', '/whatever', req),
       
    46                           '/whatever')
       
    47         self.assertEqual(urlrewriter.rewrite('www.logilab.fr', '/json/bla', req),
       
    48                           '/json/bla')
       
    49         self.assertEqual(urlrewriter.rewrite('abcd.logilab.fr', '/json/bla', req),
       
    50                           '/json/bla')
       
    51         self.assertEqual(urlrewriter.rewrite('abcd.logilab.fr', '/data/bla', req),
       
    52                           '/data/bla')
       
    53         self.assertEqual(urlrewriter.rewrite('abcd.logilab.fr', '/whatever', req),
       
    54                           '/m_abcd/whatever')
       
    55         self.assertEqual(urlrewriter.rewrite('abcd.fr', '/whatever', req),
       
    56                           '/whatever')
       
    57 
       
    58 
       
    59 if __name__ == '__main__':
       
    60     unittest_main()