blob: 9fad05a580ed83fdeb50c352f9b9bbe530646bbb [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 Yud397c362016-06-16 00:40:07 -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
13dev_appserver.fix_sys_path()
14
15from google.appengine.ext import testbed
Joe Gregorio2149bbf2011-06-22 16:43:39 -040016
Alex Yud397c362016-06-16 00:40:07 -040017# Ensure that we are not loading the httplib2 version included in the Google
18# App Engine SDK.
19sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))
Joe Gregorio2149bbf2011-06-22 16:43:39 -040020
Joe Gregorio4eed8a12013-03-03 20:29:45 -050021
22class AberrationsTest(unittest.TestCase):
Joe Gregorio4eed8a12013-03-03 20:29:45 -050023
Alex Yud397c362016-06-16 00:40:07 -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 Yud397c362016-06-16 00:40:07 -040029 def tearDown(self):
30 self.testbed.deactivate()
Joe Gregorio4eed8a12013-03-03 20:29:45 -050031
Alex Yud397c362016-06-16 00:40:07 -040032 @mock.patch.dict('os.environ', {'SERVER_SOFTWARE': ''})
33 def testConnectionInit(self):
34 global httplib2
35 import httplib2
36 self.assertNotEqual(
37 httplib2.SCHEME_TO_CONNECTION['https'], httplib2.AppEngineHttpsConnection)
38 self.assertNotEqual(
39 httplib2.SCHEME_TO_CONNECTION['http'], httplib2.AppEngineHttpConnection)
40 del globals()['httplib2']
41
42
43class AppEngineHttpTest(unittest.TestCase):
44
45 def setUp(self):
46 self.testbed = testbed.Testbed()
47 self.testbed.activate()
48 self.testbed.init_urlfetch_stub()
49 global httplib2
50 import httplib2
51 reload(httplib2)
52
53 def tearDown(self):
54 self.testbed.deactivate()
55 del globals()['httplib2']
56
57 def testConnectionInit(self):
58 self.assertEqual(
59 httplib2.SCHEME_TO_CONNECTION['https'], httplib2.AppEngineHttpsConnection)
60 self.assertEqual(
61 httplib2.SCHEME_TO_CONNECTION['http'], httplib2.AppEngineHttpConnection)
62
63 def testGet(self):
64 http = httplib2.Http()
65 response, content = http.request("http://www.google.com")
66 self.assertEqual(httplib2.SCHEME_TO_CONNECTION['https'],
67 httplib2.AppEngineHttpsConnection)
68 self.assertEquals(1, len(http.connections))
69 self.assertEquals(response.status, 200)
70 self.assertEquals(response['status'], '200')
71
72 def testProxyInfoIgnored(self):
73 http = httplib2.Http(proxy_info=mock.MagicMock())
74 response, content = http.request("http://www.google.com")
75 self.assertEquals(response.status, 200)
Joe Gregorio4eed8a12013-03-03 20:29:45 -050076
77
Joe Gregorio2149bbf2011-06-22 16:43:39 -040078if __name__ == '__main__':
79 unittest.main()