reduce InstalledApp boilerplate
test modules don't have to subclass BaseInstalledApp
This commit is contained in:
@@ -1,15 +1,30 @@
|
||||
import wsgi_intercept
|
||||
|
||||
|
||||
class BaseInstalledApp(object):
|
||||
def __init__(self, app, host, port=80, script_name=''):
|
||||
def __init__(self, app, host, port=80, script_name='',
|
||||
install=None, uninstall=None):
|
||||
self.app = app
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.script_name = script_name
|
||||
self._install = install or (lambda: None)
|
||||
self._uninstall = uninstall or (lambda: None)
|
||||
|
||||
def install_wsgi_intercept(self):
|
||||
wsgi_intercept.add_wsgi_intercept(
|
||||
self.host, self.port, self.factory, script_name=self.script_name)
|
||||
|
||||
def uninstall_wsgi_intercept(self):
|
||||
wsgi_intercept.remove_wsgi_intercept(self.host, self.port)
|
||||
|
||||
def install(self):
|
||||
raise NotImplementedError()
|
||||
self._install()
|
||||
self.install_wsgi_intercept()
|
||||
|
||||
def uninstall(self):
|
||||
raise NotImplementedError()
|
||||
self.uninstall_wsgi_intercept()
|
||||
self._uninstall()
|
||||
|
||||
def factory(self):
|
||||
return self.app
|
||||
@@ -20,3 +35,17 @@ class BaseInstalledApp(object):
|
||||
|
||||
def __exit__(self, *args, **kwargs):
|
||||
self.uninstall()
|
||||
|
||||
|
||||
def installer_class(module=None, install=None, uninstall=None):
|
||||
if module:
|
||||
install = install or getattr(module, 'install', None)
|
||||
uninstall = uninstall or getattr(module, 'uninstall', None)
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def __init__(self, app, host, port=80, script_name=''):
|
||||
BaseInstalledApp.__init__(
|
||||
self, app=app, host=host, port=port, script_name=script_name,
|
||||
install=install, uninstall=uninstall)
|
||||
|
||||
return InstalledApp
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import py.test
|
||||
import wsgi_intercept
|
||||
from wsgi_intercept import http_client_intercept
|
||||
from wsgi_intercept import http_client_intercept, WSGIAppError
|
||||
from test import wsgi_app
|
||||
from test.install import BaseInstalledApp
|
||||
from test.install import installer_class
|
||||
try:
|
||||
import http.client as http_lib
|
||||
except ImportError:
|
||||
@@ -10,15 +9,7 @@ except ImportError:
|
||||
|
||||
HOST = 'some_hopefully_nonexistant_domain'
|
||||
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def install(self):
|
||||
http_client_intercept.install()
|
||||
wsgi_intercept.add_wsgi_intercept(self.host, self.port, self.factory)
|
||||
|
||||
def uninstall(self):
|
||||
wsgi_intercept.remove_wsgi_intercept(self.host, self.port)
|
||||
http_client_intercept.uninstall()
|
||||
InstalledApp = installer_class(http_client_intercept)
|
||||
|
||||
|
||||
def test_http_success():
|
||||
@@ -45,6 +36,6 @@ def test_app_error():
|
||||
mock_app = wsgi_app.MockWSGIApp(wsgi_app.raises_app)
|
||||
with InstalledApp(mock_app, host=HOST, port=80):
|
||||
http_client = http_lib.HTTPConnection(HOST)
|
||||
with py.test.raises(wsgi_intercept.WSGIAppError):
|
||||
with py.test.raises(WSGIAppError):
|
||||
http_client.request('GET', '/')
|
||||
http_client.getresponse().read()
|
||||
|
||||
@@ -1,22 +1,13 @@
|
||||
import py.test
|
||||
import wsgi_intercept
|
||||
from wsgi_intercept import httplib2_intercept
|
||||
from wsgi_intercept import httplib2_intercept, WSGIAppError
|
||||
from test import wsgi_app
|
||||
from test.install import BaseInstalledApp
|
||||
from test.install import installer_class
|
||||
import httplib2
|
||||
from socket import gaierror
|
||||
|
||||
HOST = 'some_hopefully_nonexistant_domain'
|
||||
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def install(self):
|
||||
httplib2_intercept.install()
|
||||
wsgi_intercept.add_wsgi_intercept(self.host, self.port, self.factory)
|
||||
|
||||
def uninstall(self):
|
||||
wsgi_intercept.remove_wsgi_intercept(self.host, self.port)
|
||||
httplib2_intercept.uninstall()
|
||||
InstalledApp = installer_class(httplib2_intercept)
|
||||
|
||||
|
||||
def test_success():
|
||||
@@ -49,6 +40,6 @@ def test_app_error():
|
||||
mock_app = wsgi_app.MockWSGIApp(wsgi_app.raises_app)
|
||||
with InstalledApp(mock_app, host=HOST, port=80):
|
||||
http = httplib2.Http()
|
||||
with py.test.raises(wsgi_intercept.WSGIAppError):
|
||||
with py.test.raises(WSGIAppError):
|
||||
http.request(
|
||||
'http://some_hopefully_nonexistant_domain:80/')
|
||||
|
||||
@@ -1,21 +1,13 @@
|
||||
import py.test
|
||||
import wsgi_intercept
|
||||
from wsgi_intercept import requests_intercept
|
||||
from wsgi_intercept import requests_intercept, WSGIAppError
|
||||
from test import wsgi_app
|
||||
from test.install import BaseInstalledApp
|
||||
from test.install import installer_class
|
||||
import requests
|
||||
from requests.exceptions import ConnectionError
|
||||
|
||||
HOST = 'some_hopefully_nonexistant_domain'
|
||||
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def install(self):
|
||||
requests_intercept.install()
|
||||
wsgi_intercept.add_wsgi_intercept(self.host, self.port, self.factory)
|
||||
|
||||
def uninstall(self):
|
||||
requests_intercept.uninstall()
|
||||
InstalledApp = installer_class(requests_intercept)
|
||||
|
||||
|
||||
def test_success():
|
||||
@@ -45,5 +37,5 @@ def test_https_success():
|
||||
def test_app_error():
|
||||
mock_app = wsgi_app.MockWSGIApp(wsgi_app.raises_app)
|
||||
with InstalledApp(mock_app, host=HOST, port=80):
|
||||
with py.test.raises(wsgi_intercept.WSGIAppError):
|
||||
with py.test.raises(WSGIAppError):
|
||||
requests.get('http://some_hopefully_nonexistant_domain:80/')
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
import py.test
|
||||
import wsgi_intercept
|
||||
from wsgi_intercept import urllib_intercept
|
||||
from wsgi_intercept import urllib_intercept, WSGIAppError
|
||||
from test import wsgi_app
|
||||
from test.install import BaseInstalledApp
|
||||
from test.install import installer_class
|
||||
try:
|
||||
import urllib.request as url_lib
|
||||
except ImportError:
|
||||
@@ -10,14 +9,7 @@ except ImportError:
|
||||
|
||||
HOST = 'some_hopefully_nonexistant_domain'
|
||||
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def install(self):
|
||||
urllib_intercept.install_opener()
|
||||
wsgi_intercept.add_wsgi_intercept(self.host, self.port, self.factory)
|
||||
|
||||
def uninstall(self):
|
||||
wsgi_intercept.remove_wsgi_intercept(self.host, self.port)
|
||||
InstalledApp = installer_class(install=urllib_intercept.install_opener)
|
||||
|
||||
|
||||
def test_http():
|
||||
@@ -51,5 +43,5 @@ def test_https_default_port():
|
||||
def test_app_error():
|
||||
mock_app = wsgi_app.MockWSGIApp(wsgi_app.raises_app)
|
||||
with InstalledApp(mock_app, host=HOST, port=80):
|
||||
with py.test.raises(wsgi_intercept.WSGIAppError):
|
||||
with py.test.raises(WSGIAppError):
|
||||
url_lib.urlopen('http://some_hopefully_nonexistant_domain:80/')
|
||||
|
||||
@@ -1,23 +1,13 @@
|
||||
import sys
|
||||
import py.test
|
||||
import wsgi_intercept
|
||||
from wsgi_intercept import httplib2_intercept
|
||||
from test import wsgi_app
|
||||
from test.install import BaseInstalledApp
|
||||
from test.install import installer_class
|
||||
import httplib2
|
||||
|
||||
HOST = 'some_hopefully_nonexistant_domain'
|
||||
|
||||
|
||||
class InstalledApp(BaseInstalledApp):
|
||||
def install(self):
|
||||
httplib2_intercept.install()
|
||||
wsgi_intercept.add_wsgi_intercept(
|
||||
self.host, self.port, self.factory, script_name=self.script_name)
|
||||
|
||||
def uninstall(self):
|
||||
wsgi_intercept.remove_wsgi_intercept(self.host, self.port)
|
||||
httplib2_intercept.uninstall()
|
||||
InstalledApp = installer_class(httplib2_intercept)
|
||||
|
||||
|
||||
def test_simple_override():
|
||||
|
||||
Reference in New Issue
Block a user