blob: ea36f39cb77a7b5ca583d804b7bf1d83f6d08058 [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 Gregoriof35aafc2012-12-26 08:57:14 -050031from google.appengine.runtime import DeadlineExceededError
32
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
40 if sys.version_info < (2, 6):
41 disable_cert_validation = True
42 else:
43 disable_cert_validation = False
44
45 def test(self):
46 h = httplib2.Http()
47 response, content = h.request("http://bitworking.org")
48 self.assertEqual(httplib2.SCHEME_TO_CONNECTION['https'],
49 httplib2.AppEngineHttpsConnection)
Joe Gregorio2149bbf2011-06-22 16:43:39 -040050 self.assertEquals(1, len(h.connections))
Joe Gregorio2149bbf2011-06-22 16:43:39 -040051 self.assertEquals(response.status, 200)
52 self.assertEquals(response['status'], '200')
53
Joe Gregoriof35aafc2012-12-26 08:57:14 -050054 # It would be great to run the test below, but it really tests the
55 # aberrant behavior of httplib on App Engine, but that special aberrant
56 # httplib only appears when actually running on App Engine and not when
57 # running via the SDK. When running via the SDK the httplib in std lib is
58 # loaded, which throws a different error when a timeout occurs.
59 #
60 #def test_timeout(self):
61 # # The script waits 3 seconds, so a timeout of more than that should succeed.
62 # h = httplib2.Http(timeout=7)
63 # r, c = h.request('http://bitworking.org/projects/httplib2/test/timeout/timeout.cgi')
64 #
65 # import httplib
66 # print httplib.__file__
67 # h = httplib2.Http(timeout=1)
68 # try:
69 # r, c = h.request('http://bitworking.org/projects/httplib2/test/timeout/timeout.cgi')
70 # self.fail('Timeout should have raised an exception.')
71 # except DeadlineExceededError:
72 # pass
73
74
75
76 def test_proxy_info_ignored(self):
Joe Gregorio2149bbf2011-06-22 16:43:39 -040077 h = httplib2.Http(proxy_info='foo.txt')
Joe Gregoriof35aafc2012-12-26 08:57:14 -050078 response, content = h.request("http://bitworking.org")
79 self.assertEquals(response.status, 200)
Joe Gregorio2149bbf2011-06-22 16:43:39 -040080
81if __name__ == '__main__':
82 unittest.main()