Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 1 | """Tests for httplib2 on Google App Engine.""" |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 2 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 3 | import mock |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 4 | import os |
| 5 | import sys |
| 6 | import unittest |
| 7 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 8 | APP_ENGINE_PATH='/usr/local/google_appengine' |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 9 | |
| 10 | sys.path.insert(0, APP_ENGINE_PATH) |
| 11 | |
| 12 | import dev_appserver |
| 13 | dev_appserver.fix_sys_path() |
| 14 | |
| 15 | from google.appengine.ext import testbed |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 16 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 17 | # Ensure that we are not loading the httplib2 version included in the Google |
| 18 | # App Engine SDK. |
| 19 | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 20 | |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 21 | |
| 22 | class AberrationsTest(unittest.TestCase): |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 23 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 24 | def setUp(self): |
| 25 | self.testbed = testbed.Testbed() |
| 26 | self.testbed.activate() |
| 27 | self.testbed.init_urlfetch_stub() |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 28 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 29 | def tearDown(self): |
| 30 | self.testbed.deactivate() |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 31 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 32 | @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 | |
| 43 | class 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 Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 76 | |
| 77 | |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 78 | if __name__ == '__main__': |
| 79 | unittest.main() |