Interceptor

Context manager based WSGI interception.

class wsgi_intercept.interceptor.HttpClientInterceptor(app, host=None, port=80, prefix=None, url=None)

Interceptor for httplib and http.client.

class wsgi_intercept.interceptor.Httplib2Interceptor(app, host=None, port=80, prefix=None, url=None)

Interceptor for httplib2.

class wsgi_intercept.interceptor.Interceptor(app, host=None, port=80, prefix=None, url=None)

A convenience class over the guts of wsgi_intercept.

An Interceptor subclass provides a clean entry point to the wsgi_intercept functionality in two ways: by encapsulating the interception addition and removal in methods and by providing a context manager that automates the process of addition and removal.

Each Interceptor subclass is associated with a specific http library.

Each class may be passed a url or a host and a port. If no args are passed a hostname will be automatically generated and the resulting url will be returned by the context manager.

class wsgi_intercept.interceptor.RequestsInterceptor(app, host=None, port=80, prefix=None, url=None)

Interceptor for requests.

class wsgi_intercept.interceptor.Urllib3Interceptor(app, host=None, port=80, prefix=None, url=None)

Interceptor for requests.

class wsgi_intercept.interceptor.UrllibInterceptor(app, host=None, port=80, prefix=None, url=None)

Interceptor for urllib2 and urllib.request.

Example using httplib2, others are much the same:

import httplib2
from wsgi_intercept.interceptor import Httplib2Interceptor


def app(environ, start_response):
    start_response('200 OK', [('Content-Type', 'text/plain')])
    return [b'Whee']


def make_app():
    return app


http = httplib2.Http()
with Httplib2Interceptor(make_app, host='localhost', port=80) as url:
    resp, content = http.request(url)
    assert content == b'Whee'