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 | aa1b95b | 2018-07-26 23:23:35 -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 |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 13 | |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 14 | dev_appserver.fix_sys_path() |
| 15 | |
| 16 | from google.appengine.ext import testbed |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 17 | |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 18 | # Ensure that we are not loading the httplib2 version included in the Google |
| 19 | # App Engine SDK. |
| 20 | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__))) |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 21 | |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 22 | |
| 23 | class AberrationsTest(unittest.TestCase): |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -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 | aa1b95b | 2018-07-26 23:23:35 -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 | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 32 | @mock.patch.dict("os.environ", {"SERVER_SOFTWARE": ""}) |
| 33 | def testConnectionInit(self): |
| 34 | global httplib2 |
| 35 | import httplib2 |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 36 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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"] |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 44 | |
| 45 | |
| 46 | class AppEngineHttpTest(unittest.TestCase): |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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 |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 53 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 54 | reload(httplib2) |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 55 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 56 | def tearDown(self): |
| 57 | self.testbed.deactivate() |
| 58 | del globals()["httplib2"] |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 59 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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 | ) |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 67 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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") |
Alex Yu | d397c36 | 2016-06-16 00:40:07 -0400 | [diff] [blame] | 77 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 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) |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 82 | |
| 83 | |
Alex Yu | aa1b95b | 2018-07-26 23:23:35 -0400 | [diff] [blame^] | 84 | if __name__ == "__main__": |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 85 | unittest.main() |