Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 36 additions & 3 deletions pytest_blockage.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,21 @@ class MockSmtpCall(Exception):
pass


def block_http(whitelist):
def http_fail(host):
logger.warning('Denied HTTP connection to: %s' % host)
raise MockHttpCall(host)


def block_http_httplib(whitelist):
def whitelisted(self, host, *args, **kwargs):
try:
string_type = basestring
except NameError:
# python3
string_type = str
if isinstance(host, string_type) and host not in whitelist:
logger.warning('Denied HTTP connection to: %s' % host)
raise MockHttpCall(host)
http_fail(host)

logger.debug('Allowed HTTP connection to: %s' % host)
return self.old(host, *args, **kwargs)

Expand All @@ -39,6 +44,34 @@ def whitelisted(self, host, *args, **kwargs):
httplib.HTTPConnection.__init__ = whitelisted


def block_http_aiohttp(whitelist):
try:
from aiohttp.client import ClientSession
except ImportError:
# no aiohttp installed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add log message at level info here. If one day aiohttp changes import locations, this code will silently fail without giving a hint as to why it fails. better yet, if you want, don't catch for import errors at all but use pkg_resources to check if the aiohttp package is installed.
Then a changed import in aiohttp will trigger an exception which is even better than a log message.

return

from yarl import URL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is yarl?
If it is from aiohttp, please put it in the try block

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's a requirement of aiohttp


def patch(self, method, url, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am fine with naming the patched method patch, but if you do it, please rename whitelisted above too. It should be consistent.

yurl = URL(url)

if yurl.host not in whitelist:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a log statement when an url passes the whitelist, as above.

http_fail(yurl.host)

return self.old_request(method, url, **kwargs)

if not getattr(ClientSession, 'blockage', False):
logger.debug('Monkey patching httplib')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You aren't monkeypatching httplib here. This debug message might be confusing.

ClientSession.old_request = ClientSession._request
ClientSession._request = patch


def block_http(whitelist):
block_http_httplib(whitelist)
block_http_aiohttp(whitelist)


def block_smtp(whitelist):
def whitelisted(self, host, *args, **kwargs):
if isinstance(host, basestring) and host not in whitelist:
Expand Down