summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Falcão <gabrielfalcao@users.noreply.github.com>2021-05-14 00:06:43 +0200
committerGitHub <noreply@github.com>2021-05-14 00:06:43 +0200
commitedfb1256e4383ee6e7181d888f96d42f14781d5c (patch)
treef0be453fc75cc798bd0856afaa4a7f9ef7a02cfa
parent2224470ecc15daa78832aefb50c03e9d7aab454e (diff)
downloadhttpretty-edfb1256e4383ee6e7181d888f96d42f14781d5c.tar.gz
Fixes pyopenssl issue with urllib3 (#420)
closes #417
-rw-r--r--httpretty/core.py27
-rw-r--r--tests/functional/bugfixes/test_417_openssl.py31
2 files changed, 51 insertions, 7 deletions
diff --git a/httpretty/core.py b/httpretty/core.py
index f381eaf..f76aebe 100644
--- a/httpretty/core.py
+++ b/httpretty/core.py
@@ -117,11 +117,23 @@ try:
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:
@@ -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)
@@ -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()
diff --git a/tests/functional/bugfixes/test_417_openssl.py b/tests/functional/bugfixes/test_417_openssl.py
new file mode 100644
index 0000000..29d82da
--- /dev/null
+++ b/tests/functional/bugfixes/test_417_openssl.py
@@ -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