Skip to content

Commit

Permalink
fixes and tests for charset accept
Browse files Browse the repository at this point in the history
  • Loading branch information
bwhmather committed Aug 23, 2015
1 parent 23cdf8d commit e8d10cc
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
23 changes: 13 additions & 10 deletions verktyg/http/accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,16 @@ def __init__(self, value, *, qs=None):

def _matches_option(self, option):
if option.value == '*':
return self.match_type(
self, exact_match=False,
q=option.q, qs=self.qs
)
if self.value == option.value:
return self.match_type(
self, exact_match=True,
q=option.q, qs=self.qs
)
exact_match = False
elif self.value == option.value:
exact_match = True
else:
raise NotAcceptable()

return self.match_type(
self, exact_match=exact_match,
q=option.q, qs=self.qs
)

def matches(self, accept):
best_match = None
Expand Down Expand Up @@ -360,7 +361,9 @@ class CharsetAccept(_Accept):


class CharsetMatch(_Match):
pass
@property
def charset(self):
return self._value


class Charset(_Value):
Expand Down
67 changes: 67 additions & 0 deletions verktyg/testsuite/test_http_accept.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,3 +246,70 @@ def test_serialize_accept_with_q(self):
def test_serialize_accept_redundant_q(self):
accept = http.CharsetAccept([('utf-8', '1')])
self.assertEqual(accept.to_header(), 'utf-8')

def test_serialize_accept_multiple(self):
accept = http.CharsetAccept([
'utf-8',
('ascii', 0.5),
('*', 0.1),
])
self.assertEqual(
accept.to_header(),
(
'utf-8,'
'ascii;q=0.5,'
'*;q=0.1'
)
)

def test_match_basic(self):
accept = http.CharsetAccept(['utf-8'])

acceptable = http.Charset('utf-8')
unacceptable = http.Charset('latin-1')

self.assertRaises(NotAcceptable, unacceptable.matches, accept)

match = acceptable.matches(accept)
self.assertEqual(acceptable, match.charset)
self.assertTrue(match.exact_match)

def test_match_wildcard(self):
accept = http.CharsetAccept(['*'])

charset = http.Charset('iso-8859-8')

match = charset.matches(accept)
self.assertEqual(charset, match.charset)
self.assertFalse(match.exact_match)

def test_match_quality(self):
accept = http.CharsetAccept([('utf-8', '0.5')])

no_qs = http.Charset('utf-8')
qs = http.Charset('utf-8', qs=0.5)

self.assertEqual(0.5, no_qs.matches(accept).quality)
self.assertEqual(0.25, qs.matches(accept).quality)

def test_match(self):
accept = http.CharsetAccept([
'utf-8',
('ascii', 0.5),
('*', 0.1),
])

charset = http.Charset('utf-8')
match = charset.matches(accept)
self.assertEqual(1.0, match.quality)
self.assertTrue(match.exact_match)

charset = http.Charset('ascii')
match = charset.matches(accept)
self.assertEqual(0.5, match.quality)
self.assertTrue(match.exact_match)

charset = http.Charset('latin-1')
match = charset.matches(accept)
self.assertEqual(0.1, match.quality)
self.assertFalse(match.exact_match)

0 comments on commit e8d10cc

Please sign in to comment.