def test_advocate_wrapper_futures(self): wrapper = RequestsAPIWrapper(validator=AddrValidator()) local_validator = AddrValidator(ip_whitelist={ ipaddress.ip_network("127.0.0.1"), }) local_wrapper = RequestsAPIWrapper(validator=local_validator) with self.assertRaises(UnacceptableAddressException): sess = wrapper.FuturesSession() sess.get("http://127.0.0.1/").result() with self.assertRaises(Exception) as cm: sess = local_wrapper.FuturesSession() sess.get("http://127.0.0.1:1/").result() # Check that we got a connection exception instead of a validation one # This might be either exception depending on the requests version self.assertRegexpMatches( cm.exception.__class__.__name__, r"\A(Connection|Protocol)Error", ) with self.assertRaises(UnacceptableAddressException): sess = wrapper.FuturesSession() sess.get("http://localhost:1/").result() with self.assertRaises(UnacceptableAddressException): sess = wrapper.FuturesSession() sess.get("https://localhost:1/").result()
def test_advocate_requests_api_wrapper_hostnames(self): wrapper = RequestsAPIWrapper( validator=AddrValidator(hostname_blacklist={"google.com"}, )) self.assertRaises( UnacceptableAddressException, wrapper.get, "https://google.com/", )
def test_advocate_requests_api_wrapper_req_methods(self): # Make sure all the convenience methods make requests with the correct # methods wrapper = RequestsAPIWrapper(AddrValidator()) request_methods = ("get", "options", "head", "post", "put", "patch", "delete") for method_name in request_methods: with requests_mock.mock() as request_mock: # This will fail if the request expected by `request_mock` # isn't sent when calling the wrapper method request_mock.request(method_name, "http://example.com/foo") getattr(wrapper, method_name)("http://example.com/foo")
def test_wrapper_getattr_fallback(self): # Make sure wrappers include everything in Advocate's `__init__.py` wrapper = RequestsAPIWrapper(AddrValidator()) self.assertIsNotNone(wrapper.PreparedRequest)
from advocate.connection import advocate_getaddrinfo from advocate.exceptions import ( MountDisabledException, NameserverException, UnacceptableAddressException, ) from advocate.packages import ipaddress from advocate.futures import FuturesSession # We use port 1 for testing because nothing is likely to legitimately listen # on it. AddrValidator.DEFAULT_PORT_WHITELIST.add(1) RequestsAPIWrapper.SUPPORT_WRAPPER_PICKLING = True global_wrapper = RequestsAPIWrapper(validator=AddrValidator(ip_whitelist={ ipaddress.ip_network("127.0.0.1"), })) RequestsAPIWrapper.SUPPORT_WRAPPER_PICKLING = False class _WrapperSubclass(global_wrapper.Session): def good_method(self): return "foo" def canonname_supported(): """Check if the nameserver supports the AI_CANONNAME flag travis-ci.org's Python 3 env doesn't seem to support it, so don't try any of the test that rely on it. """