Class Documentation

Proxy

This module contains a single class - Proxy - which implements proxying of requests (and adding CORS headers). It is intended to run under a WSGI webserver. See class documentation for additional details.

class CORSProxy.CORSProxy.Proxy(server_name, server_port=0, target_protocol='', allow_from=False, auth=None, add_headers=None)[source]

A (relatively) sinple class to perform HTTP request proxying and add COR headers to the response.

A simple way to run this is (adjusting hosts and ports, of course):

>>> from wsgiref.simple_server import make_server
>>> myproxy = CORSProxy("localhost",8081,allow_from=True)
>>> server = make_server('127.0.0.1', 8080, myproxy)
>>> server.handle_request()
proxy_start_response(status, headers)[source]

A wrapper around the default start_response function. Designed to work around limitations of various servers and to massage any headers required in the response.

If you have some special work that needs to be done on the response, override this function. Please be sure to call the original as well!

Test Documentation

test_CORSProxy

Tests to cover the CORSProxy.Proxy class.

class test_CORSProxy.Response[source]

A simple wrapper to keep track of response details.

test_CORSProxy.get_default_environ()[source]

Fetches an approximation of the environment object used by WSGI servers.

class test_CORSProxy.test_CORSProxy(methodName='runTest')[source]

A class encompasing tests for the CORSProxy.Proxy class

setUp()[source]

See unittest module for more details. Generic setup for every test.

simulate_called(environ, start_response)[source]

Simulates the side effects of calling the wsgiproxy.exactproxy class. Mostly this is just storing the environment.

simulate_start_response(status, headers)[source]

Simulates the start_response function. Stores the status and headers passed to it.

test_ACAO_origin_list_match()[source]

Tests that the correct ACAO header is added if allow_from is a list of domains and an origin is specified.

test_ACAO_origin_list_no_match()[source]

Tests that the expected ACAO header is added if allow_from is a list and the origin header is present, but not one of the named domains.

test_add_headers()[source]

Tests that user-added headers are sent in the request.

test_added_ACAO_False_origin()[source]

Tests that the ACAO header is not added if allow_from is False.

test_added_ACAO_True_origin()[source]

Tests that the appropriate ACAO header is added if allow_from=True and the request has an Origin header.

test_added_ACAO_star_no_origin()[source]

Tests that the appropriate ACAO header is added if allow_from is True and the request did not have an Origin header.

test_added_ACAO_star_origin()[source]

Tests that the correct ACAO header is added if allow_from=”*” and the request has an origin header.

test_auth_false()[source]

Tests that authentication behaves as expected (returns 401) when the authentication functon returns False

test_auth_message()[source]

Tests that authentication behaves as expected (returns 401 and an error string) when the authentication function returns a string.

test_auth_true()[source]

Tests that authentication behaves as intended when the passed auth function returns True

test_bad_target_protocol()[source]

Tests that the Proxy class raises a ValueError if the user specified an invalid target_protocol

test_bad_url_scheme_from_server()[source]

Tests that bad protocols from the server are caught and handled when the user did not specify a target_protocol. Alternatively, tests that the user cannot force the use of a different protocol by fussing with class innards.

test_basic()[source]

Tests basic functionality of the class and test harness. The simplest possible code path.

test_cant_fall_through_proto()[source]

Tests that even odd capitolization behaves as intended in the target_protocol parsing.

test_dont_remove_bad_headers()[source]

Tests that headers are not altered if not using the wsgiref server.

test_fix_environ_proto_odd_caps()[source]

Tests that the Proxy class fixes up capitalization if the server passes in an oddly-capitalized protocol.

test_fix_user_forced_proto_odd_caps()[source]

Tests that the Proxy class behaves as intended even if the user forces odd protocol capitalization after the initializer.

test_force_http_downgrade()[source]

Tests that the request is downgraded to http if the incoming request was https and the user specified target_protocol=http

test_force_https_upgrade()[source]

Tests that the request is upgraded to https is the incoming request was http and the user specified target_protocol=https

test_keep_http()[source]

Verifies that the http protocol is maintained when the request used it and the user did not specify a target_protocol

test_keep_https()[source]

Verifies that the https protocol is maintained when the request used it and the user did not specify a target_protocol

test_keep_odd_port()[source]

Tests that the Proxy class will not override a user-specified port.

test_pick_port_http()[source]

Tests that the appropriate port for http is picked if not specified.

test_pick_port_https()[source]

Tests that the appropriate port for https is used if not specified.

test_remove_bad_headers()[source]

Tests that headers not allowed by the wsgiref server are stripped from the response.

test_update_host_server()[source]

Tests that the Proxy class properly updates the environment to target the remote API.