blob: 0c0bdc2bc3f709d1b93a55b499752322d52c610f [file] [log] [blame]
Joe Gregorio2149bbf2011-06-22 16:43:39 -04001"""
2httplib2test_appengine
3
4A 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
11import os
12import sys
13import unittest
14
15# The test resources base uri
16base = 'http://bitworking.org/projects/httplib2/test/'
17#base = 'http://localhost/projects/httplib2/test/'
18cacheDirName = ".cache"
19APP_ENGINE_PATH='../../google_appengine'
20
21sys.path.insert(0, APP_ENGINE_PATH)
22
23import dev_appserver
24dev_appserver.fix_sys_path()
25
26from google.appengine.ext import testbed
27testbed = testbed.Testbed()
28testbed.activate()
29testbed.init_urlfetch_stub()
30
Joe Gregorio4eed8a12013-03-03 20:29:45 -050031import google.appengine.api
Joe Gregoriof35aafc2012-12-26 08:57:14 -050032
Joe Gregorio2149bbf2011-06-22 16:43:39 -040033import httplib2
34
35class AppEngineHttpTest(unittest.TestCase):
36 def setUp(self):
Joe Gregoriof35aafc2012-12-26 08:57:14 -050037 if os.path.exists(cacheDirName):
Joe Gregorio2149bbf2011-06-22 16:43:39 -040038 [os.remove(os.path.join(cacheDirName, file)) for file in os.listdir(cacheDirName)]
39
Joe Gregorio2149bbf2011-06-22 16:43:39 -040040 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 Gregorio2149bbf2011-06-22 16:43:39 -040045 self.assertEquals(1, len(h.connections))
Joe Gregorio2149bbf2011-06-22 16:43:39 -040046 self.assertEquals(response.status, 200)
47 self.assertEquals(response['status'], '200')
48
Joe Gregoriof35aafc2012-12-26 08:57:14 -050049 # 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 Gregoriof35aafc2012-12-26 08:57:14 -050069 def test_proxy_info_ignored(self):
Joe Gregorio2149bbf2011-06-22 16:43:39 -040070 h = httplib2.Http(proxy_info='foo.txt')
Joe Gregoriof35aafc2012-12-26 08:57:14 -050071 response, content = h.request("http://bitworking.org")
72 self.assertEquals(response.status, 200)
Joe Gregorio2149bbf2011-06-22 16:43:39 -040073
Joe Gregorio4eed8a12013-03-03 20:29:45 -050074
75class 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 Gregorio2149bbf2011-06-22 16:43:39 -040094if __name__ == '__main__':
95 unittest.main()