Fix Python3 warnings thrown while running tests

1. can't concat str to bytes
2. ResourceWarning: unclosed
This commit is contained in:
Abhinav Singh 2018-12-10 14:43:44 +05:30
parent 4fde72fd10
commit 917564b1d2
4 changed files with 17 additions and 12 deletions

1
.gitignore vendored
View File

@ -4,6 +4,7 @@
.pydevproject
.settings
*.pyc
venv
cover
dist
proxy.py.egg-info

View File

@ -1,4 +1,4 @@
.PHONY: all clean package test
.PHONY: all clean test package release
all: clean test

View File

@ -29,28 +29,28 @@ __license__ = 'BSD'
logger = logging.getLogger(__name__)
# True if we are running on Python 3.
if sys.version_info[0] == 3:
if sys.version_info[0] == 3: # pragma: no cover
text_type = str
binary_type = bytes
from urllib import parse as urlparse
else:
else: # pragma: no cover
text_type = unicode
binary_type = str
import urlparse
def text_(s, encoding='utf-8', errors='strict'):
def text_(s, encoding='utf-8', errors='strict'): # pragma: no cover
""" If ``s`` is an instance of ``binary_type``, return
``s.decode(encoding, errors)``, otherwise return ``s``"""
if isinstance(s, binary_type):
return s.decode(encoding, errors)
return s # pragma: no cover
return s
def bytes_(s, encoding='utf-8', errors='strict'):
def bytes_(s, encoding='utf-8', errors='strict'): # pragma: no cover
""" If ``s`` is an instance of ``text_type``, return
``s.encode(encoding, errors)``, otherwise return ``s``"""
if isinstance(s, text_type): # pragma: no cover
if isinstance(s, text_type):
return s.encode(encoding, errors)
return s
@ -314,6 +314,10 @@ class Server(Connection):
super(Server, self).__init__(b'server')
self.addr = (host, int(port))
def __del__(self):
if self.conn:
self.close()
def connect(self):
self.conn = socket.create_connection((self.addr[0], self.addr[1]))

View File

@ -211,15 +211,15 @@ class TestHttpParser(unittest.TestCase):
b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n' +
b'<TITLE>301 Moved</TITLE></HEAD>',
b'<BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n'
]))
self.assertEqual(self.parser.code, b'301')
self.assertEqual(self.parser.reason, b'Moved Permanently')
self.assertEqual(self.parser.version, b'HTTP/1.1')
self.assertEqual(self.parser.body,
b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n' +
'<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n')
b'<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n')
self.assertDictContainsSubset({b'content-length': (b'Content-Length', b'219')}, self.parser.headers)
self.assertEqual(self.parser.state, HTTP_PARSER_STATE_COMPLETE)
@ -243,11 +243,11 @@ class TestHttpParser(unittest.TestCase):
self.assertEqual(self.parser.state, HTTP_PARSER_STATE_HEADERS_COMPLETE)
self.parser.parse(
b'<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n' +
'<TITLE>301 Moved</TITLE></HEAD>')
b'<TITLE>301 Moved</TITLE></HEAD>')
self.assertEqual(self.parser.state, HTTP_PARSER_STATE_RCVING_BODY)
self.parser.parse(
b'<BODY>\n<H1>301 Moved</H1>\nThe document has moved\n' +
'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n')
b'<A HREF="http://www.google.com/">here</A>.\r\n</BODY></HTML>\r\n')
self.assertEqual(self.parser.state, HTTP_PARSER_STATE_COMPLETE)
def test_chunked_response_parse(self):