add match() method to ruleset
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
parent
6a4c080f56
commit
bcc2b80be2
|
@ -87,3 +87,12 @@ class RuleSet(object):
|
|||
rule_type = params[0].lower()
|
||||
rule = self._factories[rule_type](linenum, *params)
|
||||
self._rules.append(rule)
|
||||
|
||||
def __iter__(self):
|
||||
return iter(self._rules)
|
||||
|
||||
def match(self, path):
|
||||
for rule in self:
|
||||
m = rule.match(path)
|
||||
if m is not None:
|
||||
yield (rule.linenum,) + m
|
||||
|
|
|
@ -155,3 +155,28 @@ class TestRuleSet(base.TestCase):
|
|||
)
|
||||
self.assertEqual(1, len(self.ruleset._rules))
|
||||
self.assertIsInstance(self.ruleset._rules[0], rules.RedirectMatch)
|
||||
|
||||
def test_match_one(self):
|
||||
self.ruleset.add(
|
||||
1,
|
||||
'redirect', '301', '/path', '/new/path',
|
||||
)
|
||||
self.assertEqual(
|
||||
[(1, '301', '/new/path')],
|
||||
list(self.ruleset.match('/path')),
|
||||
)
|
||||
|
||||
def test_match_multiple(self):
|
||||
self.ruleset.add(
|
||||
1,
|
||||
'redirect', '301', '/path', '/new/path',
|
||||
)
|
||||
self.ruleset.add(
|
||||
2,
|
||||
'redirect', '301', '/path', '/other/path',
|
||||
)
|
||||
self.assertEqual(
|
||||
[(1, '301', '/new/path'),
|
||||
(2, '301', '/other/path')],
|
||||
list(self.ruleset.match('/path')),
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue