From 00312ca0809c443e064ea4df63b5216e1e81bd22 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 28 Dec 2015 22:24:52 +0000 Subject: [PATCH] Add support for automatic domains If not host or url is passed to the Interceptor a uuid is generated for that context. --- test/test_interceptor.py | 10 ++++++++++ wsgi_intercept/interceptor.py | 8 +++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/test/test_interceptor.py b/test/test_interceptor.py index 5cd84c8..ca37ebd 100644 --- a/test/test_interceptor.py +++ b/test/test_interceptor.py @@ -107,6 +107,16 @@ def test_httplib2_interceptor_https_host(): assert 'WSGI intercept successful!' in content.decode('utf-8') +def test_httplib2_interceptor_no_host(): + # no hostname or port, one will be generated automatically + # we never actually know what it is + http = Http() + with Httplib2Interceptor(app=app) as url: + response, content = http.request(url) + assert response.status == 200 + assert 'WSGI intercept successful!' in content.decode('utf-8') + + def test_httplib2_interceptor_url(): hostname = str(uuid4()) port = 9999 diff --git a/wsgi_intercept/interceptor.py b/wsgi_intercept/interceptor.py index 1d99349..fd48c1a 100644 --- a/wsgi_intercept/interceptor.py +++ b/wsgi_intercept/interceptor.py @@ -2,6 +2,7 @@ """ from importlib import import_module +from uuid import uuid4 from six.moves.urllib import parse as urlparse @@ -17,11 +18,16 @@ class Interceptor(object): 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. """ def __init__(self, app, host=None, port=80, prefix=None, url=None): assert app - assert (host and port) or (url) + if (not host and not url): + host = str(uuid4()) self.app = app