diff --git a/.gitignore b/.gitignore index 3b3baaa6..457cab9f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ .pydevproject .settings *.pyc +venv cover dist proxy.py.egg-info diff --git a/Makefile b/Makefile index 422ea38b..7599b073 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: all clean package test +.PHONY: all clean test package release all: clean test diff --git a/proxy.py b/proxy.py index ae7baa91..36de7d42 100755 --- a/proxy.py +++ b/proxy.py @@ -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])) diff --git a/tests.py b/tests.py index d570b612..57d9bb8f 100644 --- a/tests.py +++ b/tests.py @@ -211,15 +211,15 @@ class TestHttpParser(unittest.TestCase): b'\n' + b'301 Moved', b'\n

301 Moved

\nThe document has moved\n' + - 'here.\r\n\r\n' + b'here.\r\n\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'\n' + - '301 Moved\n

301 Moved

\nThe document has moved\n' + - 'here.\r\n\r\n') + b'301 Moved\n

301 Moved

\nThe document has moved\n' + + b'here.\r\n\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'\n' + - '301 Moved') + b'301 Moved') self.assertEqual(self.parser.state, HTTP_PARSER_STATE_RCVING_BODY) self.parser.parse( b'\n

301 Moved

\nThe document has moved\n' + - 'here.\r\n\r\n') + b'here.\r\n\r\n') self.assertEqual(self.parser.state, HTTP_PARSER_STATE_COMPLETE) def test_chunked_response_parse(self):