Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 1 | """ |
| 2 | httplib2test_appengine |
| 3 | |
| 4 | A set of unit tests for httplib2.py on Google App Engine |
| 5 | |
| 6 | """ |
| 7 | |
| 8 | __author__ = "Joe Gregorio (joe@bitworking.org)" |
| 9 | __copyright__ = "Copyright 2011, Joe Gregorio" |
| 10 | |
| 11 | import os |
| 12 | import sys |
| 13 | import unittest |
| 14 | |
| 15 | # The test resources base uri |
| 16 | base = 'http://bitworking.org/projects/httplib2/test/' |
| 17 | #base = 'http://localhost/projects/httplib2/test/' |
| 18 | cacheDirName = ".cache" |
| 19 | APP_ENGINE_PATH='../../google_appengine' |
| 20 | |
| 21 | sys.path.insert(0, APP_ENGINE_PATH) |
| 22 | |
| 23 | import dev_appserver |
| 24 | dev_appserver.fix_sys_path() |
| 25 | |
| 26 | from google.appengine.ext import testbed |
| 27 | testbed = testbed.Testbed() |
| 28 | testbed.activate() |
| 29 | testbed.init_urlfetch_stub() |
| 30 | |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 31 | import google.appengine.api |
Joe Gregorio | f35aafc | 2012-12-26 08:57:14 -0500 | [diff] [blame] | 32 | |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 33 | import httplib2 |
| 34 | |
| 35 | class AppEngineHttpTest(unittest.TestCase): |
| 36 | def setUp(self): |
Joe Gregorio | f35aafc | 2012-12-26 08:57:14 -0500 | [diff] [blame] | 37 | if os.path.exists(cacheDirName): |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 38 | [os.remove(os.path.join(cacheDirName, file)) for file in os.listdir(cacheDirName)] |
| 39 | |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 40 | def test(self): |
| 41 | h = httplib2.Http() |
| 42 | response, content = h.request("http://bitworking.org") |
| 43 | self.assertEqual(httplib2.SCHEME_TO_CONNECTION['https'], |
| 44 | httplib2.AppEngineHttpsConnection) |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 45 | self.assertEquals(1, len(h.connections)) |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 46 | self.assertEquals(response.status, 200) |
| 47 | self.assertEquals(response['status'], '200') |
| 48 | |
Joe Gregorio | f35aafc | 2012-12-26 08:57:14 -0500 | [diff] [blame] | 49 | # It would be great to run the test below, but it really tests the |
| 50 | # aberrant behavior of httplib on App Engine, but that special aberrant |
| 51 | # httplib only appears when actually running on App Engine and not when |
| 52 | # running via the SDK. When running via the SDK the httplib in std lib is |
| 53 | # loaded, which throws a different error when a timeout occurs. |
| 54 | # |
| 55 | #def test_timeout(self): |
| 56 | # # The script waits 3 seconds, so a timeout of more than that should succeed. |
| 57 | # h = httplib2.Http(timeout=7) |
| 58 | # r, c = h.request('http://bitworking.org/projects/httplib2/test/timeout/timeout.cgi') |
| 59 | # |
| 60 | # import httplib |
| 61 | # print httplib.__file__ |
| 62 | # h = httplib2.Http(timeout=1) |
| 63 | # try: |
| 64 | # r, c = h.request('http://bitworking.org/projects/httplib2/test/timeout/timeout.cgi') |
| 65 | # self.fail('Timeout should have raised an exception.') |
| 66 | # except DeadlineExceededError: |
| 67 | # pass |
| 68 | |
Joe Gregorio | f35aafc | 2012-12-26 08:57:14 -0500 | [diff] [blame] | 69 | def test_proxy_info_ignored(self): |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 70 | h = httplib2.Http(proxy_info='foo.txt') |
Joe Gregorio | f35aafc | 2012-12-26 08:57:14 -0500 | [diff] [blame] | 71 | response, content = h.request("http://bitworking.org") |
| 72 | self.assertEquals(response.status, 200) |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 73 | |
Joe Gregorio | 4eed8a1 | 2013-03-03 20:29:45 -0500 | [diff] [blame] | 74 | |
| 75 | class AberrationsTest(unittest.TestCase): |
| 76 | def setUp(self): |
| 77 | self.orig_apiproxy_stub_map = google.appengine.api.apiproxy_stub_map |
| 78 | |
| 79 | # Force apiproxy_stub_map to None to trigger the test condition. |
| 80 | google.appengine.api.apiproxy_stub_map = None |
| 81 | reload(httplib2) |
| 82 | |
| 83 | def tearDown(self): |
| 84 | google.appengine.api.apiproxy_stub_map = self.orig_apiproxy_stub_map |
| 85 | reload(httplib2) |
| 86 | |
| 87 | def test(self): |
| 88 | self.assertNotEqual(httplib2.SCHEME_TO_CONNECTION['https'], |
| 89 | httplib2.AppEngineHttpsConnection) |
| 90 | self.assertNotEqual(httplib2.SCHEME_TO_CONNECTION['http'], |
| 91 | httplib2.AppEngineHttpConnection) |
| 92 | |
| 93 | |
Joe Gregorio | 2149bbf | 2011-06-22 16:43:39 -0400 | [diff] [blame] | 94 | if __name__ == '__main__': |
| 95 | unittest.main() |