support 410 rules

Add support for redirect responses that indicate that a path is not
going to be valid again in the future and return a 410 response. This
is needed for testing nova's .htaccess file.

Change-Id: Idebefd6c57d04886394f8af435f8aa71aa94c7a2
Signed-off-by: Doug Hellmann <doug@doughellmann.com>
This commit is contained in:
Doug Hellmann 2017-09-10 13:26:57 -04:00
parent d9ec206a4c
commit ab9a38e522
2 changed files with 54 additions and 7 deletions

View File

@ -30,11 +30,17 @@ class Rule(object):
self.pattern = params[2]
self.target = params[3]
elif len(params) == 3:
# redirect pattern target
# (code is implied)
self.code = '301'
self.pattern = params[1]
self.target = params[2]
if params[1] == '410':
# The page has been deleted and is not coming back.
self.code = params[1]
self.pattern = params[2]
self.target = None
else:
# redirect pattern target
# (code is implied)
self.code = '301'
self.pattern = params[1]
self.target = params[2]
else:
raise ValueError('Could not understand rule {}'.format(params))
@ -63,12 +69,19 @@ class RedirectMatch(Rule):
def __init__(self, linenum, *params):
super(RedirectMatch, self).__init__(linenum, *params)
self.regex = re.compile(self.pattern)
self.target_repl = self.target.replace('$', '\\')
if self.target:
self.target_repl = self.target.replace('$', '\\')
else:
self.target_repl = None
def match(self, path):
m = self.regex.search(path)
if m:
return (self.code, self.regex.sub(self.target_repl, path))
if self.target_repl:
return (self.code, self.regex.sub(self.target_repl, path))
else:
# A rule that doesn't have a response target, like 410.
return (self.code, self.target_repl)
return None

View File

@ -66,6 +66,23 @@ class TestRedirect(base.TestCase):
(1, 'redirect', '301', '/the/path', '/new/path', 'extra-value'),
)
def test_410(self):
rule = rules.Redirect(
1,
'redirect', '410', '/the/path',
)
self.assertEqual(
'410',
rule.code,
)
self.assertIsNone(
rule.target,
)
self.assertEqual(
('410', None),
rule.match('/the/path'),
)
class TestRedirectMatch(base.TestCase):
@ -133,6 +150,23 @@ class TestRedirectMatch(base.TestCase):
'extra-value'),
)
def test_410(self):
rule = rules.RedirectMatch(
1,
'redirect', '410', '/the/path',
)
self.assertEqual(
'410',
rule.code,
)
self.assertIsNone(
rule.target,
)
self.assertEqual(
('410', None),
rule.match('/the/path'),
)
class TestRuleSet(base.TestCase):