Package ndg :: Package httpsclient :: Package test :: Module test_urllib2
[hide private]

Source Code for Module ndg.httpsclient.test.test_urllib2

 1  """unit tests module for ndg.httpsclient.urllib2_build_opener module 
 2   
 3  PyOpenSSL utility to make a httplib-like interface suitable for use with  
 4  urllib2 
 5  """ 
 6  __author__ = "P J Kershaw (STFC)" 
 7  __date__ = "06/01/12" 
 8  __copyright__ = "(C) 2012 Science and Technology Facilities Council" 
 9  __license__ = "BSD - see LICENSE file in top-level directory" 
10  __contact__ = "Philip.Kershaw@stfc.ac.uk" 
11  __revision__ = '$Id$' 
12  from urllib2 import URLError 
13  import unittest 
14   
15  from OpenSSL import SSL 
16  from ndg.httpsclient.test import Constants 
17  from ndg.httpsclient.urllib2_build_opener import build_opener 
18   
19   
20 -class Urllib2TestCase(unittest.TestCase):
21 """Unit tests for urllib2 functionality""" 22 24 opener = build_opener() 25 self.assert_(opener)
26
27 - def test02_open(self):
28 opener = build_opener() 29 res = opener.open(Constants.TEST_URI) 30 self.assert_(res) 31 print("res = %s" % res.read())
32
33 - def test03_open_fails_unknown_loc(self):
34 opener = build_opener() 35 self.failUnlessRaises(URLError, opener.open, Constants.TEST_URI2)
36
37 - def test04_open_peer_cert_verification_fails(self):
38 # Explicitly set empty CA directory to make verification fail 39 ctx = SSL.Context(SSL.SSLv3_METHOD) 40 verify_callback = lambda conn, x509, errnum, errdepth, preverify_ok: \ 41 preverify_ok 42 43 ctx.set_verify(SSL.VERIFY_PEER, verify_callback) 44 ctx.load_verify_locations(None, './') 45 opener = build_opener(ssl_context=ctx) 46 self.failUnlessRaises(SSL.Error, opener.open, Constants.TEST_URI)
47 48 49 if __name__ == "__main__": 50 unittest.main() 51