Merge pull request #693 from kgriffs/remove-python-nocovers

test: Improve code coverage
This commit is contained in:
John Vrbanac
2016-01-27 10:17:02 -06:00
3 changed files with 27 additions and 12 deletions

View File

@@ -187,7 +187,7 @@ class Response(object):
if not is_ascii_encodable(value):
raise ValueError('"value" is not ascii encodable')
if PY2: # pragma: no cover
if PY2:
name = str(name)
value = str(value)
@@ -576,7 +576,7 @@ class Response(object):
if set_content_type:
headers['content-type'] = media_type
if py2: # pragma: no cover
if py2:
# PERF(kgriffs): Don't create an extra list object if
# it isn't needed.
items = headers.items()

View File

@@ -62,7 +62,7 @@ def format_range(value):
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."""
try:
s.encode("ascii")

View File

@@ -2,6 +2,7 @@ import re
import sys
from datetime import datetime, timedelta, tzinfo
import ddt
from six.moves.http_cookies import Morsel
from testtools.matchers import LessThan
@@ -10,6 +11,9 @@ import falcon.testing as testing
from falcon.util import TimezoneGMT, http_date_to_dt
UNICODE_TEST_STRING = u"Unicode_\xc3\xa6\xc3\xb8"
class TimezoneGMTPlus1(tzinfo):
def utcoffset(self, dt):
@@ -46,6 +50,7 @@ class CookieResource:
resp.unset_cookie("bad")
@ddt.ddt
class TestCookies(testing.TestBase):
#
@@ -163,26 +168,36 @@ class TestCookies(testing.TestBase):
def test_unicode_inside_ascii_range(self):
resp = falcon.Response()
# should be ok
resp.set_cookie("non_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(u"unicode_ascii_name_2", u"unicode_ascii_value")
def test_unicode_outside_ascii_range(self):
def set_bad_cookie_name():
resp = falcon.Response()
resp.set_cookie(u"unicode_\xc3\xa6\xc3\xb8", "ok_value")
self.assertRaises(KeyError, set_bad_cookie_name)
@ddt.data(
UNICODE_TEST_STRING,
UNICODE_TEST_STRING.encode('utf-8'),
42
)
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
# that it is not instance of UnicodeEncodeError, so
# we cannot simply use assertRaises
try:
set_bad_cookie_value()
resp.set_cookie("ok_name", value)
except ValueError as e:
self.assertIsInstance(e, ValueError)
self.assertNotIsInstance(e, UnicodeEncodeError)