Merge pull request #693 from kgriffs/remove-python-nocovers
test: Improve code coverage
This commit is contained in:
@@ -187,7 +187,7 @@ class Response(object):
|
|||||||
if not is_ascii_encodable(value):
|
if not is_ascii_encodable(value):
|
||||||
raise ValueError('"value" is not ascii encodable')
|
raise ValueError('"value" is not ascii encodable')
|
||||||
|
|
||||||
if PY2: # pragma: no cover
|
if PY2:
|
||||||
name = str(name)
|
name = str(name)
|
||||||
value = str(value)
|
value = str(value)
|
||||||
|
|
||||||
@@ -576,7 +576,7 @@ class Response(object):
|
|||||||
if set_content_type:
|
if set_content_type:
|
||||||
headers['content-type'] = media_type
|
headers['content-type'] = media_type
|
||||||
|
|
||||||
if py2: # pragma: no cover
|
if py2:
|
||||||
# PERF(kgriffs): Don't create an extra list object if
|
# PERF(kgriffs): Don't create an extra list object if
|
||||||
# it isn't needed.
|
# it isn't needed.
|
||||||
items = headers.items()
|
items = headers.items()
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ def format_range(value):
|
|||||||
return 'bytes %s-%s/%s' % (value[0], value[1], value[2])
|
return 'bytes %s-%s/%s' % (value[0], value[1], value[2])
|
||||||
|
|
||||||
|
|
||||||
def is_ascii_encodable(s): # pragma: no cover
|
def is_ascii_encodable(s):
|
||||||
"""Check if argument encodes to ascii without error."""
|
"""Check if argument encodes to ascii without error."""
|
||||||
try:
|
try:
|
||||||
s.encode("ascii")
|
s.encode("ascii")
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
from datetime import datetime, timedelta, tzinfo
|
from datetime import datetime, timedelta, tzinfo
|
||||||
|
|
||||||
|
import ddt
|
||||||
from six.moves.http_cookies import Morsel
|
from six.moves.http_cookies import Morsel
|
||||||
from testtools.matchers import LessThan
|
from testtools.matchers import LessThan
|
||||||
|
|
||||||
@@ -10,6 +11,9 @@ import falcon.testing as testing
|
|||||||
from falcon.util import TimezoneGMT, http_date_to_dt
|
from falcon.util import TimezoneGMT, http_date_to_dt
|
||||||
|
|
||||||
|
|
||||||
|
UNICODE_TEST_STRING = u"Unicode_\xc3\xa6\xc3\xb8"
|
||||||
|
|
||||||
|
|
||||||
class TimezoneGMTPlus1(tzinfo):
|
class TimezoneGMTPlus1(tzinfo):
|
||||||
|
|
||||||
def utcoffset(self, dt):
|
def utcoffset(self, dt):
|
||||||
@@ -46,6 +50,7 @@ class CookieResource:
|
|||||||
resp.unset_cookie("bad")
|
resp.unset_cookie("bad")
|
||||||
|
|
||||||
|
|
||||||
|
@ddt.ddt
|
||||||
class TestCookies(testing.TestBase):
|
class TestCookies(testing.TestBase):
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -163,26 +168,36 @@ class TestCookies(testing.TestBase):
|
|||||||
|
|
||||||
def test_unicode_inside_ascii_range(self):
|
def test_unicode_inside_ascii_range(self):
|
||||||
resp = falcon.Response()
|
resp = falcon.Response()
|
||||||
|
|
||||||
# should be ok
|
# should be ok
|
||||||
resp.set_cookie("non_unicode_ascii_name_1", "ascii_value")
|
resp.set_cookie("non_unicode_ascii_name_1", "ascii_value")
|
||||||
resp.set_cookie(u"unicode_ascii_name_1", "ascii_value")
|
resp.set_cookie(u"unicode_ascii_name_1", "ascii_value")
|
||||||
resp.set_cookie("non_unicode_ascii_name_2", u"unicode_ascii_value")
|
resp.set_cookie("non_unicode_ascii_name_2", u"unicode_ascii_value")
|
||||||
resp.set_cookie(u"unicode_ascii_name_2", u"unicode_ascii_value")
|
resp.set_cookie(u"unicode_ascii_name_2", u"unicode_ascii_value")
|
||||||
|
|
||||||
def test_unicode_outside_ascii_range(self):
|
@ddt.data(
|
||||||
def set_bad_cookie_name():
|
UNICODE_TEST_STRING,
|
||||||
resp = falcon.Response()
|
UNICODE_TEST_STRING.encode('utf-8'),
|
||||||
resp.set_cookie(u"unicode_\xc3\xa6\xc3\xb8", "ok_value")
|
42
|
||||||
self.assertRaises(KeyError, set_bad_cookie_name)
|
)
|
||||||
|
def test_non_ascii_name(self, name):
|
||||||
|
resp = falcon.Response()
|
||||||
|
self.assertRaises(KeyError, resp.set_cookie,
|
||||||
|
name, "ok_value")
|
||||||
|
|
||||||
|
@ddt.data(
|
||||||
|
UNICODE_TEST_STRING,
|
||||||
|
UNICODE_TEST_STRING.encode('utf-8'),
|
||||||
|
42
|
||||||
|
)
|
||||||
|
def test_non_ascii_value(self, value):
|
||||||
|
resp = falcon.Response()
|
||||||
|
|
||||||
def set_bad_cookie_value():
|
|
||||||
resp = falcon.Response()
|
|
||||||
resp.set_cookie("ok_name", u"unicode_\xc3\xa6\xc3\xb8")
|
|
||||||
# NOTE(tbug): we need to grab the exception to check
|
# NOTE(tbug): we need to grab the exception to check
|
||||||
# that it is not instance of UnicodeEncodeError, so
|
# that it is not instance of UnicodeEncodeError, so
|
||||||
# we cannot simply use assertRaises
|
# we cannot simply use assertRaises
|
||||||
try:
|
try:
|
||||||
set_bad_cookie_value()
|
resp.set_cookie("ok_name", value)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self.assertIsInstance(e, ValueError)
|
self.assertIsInstance(e, ValueError)
|
||||||
self.assertNotIsInstance(e, UnicodeEncodeError)
|
self.assertNotIsInstance(e, UnicodeEncodeError)
|
||||||
|
|||||||
Reference in New Issue
Block a user