Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Mocking an SSLError in Python

I’ve been dealing with the ssl.SSLError: [SSL: UNSAFE_LEGACY_RENEGOTIATION_DISABLED] unsafe legacy renegotiation disabled issue described here. As part of the fix, I’m attempting to write in exception-handling for the exception:

try:
    return req()
except (ssl.SSLError, RSSLError) as ssl_err:
    print(ssl_err)
    if "UNSAFE_LEGACY_RENEGOTIATION_DISABLED" in str(ssl_err):
        ctx = ssl.create_default_context(ssl.Purpose.SERVER_AUTH)
        ctx.options |= 0x4
        self._sess.mount("https://", CustomHttpAdapter(ctx))
        return req()
    raise

The issue I’m having is testing it. I’ve tried doing this:

err = SSLError()
err.reason = "UNSAFE_LEGACY_RENEGOTIATION_DISABLED"

but this prints as (). How do I create a mock SSLError that I can use to test this code?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to pass the string as your argument to the constructor:

>>> from ssl import SSLError
>>> error = SSLError("UNSAFE_LEGACY_RENEGOTIATION_DISABLED")
>>> print(error)
('UNSAFE_LEGACY_RENEGOTIATION_DISABLED',)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading