Home | History | Annotate | Download | only in webpagereplay
      1 # Copyright 2015 Google Inc. All Rights Reserved.
      2 #
      3 # Licensed under the Apache License, Version 2.0 (the "License");
      4 # you may not use this file except in compliance with the License.
      5 # You may obtain a copy of the License at
      6 #
      7 #      http://www.apache.org/licenses/LICENSE-2.0
      8 #
      9 # Unless required by applicable law or agreed to in writing, software
     10 # distributed under the License is distributed on an "AS IS" BASIS,
     11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 # See the License for the specific language governing permissions and
     13 # limitations under the License.
     14 
     15 """Unit tests for rules_parser.  Usage: ./rules_parser_test.py"""
     16 
     17 import collections
     18 import logging
     19 from StringIO import StringIO
     20 import unittest
     21 
     22 import rules_parser
     23 
     24 
     25 class RuleParserTest(unittest.TestCase):
     26 
     27   @classmethod
     28   def setUpClass(cls):
     29     if not logging.root.handlers:
     30       logging.basicConfig(level=logging.DEBUG,  # Enable log_url stdout.
     31                           format='%(asctime)s %(levelname)s %(message)s')
     32 
     33   def testCall(self):
     34     my_rules = rules_parser.Rules(StringIO(r'''
     35         [{"comment": "ignore me"},
     36          {"LogUrl": {"url": "example\\.com/ss.*"}},
     37          {"LogUrl": {"url": "example\\.com/blah$"}}]'''))
     38     log_url = my_rules.Find('log_url')
     39     self.assertEquals(True, log_url(FakeRequest(full_path='/ss'), None))
     40     self.assertEquals(True, log_url(FakeRequest(full_path='/ssxxxx'), None))
     41     self.assertEquals(True, log_url(FakeRequest(full_path='/blah'), None))
     42     self.assertEquals(None, log_url(FakeRequest(full_path='/blahxxx'), None))
     43     self.assertEquals(None, log_url(FakeRequest(full_path='/'), None))
     44 
     45   def testImport(self):
     46     my_rules = rules_parser.Rules(StringIO(r'''
     47         [{"rules.LogUrl": {"url": "example\\.com/ss.*"}}]'''))
     48     self.assertTrue(my_rules.Contains('log_url'))
     49 
     50   def testRaises(self):
     51     input_pairs = [
     52         'bad_json',
     53         '123',
     54         '{}',
     55         '[42]',
     56         '[{12:34}]',
     57         '[{"a":"b","c":"d"}]',
     58         '[{"bad+rule@name":{}}]',
     59         '["unallowed.Path":{}]',
     60         '["NoSuchRule":{}]',
     61         '["LogUrl":"bad"]',
     62         '["LogUrl":{}]',
     63         '["LogUrl":{"url":123}]',
     64         '["LogUrl":{"url":"", "bad_arg":123}]',
     65     ]
     66     for input_text in input_pairs:
     67       self.assertRaises(Exception, rules_parser.Rules, StringIO(input_text))
     68 
     69 
     70 class FakeRequest(collections.namedtuple(
     71     'FakeRequest', ('command', 'host', 'full_path', 'request_body',
     72                     'headers', 'is_ssl'))):
     73 
     74   def __new__(cls, command='GET', host='example.com', full_path='/',
     75               request_body=None, headers=None, is_ssl=False):
     76     return super(FakeRequest, cls).__new__(
     77         cls, command, host, full_path, request_body, headers or {}, is_ssl)
     78 
     79 
     80 if __name__ == '__main__':
     81   unittest.main()
     82