1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
import os
import pytest
from wsgi_intercept import urllib3_intercept, WSGIAppError
from . import wsgi_app
from .install import installer_class, skipnetwork
import urllib3
HOST = 'some_hopefully_nonexistant_domain'
InstalledApp = installer_class(urllib3_intercept)
http = urllib3.PoolManager()
def test_http():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80) as app:
resp = http.request(
'GET', 'http://some_hopefully_nonexistant_domain:80/')
assert resp.data == b'WSGI intercept successful!\n'
assert app.success()
def test_http_default_port():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80) as app:
resp = http.request('GET', 'http://some_hopefully_nonexistant_domain/')
assert resp.data == b'WSGI intercept successful!\n'
assert app.success()
def test_http_other_port():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=8080) as app:
resp = http.request(
'GET', 'http://some_hopefully_nonexistant_domain:8080/')
assert resp.data == b'WSGI intercept successful!\n'
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'http'
def test_bogus_domain():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80):
with pytest.raises(urllib3.exceptions.ProtocolError):
http.request("GET", "http://_nonexistant_domain_", retries=False)
def test_proxy_handling():
with pytest.raises(RuntimeError) as exc:
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80,
proxy='some_proxy.com:1234'):
http.request('GET', 'http://some_hopefully_nonexistant_domain:80/')
assert 'http_proxy or https_proxy set in environment' in str(exc.value)
# We need to do this by hand because the exception was raised
# during the entry of the context manager, so the exit handler
# wasn't reached.
del os.environ['http_proxy']
def test_https():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app:
resp = http.request(
'GET', 'https://some_hopefully_nonexistant_domain:443/')
assert resp.data == b'WSGI intercept successful!\n'
assert app.success()
def test_https_default_port():
with InstalledApp(wsgi_app.simple_app, host=HOST, port=443) as app:
resp = http.request(
'GET', 'https://some_hopefully_nonexistant_domain/')
assert resp.data == b'WSGI intercept successful!\n'
assert app.success()
environ = app.get_internals()
assert environ['wsgi.url_scheme'] == 'https'
def test_socket_options():
http = urllib3.PoolManager(socket_options=[])
with InstalledApp(wsgi_app.simple_app, host=HOST, port=80):
http.request('GET', 'http://some_hopefully_nonexistant_domain/')
with InstalledApp(wsgi_app.simple_app, host=HOST, port=443):
http.request('GET', 'https://some_hopefully_nonexistant_domain/')
def test_app_error():
with InstalledApp(wsgi_app.raises_app, host=HOST, port=80):
with pytest.raises(WSGIAppError):
http.request('GET', 'http://some_hopefully_nonexistant_domain/')
@skipnetwork
def test_http_not_intercepted():
with InstalledApp(wsgi_app.raises_app, host=HOST, port=80):
resp = http.request('GET', 'http://google.com')
assert resp.status >= 200 and resp.status < 300
@skipnetwork
def test_https_not_intercepted():
with InstalledApp(wsgi_app.raises_app, host=HOST, port=80):
resp = http.request('GET', 'https://google.com')
assert resp.status >= 200 and resp.status < 300
|