swob.Match: Optional whitespace is optional

https://tools.ietf.org/html/rfc7232#section-3 defines the form for
If-Match and If-None-Match as

    If-Match = "*" / 1#entity-tag
    If-None-Match = "*" / 1#entity-tag

https://tools.ietf.org/html/rfc7230#section-7 in turn defines the
1#<type> syntax as

    1#element => element *( OWS "," OWS element )

where OWS is *optional* whitespace. Our swob.Match object should respect
that optionality.

Change-Id: I6ee1c6674e0e9c156149319022fd289504bd3722
This commit is contained in:
Tim Burke 2018-03-26 14:30:07 -07:00
parent 9b4c978f99
commit baa4fa5d65
2 changed files with 18 additions and 1 deletions

View File

@ -622,7 +622,10 @@ class Match(object):
"""
def __init__(self, headerval):
self.tags = set()
for tag in headerval.split(', '):
for tag in headerval.split(','):
tag = tag.strip()
if not tag:
continue
if tag.startswith('"') and tag.endswith('"'):
self.tags.add(tag[1:-1])
else:

View File

@ -285,6 +285,20 @@ class TestMatch(unittest.TestCase):
self.assertIn('b', match)
self.assertNotIn('c', match)
def test_match_no_optional_white_space(self):
match = swift.common.swob.Match('"a","b"')
self.assertEqual(match.tags, set(('a', 'b')))
self.assertIn('a', match)
self.assertIn('b', match)
self.assertNotIn('c', match)
def test_match_lots_of_optional_white_space(self):
match = swift.common.swob.Match('"a" , , "b" ')
self.assertEqual(match.tags, set(('a', 'b')))
self.assertIn('a', match)
self.assertIn('b', match)
self.assertNotIn('c', match)
class TestTransferEncoding(unittest.TestCase):
def test_is_chunked(self):