import ssl import urllib.request from urllib.request import urlopen, build_opener, install_opener, HTTPSHandler def test_generic_ssl(): ''' this will pass as urlopen will add a context for us ''' r = urlopen('https://blue-labs.org') install_opener(None) def test_single_context(): ''' this should pass as we build a context and pass it to urlopen ''' c1 = ssl.create_default_context() r = urlopen('https://blue-labs.org', context=c1) install_opener(None) def test_add_https_handler_no_context(): ''' this should pass because we build a generic HTTPSHandler without a context, and let urlopen add a default context ''' hs = HTTPSHandler() opener = build_opener(hs) install_opener(opener) r = urlopen('https://blue-labs.org') install_opener(None) def test_add_https_handler_and_context(): ''' this should pass because we build a generic HTTPSHandler without a context, then we add one in our call to urlopen. urlopen will find our HTTPSHandler and attach this context to it ''' hs = HTTPSHandler() opener = build_opener(hs) install_opener(opener) c1 = ssl.create_default_context() r = urlopen('https://blue-labs.org', context=c1) install_opener(None) def test_subclass_https_handler_without_context(): ''' this should pass because we subclass HTTPSHandler but without a context. urlopen will find our subclassed HTTPSHandler and add a default context to it ''' class SubHTTPSHandler(HTTPSHandler): def __init__(self, *args, **kwargs): HTTPSHandler.__init__(self, *args, **kwargs) self.arbitrary_property = True hs = SubHTTPSHandler() opener = build_opener(hs) install_opener(opener) r = urlopen('https://blue-labs.org') install_opener(None) def test_subclass_https_handler_with_context(): ''' this should pass because we subclass HTTPSHandler but without a context. urlopen will find our subclassed HTTPSHandler and add a default context to it ''' class SubHTTPSHandler(HTTPSHandler): def __init__(self, *args, **kwargs): HTTPSHandler.__init__(self, *args, **kwargs) self.arbitrary_property = True hs = SubHTTPSHandler() opener = build_opener(hs) install_opener(opener) c1 = ssl.create_default_context() r = urlopen('https://blue-labs.org', context=c1) install_opener(None) def test_multiple_https_handlers(): ''' this should fail because we can only have one HTTPSHandler in our chain ''' hs1 = HTTPSHandler() hs2 = HTTPSHandler() try: opener = build_opener(hs1, hs2) except ValueError: return False def test_multiple_contexts(): ''' this should fail because we should only have one SSL in our chain and handler ''' c1 = ssl.create_default_context() hs1 = HTTPSHandler(context=c1) opener = build_opener(hs1) install_opener(opener) c2 = ssl.create_default_context() try: r = urlopen('https://blue-labs.org', context=c2) print('failed') except ValueError: return False finally: install_opener(None) # all these tests will pass, where an HTTPSHandler is missing, it will be added # where an ssl context is missing, it will be added assert test_generic_ssl() == None assert test_single_context() == None assert test_add_https_handler_no_context() == None assert test_add_https_handler_and_context() == None assert test_subclass_https_handler_without_context() == None assert test_subclass_https_handler_with_context() == None # all these tests will return False due to multiple HTTPSHandlers existing or multiple ssl contexts # only one HTTPSHandler can exist and little reason exists for multiple contexts in the same # handler assert test_multiple_https_handlers() == False assert test_multiple_contexts() == False