def test_parses_empty_rules(): assert len(HttpRules(None)) == 2 assert len(HttpRules("")) == 2 assert len(HttpRules(" ")) == 2 assert len(HttpRules("\t")) == 2 assert len(HttpRules("\n")) == 2 assert HttpRules.parse_rule(None) is None assert HttpRules.parse_rule("") is None assert HttpRules.parse_rule(" ") is None assert HttpRules.parse_rule("\t") is None assert HttpRules.parse_rule("\n") is None
def test_load_rules_from_file(): rules = HttpRules("file://./tests/test_rules1.txt") assert len(rules) == 1 assert len(rules.sample) == 1 assert rules.sample[0].param1 == 55 rules = HttpRules("file://./tests/test_rules2.txt") assert len(rules) == 3 assert rules.allow_http_url is True assert len(rules.copy_session_field) == 1 assert len(rules.sample) == 1 assert rules.sample[0].param1 == 56 rules = HttpRules("file://./tests/test_rules3.txt") assert len(rules.remove) == 1 assert len(rules.replace) == 1 assert len(rules.sample) == 1 assert rules.sample[0].param1 == 57
def parse_ok(line, verb, scope, param1, param2): rule = HttpRules.parse_rule(line) assert rule.verb == verb if rule.scope is None: assert scope is None else: assert rule.scope.pattern == re.compile(scope).pattern if param1 is None: assert True elif hasattr(rule.param1, "match"): assert rule.param1.pattern == re.compile(param1).pattern else: assert rule.param1 == param1 if param2 is None: assert True elif hasattr(rule.param2, "match"): assert rule.param2.pattern == re.compile(param2).pattern else: assert rule.param2 == param2
def test_raises_expected_errors(): try: HttpRules("file://~/bleepblorpbleepblorp12345") assert False except FileNotFoundError as e: assert str(e) == "Failed to load rules: ~/bleepblorpbleepblorp12345" try: HttpRules("/*! stop") assert False except SyntaxError as e: assert str(e) == "Invalid expression (/*!) in rule: /*! stop" try: HttpRules("/*/ stop") assert False except SyntaxError as e: assert str(e) == "Invalid regex (/*/) in rule: /*/ stop" try: HttpRules("/boo") assert False except SyntaxError as e: assert str(e) == "Invalid rule: /boo" try: HttpRules("sample 123") assert False except SyntaxError as e: assert str(e) == "Invalid sample percent: 123" try: HttpRules("!!! stop") assert False except SyntaxError as e: assert str(e) == "Unescaped separator (!) in rule: !!! stop"
def test_includes_strict_rules(): rules = HttpRules("include strict") assert len(rules) == 2 assert len(rules.remove) == 1 assert len(rules.replace) == 1 rules = HttpRules("include strict\n") assert len(rules) == 2 rules = HttpRules("include strict\nsample 50") assert len(rules) == 3 assert len(rules.sample) == 1 rules = HttpRules("include strict\ninclude strict\n") assert len(rules) == 4 rules = HttpRules("include strict\nsample 50\ninclude strict\n") assert len(rules) == 5 assert HttpRules.default_rules() == HttpRules.strict_rules() try: HttpRules.set_default_rules("include strict") rules = HttpRules("") assert len(rules) == 2 assert len(rules.remove) == 1 assert len(rules.replace) == 1 finally: HttpRules.set_default_rules(HttpRules.strict_rules())
def test_changes_default_rules(): assert HttpRules.default_rules() == HttpRules.strict_rules() try: HttpRules.set_default_rules("") assert HttpRules.default_rules() == "" assert len(HttpRules(HttpRules.default_rules())) == 0 HttpRules.set_default_rules(" include default") assert HttpRules.default_rules() == "" HttpRules.set_default_rules("include default\n") assert HttpRules.default_rules() == "" HttpRules.set_default_rules(" include default\ninclude default\n") assert len(HttpRules(HttpRules.default_rules())) == 0 HttpRules.set_default_rules( " include default\ninclude default\nsample 42") rules = HttpRules(HttpRules.default_rules()) assert len(rules) == 1 assert len(rules.sample) == 1 finally: HttpRules.set_default_rules(HttpRules.strict_rules())
def test_includes_standard_rules(): rules = HttpRules("include standard") assert len(rules) == 3 assert len(rules.remove) == 1 assert len(rules.replace) == 2 rules = HttpRules("include standard\n") assert len(rules) == 3 rules = HttpRules("include standard\nsample 50") assert len(rules) == 4 assert len(rules.sample) == 1 rules = HttpRules("include standard\ninclude standard\n") assert len(rules) == 6 rules = HttpRules("include standard\nsample 50\ninclude standard\n") assert len(rules) == 7 assert HttpRules.default_rules() == HttpRules.strict_rules() try: HttpRules.set_default_rules("include standard") rules = HttpRules("") assert len(rules) == 3 assert len(rules.remove) == 1 assert len(rules.replace) == 2 finally: HttpRules.set_default_rules(HttpRules.strict_rules())
def test_includes_debug_rules(): rules = HttpRules("include debug") assert len(rules) == 2 assert rules.allow_http_url is True assert len(rules.copy_session_field) == 1 rules = HttpRules("include debug\n") assert len(rules) == 2 rules = HttpRules("include debug\nsample 50") assert len(rules) == 3 assert len(rules.sample) == 1 rules = HttpRules("include debug\ninclude debug\n") assert len(rules) == 4 rules = HttpRules("include debug\nsample 50\ninclude debug\n") assert len(rules) == 5 assert HttpRules.default_rules() == HttpRules.strict_rules() try: HttpRules.set_default_rules("include debug") rules = HttpRules("") assert len(rules) == 2 assert rules.allow_http_url is True assert len(rules.copy_session_field) == 1 finally: HttpRules.set_default_rules(HttpRules.strict_rules())
def parse_fail(line): try: HttpRules.parse_rule(line) except Exception: return assert False
def test_overrides_default_rules(): assert HttpRules.default_rules() == HttpRules.strict_rules() try: logger = HttpLogger(url="https://mysite.com") assert logger.rules.text == HttpRules.strict_rules() logger = HttpLogger(url="https://mysite.com", rules="# 123") assert logger.rules.text == "# 123" HttpRules.set_default_rules("") logger = HttpLogger(url="https://mysite.com") assert logger.rules.text == "" logger = HttpLogger(url="https://mysite.com", rules=" ") assert logger.rules.text == "" logger = HttpLogger(url="https://mysite.com", rules=" sample 42") assert logger.rules.text == " sample 42" HttpRules.set_default_rules("skip_compression") logger = HttpLogger(url="https://mysite.com") assert logger.rules.text == "skip_compression" logger = HttpLogger(url="https://mysite.com", rules="include default\nskip_submission\n") assert logger.rules.text == "skip_compression\nskip_submission\n" HttpRules.set_default_rules("sample 42\n") logger = HttpLogger(url="https://mysite.com") assert logger.rules.text == "sample 42\n" logger = HttpLogger(url="https://mysite.com", rules=" ") assert logger.rules.text == "sample 42\n" logger = HttpLogger(url="https://mysite.com", rules="include default\nskip_submission\n") assert logger.rules.text == "sample 42\n\nskip_submission\n" HttpRules.set_default_rules("include debug") logger = HttpLogger(url="https://mysite.com", rules=HttpRules.strict_rules()) assert logger.rules.text == HttpRules.strict_rules() finally: HttpRules.set_default_rules(HttpRules.strict_rules())
def test_changes_default_rules(): assert HttpRules.default_rules() == HttpRules.strict_rules() try: HttpRules.set_default_rules('') assert HttpRules.default_rules() == '' assert len(HttpRules(HttpRules.default_rules())) == 0 HttpRules.set_default_rules(' include default') assert HttpRules.default_rules() == '' HttpRules.set_default_rules('include default\n') assert HttpRules.default_rules() == '' HttpRules.set_default_rules(' include default\ninclude default\n') assert len(HttpRules(HttpRules.default_rules())) == 0 HttpRules.set_default_rules( ' include default\ninclude default\nsample 42') rules = HttpRules(HttpRules.default_rules()) assert len(rules) == 1 assert len(rules.sample) == 1 finally: HttpRules.set_default_rules(HttpRules.strict_rules())