Disallow {foo} in search engine URLs.

This causes an KeyError otherwise when trying to use str.format to insert the
search term.
This commit is contained in:
Florian Bruhin 2015-06-24 07:46:15 +02:00
parent b21b4377a8
commit b7c3e7b959
2 changed files with 12 additions and 0 deletions

View File

@ -1110,8 +1110,15 @@ class SearchEngineUrl(BaseType):
return
else:
raise configexc.ValidationError(value, "may not be empty!")
if '{}' not in value:
raise configexc.ValidationError(value, "must contain \"{}\"")
try:
value.format("")
except KeyError:
raise configexc.ValidationError(
value, "may not contain {...} (use {{ and }} for literal {/})")
url = QUrl(value.replace('{}', 'foobar'))
if not url.isValid():
raise configexc.ValidationError(value, "invalid url, {}".format(

View File

@ -1881,6 +1881,11 @@ class TestSearchEngineUrl:
with pytest.raises(configexc.ValidationError):
self.t.validate(':{}')
def test_validate_format_string(self):
"""Test validate with a {foo} format string."""
with pytest.raises(configexc.ValidationError):
self.t.validate('foo{bar}baz{}')
def test_transform_empty(self):
"""Test transform with an empty value."""
assert self.t.transform('') is None