pyriform

Connect the requests library to your WSGI app without using sockets.

Linking the Requests and WebTest libraries together, pyriform allows you to use the requests library to interact your WSGI app without needing to have it running on the network; it bonds these two web components together.

It’s useful for testing purposes, handles all standard HTTP methods (as well as custom ones), supports request timeouts. and is both Python 2 and 3 compatible.

Example Usage

Here’s an example with a small WSGI app (in this case, using CherryPy), and how we can use Pyriform to connect to it:

>>> # Create the WSGI app.
>>>
>>> import cherrypy
>>>
>>> class SayHello(object):
...
...     @cherrypy.expose
...     def default(self, word):
...         return "Hello %s from %s!" % (word, cherrypy.request.headers['X-Location'])
...
>>> cherrypy.config.update({'environment': 'embedded'})  # Suppress logging output.
>>> app = cherrypy.tree.mount(SayHello(), '/')
>>>
>>> # Now use Pyriform to map requests from a particular URL to this app.
>>>
>>> import pyriform
>>> import requests
>>> adapter = pyriform.WSGIAdapter(app)
>>> session = requests.Session()
>>> session.mount('http://helloapp/', adapter)
>>> resp = session.get('http://helloapp/World', headers={'X-Location': 'London'})
>>> print (resp.text)
Hello World from London!

Docs Release Version Python Version License Build Status Coverage Code Climate

Project Home

You can browse the source code and file issues at the project repository.

API

pyriform.make_session(app, prefix='http://')[source]

Convenience function for creating a session which maps the app to a particular URL.

If you need to have more control over the WSGIAdapter instance that’s created, or you need to generate a session in a different way, then you can just do it manually.

Parameters:
  • app (WSGI application) –

    The app to send requests to - this should be a callable which takes two arguments: environ and start_response.

    Alternatively, you can pass a TestApp object.

  • prefix (string) – The URL prefix to mount the app to. Defaults to http:// (e.g. for all HTTP traffic).
Returns:

A Session object which has the application mounted to the desired URL prefix.

class pyriform.WSGIAdapter(app, extra_environ=None, lint=False)[source]

A Requests adapter that will connect to a WSGI app.

This object should be mounted as a transport adapter to a Session object to have all matching requests sent to it; read more about using transport adapters here.

Parameters:
  • app (WSGI application) –

    The app to send requests to - this should be a callable which takes two arguments: environ and start_response.

    Alternatively, you can pass a TestApp object.

  • extra_environ (dict of string -> string) – Extra environment values that the WSGI app will inherit for every request that it handles.
  • lint (boolean) –

    Enables the webtest.lint module for the WSGI application.

    By default, this is disabled - it’s useful if you want to test the WSGI app itself, but not if you are testing client-side behaviour.

Alternatives

There is another library very similar to Pyriform called requests-wsgi-adapter; the API is mostly identical, so you can consider that library as an alternative.

However, version 0.3 of requests-wsgi-adapter (most recent at the time of writing) doesn’t support non-standard ports, request timeouts, custom status reasons or real-time streamed response content. However, Pyriform supports all of these. [*]

[*]This is based on running a number of tests in Pyriform’s test suite (which mostly uses httpbin as the test service) using requests-wsgi-adapter.

Indices and tables