1"""Tests for httplib2 on Google App Engine.""" 2 3import mock 4import os 5import sys 6import unittest 7 8APP_ENGINE_PATH = "/usr/local/google_appengine" 9 10sys.path.insert(0, APP_ENGINE_PATH) 11 12import dev_appserver 13 14dev_appserver.fix_sys_path() 15 16from google.appengine.ext import testbed 17 18# Ensure that we are not loading the httplib2 version included in the Google 19# App Engine SDK. 20sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) 21 22 23class AberrationsTest(unittest.TestCase): 24 def setUp(self): 25 self.testbed = testbed.Testbed() 26 self.testbed.activate() 27 self.testbed.init_urlfetch_stub() 28 29 def tearDown(self): 30 self.testbed.deactivate() 31 32 @mock.patch.dict("os.environ", {"SERVER_SOFTWARE": ""}) 33 def testConnectionInit(self): 34 global httplib2 35 import httplib2 36 37 self.assertNotEqual( 38 httplib2.SCHEME_TO_CONNECTION["https"], httplib2.AppEngineHttpsConnection 39 ) 40 self.assertNotEqual( 41 httplib2.SCHEME_TO_CONNECTION["http"], httplib2.AppEngineHttpConnection 42 ) 43 del globals()["httplib2"] 44 45 46class AppEngineHttpTest(unittest.TestCase): 47 def setUp(self): 48 self.testbed = testbed.Testbed() 49 self.testbed.activate() 50 self.testbed.init_urlfetch_stub() 51 global httplib2 52 import httplib2 53 54 reload(httplib2) 55 56 def tearDown(self): 57 self.testbed.deactivate() 58 del globals()["httplib2"] 59 60 def testConnectionInit(self): 61 self.assertEqual( 62 httplib2.SCHEME_TO_CONNECTION["https"], httplib2.AppEngineHttpsConnection 63 ) 64 self.assertEqual( 65 httplib2.SCHEME_TO_CONNECTION["http"], httplib2.AppEngineHttpConnection 66 ) 67 68 def testGet(self): 69 http = httplib2.Http() 70 response, content = http.request("http://www.google.com") 71 self.assertEqual( 72 httplib2.SCHEME_TO_CONNECTION["https"], httplib2.AppEngineHttpsConnection 73 ) 74 self.assertEquals(1, len(http.connections)) 75 self.assertEquals(response.status, 200) 76 self.assertEquals(response["status"], "200") 77 78 def testProxyInfoIgnored(self): 79 http = httplib2.Http(proxy_info=mock.MagicMock()) 80 response, content = http.request("http://www.google.com") 81 self.assertEquals(response.status, 200) 82 83 84if __name__ == "__main__": 85 unittest.main() 86