Skip to main content

wsgi_intercept installs a WSGI application in place of a real URI for testing.

Project description

Installs a WSGI application in place of a real host for testing.

Introduction

Testing 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 a WSGI application importable by your test program. Thus, you can avoid spawning multiple processes or threads to test your Web app.

Supported Libaries

wsgi_intercept works with a variety of HTTP clients in Python 2.7, 3.5 and beyond, and in pypy.

  • urllib2

  • urllib.request

  • httplib

  • http.client

  • httplib2

  • requests

  • urllib3

How Does It Work?

wsgi_intercept works by replacing httplib.HTTPConnection with 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.Interceptor and 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 == 200

The interceptor class may aslo be used directly to install intercepts. See the module documentation for more information.

Older versions required that the functions add_wsgi_intercept(host, port, app_create_fn, script_name='') and remove_wsgi_intercept(host,port) be used to specify which URLs should be redirected into what applications. These methods are still available, but the Interceptor classes are likely easier to use for most use cases.

Install

pip install -U wsgi_intercept

Packages Intercepted

Unfortunately 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 for httplib2, urllib3 and requests in both Python 2 and 3, urllib2 and httplib in Python 2 and urllib.request and http.client in Python 3.

If you are using Python 2 and need support for a different HTTP client, require a version of wsgi_intercept<0.6. Earlier versions include support for webtest, webunit and zope.testbrowser.

The best way to figure out how to use interception is to inspect the tests. More comprehensive documentation available upon request.

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 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 is tested with Python 2.7, 3.5-3.8, and pypy and pypy3. It was assembled by Chris Dent. Testing and documentation improvements from Sasha Hart.

Project Home

This project lives on GitHub. Please submit all bugs, patches, failing tests, et cetera using the Issue Tracker.

Additional documentation is available on Read The Docs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wsgi_intercept-1.9.3.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

wsgi_intercept-1.9.3-py2.py3-none-any.whl (27.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wsgi_intercept-1.9.3.tar.gz.

File metadata

  • Download URL: wsgi_intercept-1.9.3.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.12

File hashes

Hashes for wsgi_intercept-1.9.3.tar.gz
Algorithm Hash digest
SHA256 0c4dc31dc7aabdd263ba45338c201b89e3096a3baaa7d8e3260c68ff18016fb4
MD5 c8eb16ef317f1b56b48799774a2de857
BLAKE2b-256 66c8836b590bc9e2a4a264b3007ea044d33b2ebd323ec06b141642884024e6bb

See more details on using hashes here.

File details

Details for the file wsgi_intercept-1.9.3-py2.py3-none-any.whl.

File metadata

  • Download URL: wsgi_intercept-1.9.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 27.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/57.4.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.8.12

File hashes

Hashes for wsgi_intercept-1.9.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f027a3e498f443454a1fa5e63502669ccd206c88be2e023f32f83b8ff613d471
MD5 e006fac14eb412a519dcc487987e4a88
BLAKE2b-256 790d7d804e8456be2475cb0b2483fe00bfaf73a8a4fb9d2dfeb4d705e87ef75f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page