Skip to content

Commit

Permalink
Fixes pyopenssl issue with urllib3 (#420)
Browse files Browse the repository at this point in the history
closes #417
  • Loading branch information
gabrielfalcao committed May 13, 2021
1 parent 2224470 commit edfb125
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
27 changes: 20 additions & 7 deletions httpretty/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,23 @@
except ImportError:
_ssl = None
# used to handle error caused by ndg-httpsclient
pyopenssl_overrides_inject = []
pyopenssl_overrides_extract = []
try:
from requests.packages.urllib3.contrib.pyopenssl import inject_into_urllib3, extract_from_urllib3
pyopenssl_override = True
pyopenssl_overrides_extract.append(extract_from_urllib)
pyopenssl_overrides_inject.append(inject_from_urllib)
except Exception:
pyopenssl_override = False
pass



try:
from urllib3.contrib.pyopenssl import extract_from_urllib3, inject_into_urllib3
pyopenssl_overrides_extract.append(extract_from_urllib)
pyopenssl_overrides_inject.append(inject_from_urllib)
except Exception:
pass


try:
Expand Down Expand Up @@ -1720,9 +1732,9 @@ def apply_patch_socket():
socket.__dict__['getaddrinfo'] = fake_getaddrinfo


if pyopenssl_override:
# Take out the pyopenssl version - use the default implementation
extract_from_urllib3()
# Take out the pyopenssl version - use the default implementation
for extract_from_urllib3 in pyopenssl_overrides_extract:
extract_into_urllib3()

if requests_urllib3_connection is not None:
urllib3_wrap = partial(fake_wrap_socket, old_requests_ssl_wrap_socket)
Expand Down Expand Up @@ -1795,8 +1807,9 @@ def undo_patch_socket():
requests_urllib3_connection.__dict__['ssl_wrap_socket'] = \
old_requests_ssl_wrap_socket

if pyopenssl_override:
# Put the pyopenssl version back in place

# Put the pyopenssl version back in place
for inject_from_urllib3 in pyopenssl_overrides_inject:
inject_into_urllib3()


Expand Down
31 changes: 31 additions & 0 deletions tests/functional/bugfixes/test_417_openssl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# This test is based on @ento's example snippet:
# https://gist.github.com/ento/e1e33d7d67e406bf03fe61f018404c21

# Original Issue:
# https://github.com/gabrielfalcao/HTTPretty/issues/417
import httpretty
import requests
import urllib3
from sure import expect
from unittest import skipIf
try:
from urllib3.contrib.pyopenssl import extract_from_urllib3
except Exception:
extract_from_urllib3 = None


@skipIf(extract_from_urllib3 is None,
"urllib3.contrib.pyopenssl.extract_from_urllib3 does not exist")
def test_enable_disable_httpretty_extract():
expect(urllib3.util.IS_PYOPENSSL).to.be.false
httpretty.enable()
httpretty.disable()
extract_from_urllib3()
expect(urllib3.util.IS_PYOPENSSL).to.be.false

def test_enable_disable_httpretty():
expect(urllib3.util.IS_PYOPENSSL).to.be.false
httpretty.enable()
httpretty.disable()
extract_from_urllib3()
expect(urllib3.util.IS_PYOPENSSL).to.be.false

0 comments on commit edfb125

Please sign in to comment.