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:
parent
d9ec206a4c
commit
ab9a38e522
|
@ -30,6 +30,12 @@ class Rule(object):
|
||||||
self.pattern = params[2]
|
self.pattern = params[2]
|
||||||
self.target = params[3]
|
self.target = params[3]
|
||||||
elif len(params) == 3:
|
elif len(params) == 3:
|
||||||
|
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
|
# redirect pattern target
|
||||||
# (code is implied)
|
# (code is implied)
|
||||||
self.code = '301'
|
self.code = '301'
|
||||||
|
@ -63,12 +69,19 @@ class RedirectMatch(Rule):
|
||||||
def __init__(self, linenum, *params):
|
def __init__(self, linenum, *params):
|
||||||
super(RedirectMatch, self).__init__(linenum, *params)
|
super(RedirectMatch, self).__init__(linenum, *params)
|
||||||
self.regex = re.compile(self.pattern)
|
self.regex = re.compile(self.pattern)
|
||||||
|
if self.target:
|
||||||
self.target_repl = self.target.replace('$', '\\')
|
self.target_repl = self.target.replace('$', '\\')
|
||||||
|
else:
|
||||||
|
self.target_repl = None
|
||||||
|
|
||||||
def match(self, path):
|
def match(self, path):
|
||||||
m = self.regex.search(path)
|
m = self.regex.search(path)
|
||||||
if m:
|
if m:
|
||||||
|
if self.target_repl:
|
||||||
return (self.code, self.regex.sub(self.target_repl, path))
|
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
|
return None
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -66,6 +66,23 @@ class TestRedirect(base.TestCase):
|
||||||
(1, 'redirect', '301', '/the/path', '/new/path', 'extra-value'),
|
(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):
|
class TestRedirectMatch(base.TestCase):
|
||||||
|
|
||||||
|
@ -133,6 +150,23 @@ class TestRedirectMatch(base.TestCase):
|
||||||
'extra-value'),
|
'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):
|
class TestRuleSet(base.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue