blob: d5c578698b37997f01d30f87d5d14c243b982855 [file] [log] [blame]
Alex Yud397c362016-06-16 00:40:07 -04001"""Tests for httplib2 on Google App Engine."""
Joe Gregorio2149bbf2011-06-22 16:43:39 -04002
Alex Yud397c362016-06-16 00:40:07 -04003import mock
Joe Gregorio2149bbf2011-06-22 16:43:39 -04004import os
5import sys
6import unittest
7
Alex Yuaa1b95b2018-07-26 23:23:35 -04008APP_ENGINE_PATH = "/usr/local/google_appengine"
Joe Gregorio2149bbf2011-06-22 16:43:39 -04009
10sys.path.insert(0, APP_ENGINE_PATH)
11
12import dev_appserver
Alex Yuaa1b95b2018-07-26 23:23:35 -040013
Joe Gregorio2149bbf2011-06-22 16:43:39 -040014dev_appserver.fix_sys_path()
15
16from google.appengine.ext import testbed
Joe Gregorio2149bbf2011-06-22 16:43:39 -040017
Alex Yud397c362016-06-16 00:40:07 -040018# 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__)))
Joe Gregorio2149bbf2011-06-22 16:43:39 -040021
Joe Gregorio4eed8a12013-03-03 20:29:45 -050022
23class AberrationsTest(unittest.TestCase):
Alex Yuaa1b95b2018-07-26 23:23:35 -040024 def setUp(self):
25 self.testbed = testbed.Testbed()
26 self.testbed.activate()
27 self.testbed.init_urlfetch_stub()
Joe Gregorio4eed8a12013-03-03 20:29:45 -050028
Alex Yuaa1b95b2018-07-26 23:23:35 -040029 def tearDown(self):
30 self.testbed.deactivate()
Joe Gregorio4eed8a12013-03-03 20:29:45 -050031
Alex Yuaa1b95b2018-07-26 23:23:35 -040032 @mock.patch.dict("os.environ", {"SERVER_SOFTWARE": ""})
33 def testConnectionInit(self):
34 global httplib2
35 import httplib2
Joe Gregorio4eed8a12013-03-03 20:29:45 -050036
Alex Yuaa1b95b2018-07-26 23:23:35 -040037 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"]
Alex Yud397c362016-06-16 00:40:07 -040044
45
46class AppEngineHttpTest(unittest.TestCase):
Alex Yuaa1b95b2018-07-26 23:23:35 -040047 def setUp(self):
48 self.testbed = testbed.Testbed()
49 self.testbed.activate()
50 self.testbed.init_urlfetch_stub()
51 global httplib2
52 import httplib2
Alex Yud397c362016-06-16 00:40:07 -040053
Alex Yuaa1b95b2018-07-26 23:23:35 -040054 reload(httplib2)
Alex Yud397c362016-06-16 00:40:07 -040055
Alex Yuaa1b95b2018-07-26 23:23:35 -040056 def tearDown(self):
57 self.testbed.deactivate()
58 del globals()["httplib2"]
Alex Yud397c362016-06-16 00:40:07 -040059
Alex Yuaa1b95b2018-07-26 23:23:35 -040060 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 )
Alex Yud397c362016-06-16 00:40:07 -040067
Alex Yuaa1b95b2018-07-26 23:23:35 -040068 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")
Alex Yud397c362016-06-16 00:40:07 -040077
Alex Yuaa1b95b2018-07-26 23:23:35 -040078 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)
Joe Gregorio4eed8a12013-03-03 20:29:45 -050082
83
Alex Yuaa1b95b2018-07-26 23:23:35 -040084if __name__ == "__main__":
Joe Gregorio2149bbf2011-06-22 16:43:39 -040085 unittest.main()