Skip to content

Commit

Permalink
Add workaround for httpretty incompatibility with requests and pyopenssl
Browse files Browse the repository at this point in the history
See gabrielfalcao/HTTPretty#242

Patch based on mredar/harvester@228e9c3

Signed-off-by: Michal Čihař <michal@cihar.com>
  • Loading branch information
nijel committed Oct 6, 2017
1 parent 4baafdc commit 00adeb1
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions weblate/accounts/tests/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@

from weblate.trans.tests.test_views import RegistrationTestMixin

# Workaround for httpretty breakage with pyopenssl
# pylint: disable=W0612
import weblate.trans.tests.mypretty # noqa

REGISTRATION_DATA = {
'username': 'username',
'email': 'noreply-weblate@example.org',
Expand Down
43 changes: 43 additions & 0 deletions weblate/trans/tests/mypretty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# from : https://github.com/gabrielfalcao/HTTPretty/issues/242
from httpretty import HTTPretty as OriginalHTTPretty

try:
from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3, extract_from_urllib3
pyopenssl_override = True
except:
pyopenssl_override = False


class MyHTTPretty(OriginalHTTPretty):
""" pyopenssl monkey-patches the default ssl_wrap_socket() function in the 'requests' library,
but this can stop the HTTPretty socket monkey-patching from working for HTTPS requests.
Our version extends the base HTTPretty enable() and disable() implementations to undo
and redo the pyopenssl monkey-patching, respectively.
"""

@classmethod
def enable(cls):
OriginalHTTPretty.enable()
if pyopenssl_override:
# Take out the pyopenssl version - use the default implementation
extract_from_urllib3()

@classmethod
def disable(cls):
OriginalHTTPretty.disable()
if pyopenssl_override:
# Put the pyopenssl version back in place
inject_into_urllib3()


# Substitute in our version
HTTPretty = MyHTTPretty

import httpretty.core
httpretty.core.httpretty = MyHTTPretty

# May need to set other module-level attributes here, e.g. enable, reset etc,
# depending on your needs
import httpretty
httpretty.httpretty = MyHTTPretty
httpretty.register_uri = httpretty.register_uri

0 comments on commit 00adeb1

Please sign in to comment.