Package: wsgi-intercept
Installs a WSGI application in place of a real host for testing.IntroductionTesting a WSGI application sometimes involves starting a server at a
local host and port, then pointing your test code to that address.
Instead, this library lets you intercept calls to any specific host/port
combination and redirect them into aWSGI applicationimportable by
your test program. Thus, you can avoid spawning multiple processes or
threads to test your Web app.Supported Libarieswsgi_interceptworks with a variety of HTTP clients in Python 2.7,
3.7 and beyond, and in pypy.urllib2urllib.requesthttplibhttp.clienthttplib2requestsurllib3 (<2.0.0, urllib3 2 support is in progress)How Does It Work?wsgi_interceptworks by replacinghttplib.HTTPConnectionwith a
subclass,wsgi_intercept.WSGI_HTTPConnection. This class then
redirects specific server/port combinations into a WSGI application by
emulating a socket. If no intercept is registered for the host and port
requested, those requests are passed on to the standard handler.The easiest way to use an intercept is to import an appropriate subclass
of~wsgi_intercept.interceptor.Interceptorand use that as a
context manager over web requests that use the library associated with
the subclass. For example:import httplib2
from wsgi_intercept.interceptor import Httplib2Interceptor
from mywsgiapp import app

def load_app():
    return app

http = httplib2.Http()
with Httplib2Interceptor(load_app, host='example.com', port=80) as url:
    response, content = http.request('%s%s' % (url, '/path'))
    assert response.status == 200The interceptor class may aslo be used directly to install intercepts.
See the module documentation for more information.Older versions required that the functionsadd_wsgi_intercept(host, port, app_create_fn,script_name='')andremove_wsgi_intercept(host,port)be used to specify which URLs should be redirected into what applications.
These methods are still available, but theInterceptorclasses are likely
easier to use for most use cases.Noteapp_create_fnis afunction objectreturning a WSGI
application;script_namebecomesSCRIPT_NAMEin the WSGI
app’s environment, if set.NoteIfhttp_proxyorhttps_proxyis set in the environment
this can cause difficulties with some of the intercepted libraries.
If requests or urllib is being used, these will raise an exception
if one of those variables is set.NoteIfwsgi_intercept.STRICT_RESPONSE_HEADERSis set toTruethen response headers sent by an application will be checked to
make sure they are of the typestrnative to the version of
Python, as required by pep 3333. The default isFalse(to
preserve backwards compatibility)Installpip install -U wsgi_interceptPackages InterceptedUnfortunately each of the HTTP client libraries use their own specific
mechanism for making HTTP call-outs, so individual implementations are
needed. At this time there are implementations forhttplib2,urllib3(<2.0.0) andrequestsin both Python 2 and 3,urllib2andhttplibin Python 2 andurllib.requestandhttp.clientin Python 3.If you are using Python 2 and need support for a different HTTP
client, require a version ofwsgi_intercept<0.6. Earlier versions
include support forwebtest,webunitandzope.testbrowser.The best way to figure out how to use interception is to inspectthe tests. More comprehensive documentation available upon
request.HistoryPursuant to Ian Bicking’s“best Web testing framework”post, Titus
Brown put together anin-process HTTP-to-WSGI interception mechanismfor 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 theotherPython Web testing frameworks.The Python 2 version of wsgi-intercept was the result. Kumar McMillan
later took over maintenance.The current version is tested with Python 2.7, 3.5-3.11, and pypy and pypy3.
It was assembled byChris Dent. Testing and documentation improvements
fromSasha Hart.Project HomeThis project lives onGitHub. Please submit all bugs, patches,
failing tests, et cetera using the Issue Tracker.Additional documentation is available onRead The Docs.