From 2504de1f73525ae7bedb476aaa0957113cd37c08 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Thu, 22 Sep 2016 19:27:01 +0100 Subject: [PATCH] pep8 fixes to get pep8 tox target passing This is basic flake8 testing without any 'hacking' extensions. We may wish to add those later, but for the time being this code is so much of its own particular style that that would be painful. In any case, the hacking rules aren't that great. In the process some documentation adjustments were made while fixing line lenghts. A dead link was removed. --- .travis.yml | 3 +-- setup.py | 4 +++- test/test_interceptor.py | 1 + test/test_module_interceptor.py | 2 ++ test/test_urllib3.py | 16 ++++++++++------ test/test_wsgi_compliance.py | 8 +++++++- wsgi_intercept/__init__.py | 21 ++++++++++++--------- wsgi_intercept/_urllib3.py | 3 --- 8 files changed, 36 insertions(+), 22 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0a80c71..1634cf9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,8 +12,7 @@ env: - TOXENV=py34 - TOXENV=py35 - TOXENV=pypy - # XXX: known to fail, fixes to follow - #- TOXENV=pep8 + - TOXENV=pep8 - TOXENV=docs notifications: diff --git a/setup.py b/setup.py index 7da6bfe..377c179 100644 --- a/setup.py +++ b/setup.py @@ -23,7 +23,9 @@ META = { 'version': wsgi_intercept.__version__, 'author': 'Titus Brown, Kumar McMillan, Chris Dent, Sasha Hart', 'author_email': 'cdent@peermore.com', - 'description': 'wsgi_intercept installs a WSGI application in place of a real URI for testing.', + 'description': + 'wsgi_intercept installs a WSGI application in place of a ' + 'real URI for testing.', # What will the name be? 'url': 'http://pypi.python.org/pypi/wsgi_intercept', 'long_description': wsgi_intercept.__doc__, diff --git a/test/test_interceptor.py b/test/test_interceptor.py index ba52de5..5d0cac2 100644 --- a/test/test_interceptor.py +++ b/test/test_interceptor.py @@ -26,6 +26,7 @@ from .wsgi_app import simple_app httppool = urllib3.PoolManager() + def app(): return simple_app diff --git a/test/test_module_interceptor.py b/test/test_module_interceptor.py index 837c73d..c5f6692 100644 --- a/test/test_module_interceptor.py +++ b/test/test_module_interceptor.py @@ -23,6 +23,7 @@ def teardown_module(module): def test_simple_request(): + global host http = Http() response, content = http.request('http://%s/' % host) assert response.status == 200 @@ -30,6 +31,7 @@ def test_simple_request(): def test_another_request(): + global host http = Http() response, content = http.request('http://%s/foobar' % host) assert response.status == 200 diff --git a/test/test_urllib3.py b/test/test_urllib3.py index ccf1e40..93a45a4 100644 --- a/test/test_urllib3.py +++ b/test/test_urllib3.py @@ -1,6 +1,5 @@ import os import py.test -import socket from wsgi_intercept import urllib3_intercept, WSGIAppError from test import wsgi_app from test.install import installer_class, skipnetwork @@ -14,7 +13,8 @@ http = urllib3.PoolManager() def test_http(): with InstalledApp(wsgi_app.simple_app, host=HOST, port=80) as app: - resp = http.request('GET', 'http://some_hopefully_nonexistant_domain:80/') + resp = http.request( + 'GET', 'http://some_hopefully_nonexistant_domain:80/') assert resp.data == b'WSGI intercept successful!\n' assert app.success() @@ -28,7 +28,8 @@ def test_http_default_port(): def test_http_other_port(): with InstalledApp(wsgi_app.simple_app, host=HOST, port=8080) as app: - resp = http.request('GET', 'http://some_hopefully_nonexistant_domain:8080/') + resp = http.request( + 'GET', 'http://some_hopefully_nonexistant_domain:8080/') assert resp.data == b'WSGI intercept successful!\n' assert app.success() environ = app.get_internals() @@ -39,7 +40,8 @@ def test_bogus_domain(): with InstalledApp(wsgi_app.simple_app, host=HOST, port=80): py.test.raises( urllib3.exceptions.ProtocolError, - 'http.request("GET", "http://_nonexistant_domain_", retries=False)') + 'http.request("GET", "http://_nonexistant_domain_", ' + 'retries=False)') def test_proxy_handling(): @@ -56,14 +58,16 @@ def test_proxy_handling(): def test_https(): with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app: - resp = http.request('GET', 'https://some_hopefully_nonexistant_domain:443/') + resp = http.request( + 'GET', 'https://some_hopefully_nonexistant_domain:443/') assert resp.data == b'WSGI intercept successful!\n' assert app.success() def test_https_default_port(): with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app: - resp = http.request('GET', 'https://some_hopefully_nonexistant_domain/') + resp = http.request( + 'GET', 'https://some_hopefully_nonexistant_domain/') assert resp.data == b'WSGI intercept successful!\n' assert app.success() environ = app.get_internals() diff --git a/test/test_wsgi_compliance.py b/test/test_wsgi_compliance.py index 654b23c..bf12ef4 100644 --- a/test/test_wsgi_compliance.py +++ b/test/test_wsgi_compliance.py @@ -60,7 +60,13 @@ def test_more_interesting(): assert internal_env['QUERY_STRING'] == 'bar=baz%20zoom' assert internal_env['HTTP_ACCEPT'] == 'application/json' assert internal_env['HTTP_COOKIE'] == 'foo=bar' - assert type(internal_env['HTTP_COOKIE']) == type('') + # In this test we are ensuring the value, in the environ, of + # a request header has a value which is a str, as native to + # that version of Python. That means always a str, despite + # the fact that a str in Python 2 and 3 are different + # things! PEP 3333 requires this. isinstance is not used to + # avoid confusion over class hierarchies. + assert type(internal_env['HTTP_COOKIE']) == type('') # noqa E721 # Do the rather painful wsgi encoding dance. if sys.version_info[0] > 2: diff --git a/wsgi_intercept/__init__.py b/wsgi_intercept/__init__.py index 1ae41b5..8f744a2 100644 --- a/wsgi_intercept/__init__.py +++ b/wsgi_intercept/__init__.py @@ -1,4 +1,3 @@ - """Installs a WSGI application in place of a real host for testing. Introduction @@ -89,19 +88,21 @@ History Pursuant to Ian Bicking's `"best Web testing framework"`_ post, Titus Brown put together an `in-process HTTP-to-WSGI interception mechanism`_ -for his own Web testing system, twill_. Because the mechanism is pretty +for his own Web testing system, twill. Because the mechanism is pretty generic -- it works at the httplib level -- Titus decided to try adding it into all of the *other* Python Web testing frameworks. The Python 2 version of wsgi-intercept was the result. Kumar McMillan later took over maintenance. -The current version works with Python 2.7, 3.3, 3.4 and 3.5 and was assembled -by `Chris Dent`_. Testing and documentation improvements from `Sasha Hart`_. +The current version is tested with Python 2.7, 3.3, 3.4, 3.5 and pypy +and was assembled by `Chris Dent`_. Testing and documentation improvements +from `Sasha Hart`_. -.. _twill: http://www.idyll.org/~t/www-tools/twill.html -.. _"best Web testing framework": http://blog.ianbicking.org/best-of-the-web-app-test-frameworks.html -.. _in-process HTTP-to-WSGI interception mechanism: http://www.advogato.org/person/titus/diary.html?start=119 +.. _"best Web testing framework": + http://blog.ianbicking.org/best-of-the-web-app-test-frameworks.html +.. _in-process HTTP-to-WSGI interception mechanism: + http://www.advogato.org/person/titus/diary.html?start=119 .. _WSGI application: http://www.python.org/peps/pep-3333.html .. _Chris Dent: https://github.com/cdent .. _Sasha Hart: https://github.com/sashahart @@ -120,10 +121,13 @@ Additional documentation is available on `Read The Docs`_. """ from __future__ import print_function +import sys +import traceback + + __version__ = '1.3.2' -import sys try: from http.client import HTTPConnection, HTTPSConnection except ImportError: @@ -139,7 +143,6 @@ try: except ImportError: from urllib import unquote as url_unquote -import traceback debuglevel = 0 # 1 basic diff --git a/wsgi_intercept/_urllib3.py b/wsgi_intercept/_urllib3.py index 4661b43..8afcbf9 100644 --- a/wsgi_intercept/_urllib3.py +++ b/wsgi_intercept/_urllib3.py @@ -19,7 +19,6 @@ def make_urllib3_override(HTTPConnectionPool, HTTPSConnectionPool, WSGI_HTTPConnection.__init__(self, *args, **kwargs) HTTPConnection.__init__(self, *args, **kwargs) - class HTTPS_WSGIInterceptor(WSGI_HTTPSConnection, HTTPSConnection): is_verified = True @@ -29,7 +28,6 @@ def make_urllib3_override(HTTPConnectionPool, HTTPSConnectionPool, WSGI_HTTPSConnection.__init__(self, *args, **kwargs) HTTPSConnection.__init__(self, *args, **kwargs) - def install(): if 'http_proxy' in os.environ or 'https_proxy' in os.environ: raise RuntimeError( @@ -37,7 +35,6 @@ def make_urllib3_override(HTTPConnectionPool, HTTPSConnectionPool, HTTPConnectionPool.ConnectionCls = HTTP_WSGIInterceptor HTTPSConnectionPool.ConnectionCls = HTTPS_WSGIInterceptor - def uninstall(): HTTPConnectionPool.ConnectionCls = HTTPConnection HTTPSConnectionPool.ConnectionCls = HTTPSConnection