blob: 3bfde31a4f47cf82c5266178bc83ebf4c468d792 [file] [log] [blame]
jcgregorio2d66d4f2006-02-07 05:34:14 +00001#!/usr/bin/env python2.4
2"""
3httplib2test
4
5A set of unit tests for httplib2.py.
6
7Requires Python 2.4 or later
8"""
9
10__author__ = "Joe Gregorio (joe@bitworking.org)"
11__copyright__ = "Copyright 2006, Joe Gregorio"
12__contributors__ = []
13__license__ = "MIT"
14__history__ = """ """
15__version__ = "0.1 ($Rev: 118 $)"
16
17
jcgregoriode8238d2007-03-07 19:08:26 +000018import sys
19import unittest
20import httplib
21import httplib2
22import os
23import urlparse
24import time
25import base64
jcgregorio14644372007-07-30 14:13:37 +000026import StringIO
jcgregorio8421f272006-02-14 18:19:51 +000027
28# Python 2.3 support
29if not hasattr(unittest.TestCase, 'assertTrue'):
30 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
31 unittest.TestCase.assertFalse = unittest.TestCase.failIf
32
jcgregorio2d66d4f2006-02-07 05:34:14 +000033# The test resources base uri
34base = 'http://bitworking.org/projects/httplib2/test/'
35#base = 'http://localhost/projects/httplib2/test/'
jcgregorio90fb4a42006-11-17 16:19:47 +000036cacheDirName = ".cache"
jcgregorio2d66d4f2006-02-07 05:34:14 +000037
jcgregoriode8238d2007-03-07 19:08:26 +000038
39class CredentialsTest(unittest.TestCase):
40 def test(self):
41 c = httplib2.Credentials()
42 c.add("joe", "password")
43 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
44 self.assertEqual(("joe", "password"), list(c.iter(""))[0])
45 c.add("fred", "password2", "wellformedweb.org")
46 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
47 self.assertEqual(1, len(list(c.iter("bitworking.org"))))
48 self.assertEqual(2, len(list(c.iter("wellformedweb.org"))))
49 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
50 c.clear()
51 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
52 c.add("fred", "password2", "wellformedweb.org")
53 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
54 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
55 self.assertEqual(0, len(list(c.iter(""))))
56
57
jcgregorio2d66d4f2006-02-07 05:34:14 +000058class ParserTest(unittest.TestCase):
59 def testFromStd66(self):
60 self.assertEqual( ('http', 'example.com', '', None, None ), httplib2.parse_uri("http://example.com"))
61 self.assertEqual( ('https', 'example.com', '', None, None ), httplib2.parse_uri("https://example.com"))
62 self.assertEqual( ('https', 'example.com:8080', '', None, None ), httplib2.parse_uri("https://example.com:8080"))
63 self.assertEqual( ('http', 'example.com', '/', None, None ), httplib2.parse_uri("http://example.com/"))
64 self.assertEqual( ('http', 'example.com', '/path', None, None ), httplib2.parse_uri("http://example.com/path"))
65 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', None ), httplib2.parse_uri("http://example.com/path?a=1&b=2"))
66 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', 'fred' ), httplib2.parse_uri("http://example.com/path?a=1&b=2#fred"))
67 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', 'fred' ), httplib2.parse_uri("http://example.com/path?a=1&b=2#fred"))
68
jcgregorio2d66d4f2006-02-07 05:34:14 +000069
jcgregorioa46fe4e2006-11-16 04:13:45 +000070class UrlNormTest(unittest.TestCase):
71 def test(self):
72 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://example.org")[-1])
73 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://EXAMple.org")[-1])
74 self.assertEqual( "http://example.org/?=b", httplib2.urlnorm("http://EXAMple.org?=b")[-1])
75 self.assertEqual( "http://example.org/mypath?a=b", httplib2.urlnorm("http://EXAMple.org/mypath?a=b")[-1])
76 self.assertEqual( "http://localhost:80/", httplib2.urlnorm("http://localhost:80")[-1])
jcgregoriob4e9ab02006-11-17 15:53:15 +000077 self.assertEqual( httplib2.urlnorm("http://localhost:80/"), httplib2.urlnorm("HTTP://LOCALHOST:80"))
jcgregorio132d28e2007-01-23 16:22:53 +000078 try:
79 httplib2.urlnorm("/")
80 self.fail("Non-absolute URIs should raise an exception")
81 except httplib2.RelativeURIError:
82 pass
jcgregorioa46fe4e2006-11-16 04:13:45 +000083
84class UrlSafenameTest(unittest.TestCase):
85 def test(self):
86 # Test that different URIs end up generating different safe names
87 self.assertEqual( "example.org,fred,a=b,58489f63a7a83c3b7794a6a398ee8b1f", httplib2.safename("http://example.org/fred/?a=b"))
88 self.assertEqual( "example.org,fred,a=b,8c5946d56fec453071f43329ff0be46b", httplib2.safename("http://example.org/fred?/a=b"))
89 self.assertEqual( "www.example.org,fred,a=b,499c44b8d844a011b67ea2c015116968", httplib2.safename("http://www.example.org/fred?/a=b"))
90 self.assertEqual( httplib2.safename(httplib2.urlnorm("http://www")[-1]), httplib2.safename(httplib2.urlnorm("http://WWW")[-1]))
91 self.assertEqual( "www.example.org,fred,a=b,692e843a333484ce0095b070497ab45d", httplib2.safename("https://www.example.org/fred?/a=b"))
92 self.assertNotEqual( httplib2.safename("http://www"), httplib2.safename("https://www"))
93 # Test the max length limits
94 uri = "http://" + ("w" * 200) + ".org"
95 uri2 = "http://" + ("w" * 201) + ".org"
96 self.assertNotEqual( httplib2.safename(uri2), httplib2.safename(uri))
97 # Max length should be 200 + 1 (",") + 32
98 self.assertEqual(233, len(httplib2.safename(uri2)))
99 self.assertEqual(233, len(httplib2.safename(uri)))
100 # Unicode
jcgregoriodebceec2006-12-12 20:26:02 +0000101 if sys.version_info >= (2,3):
102 self.assertEqual( "xn--http,-4y1d.org,fred,a=b,579924c35db315e5a32e3d9963388193", httplib2.safename(u"http://\u2304.org/fred/?a=b"))
jcgregorioa46fe4e2006-11-16 04:13:45 +0000103
jcgregorio14644372007-07-30 14:13:37 +0000104class _MyResponse(StringIO.StringIO):
105 def __init__(self, body, **kwargs):
106 StringIO.StringIO.__init__(self, body)
107 self.headers = kwargs
108
109 def iteritems(self):
110 return self.headers.iteritems()
111
112
113class _MyHTTPConnection(object):
114 "This class is just a mock of httplib.HTTPConnection used for testing"
115
116 def __init__(self, host, port=None, key_file=None, cert_file=None,
joe.gregoriof28536d2007-10-23 14:10:11 +0000117 strict=None, timeout=None, proxy_info=None):
jcgregorio14644372007-07-30 14:13:37 +0000118 self.host = host
119 self.port = port
120 self.timeout = timeout
121 self.log = ""
122
123 def set_debuglevel(self, level):
124 pass
125
126 def connect(self):
127 "Connect to a host on a given port."
128 pass
129
130 def close(self):
131 pass
132
133 def request(self, method, request_uri, body, headers):
134 pass
135
136 def getresponse(self):
137 return _MyResponse("the body", status="200")
jcgregorioa46fe4e2006-11-16 04:13:45 +0000138
jcgregorio90fb4a42006-11-17 16:19:47 +0000139
jcgregorio2d66d4f2006-02-07 05:34:14 +0000140class HttpTest(unittest.TestCase):
141 def setUp(self):
jcgregorio7e3608f2006-06-15 13:01:53 +0000142 if os.path.exists(cacheDirName):
143 [os.remove(os.path.join(cacheDirName, file)) for file in os.listdir(cacheDirName)]
144 self.http = httplib2.Http(cacheDirName)
jcgregorio36140b52006-06-13 02:17:52 +0000145 self.http.clear_credentials()
jcgregorio2d66d4f2006-02-07 05:34:14 +0000146
jcgregorio14644372007-07-30 14:13:37 +0000147 def testConnectionType(self):
joe.gregoriof28536d2007-10-23 14:10:11 +0000148 self.http.force_exception_to_status_code = False
jcgregorio14644372007-07-30 14:13:37 +0000149 response, content = self.http.request("http://bitworking.org", connection_type=_MyHTTPConnection)
150 self.assertEqual(response['content-location'], "http://bitworking.org")
151 self.assertEqual(content, "the body")
152
jcgregorio6a638172007-01-23 16:40:23 +0000153 def testGetUnknownServer(self):
jcgregorio07a9a4a2007-03-08 21:18:39 +0000154 self.http.force_exception_to_status_code = False
jcgregorio6a638172007-01-23 16:40:23 +0000155 try:
156 self.http.request("http://fred.bitworking.org/")
157 self.fail("An httplib2.ServerNotFoundError Exception must be thrown on an unresolvable server.")
158 except httplib2.ServerNotFoundError:
159 pass
160
jcgregorio07a9a4a2007-03-08 21:18:39 +0000161 # Now test with exceptions turned off
162 self.http.force_exception_to_status_code = True
163
164 (response, content) = self.http.request("http://fred.bitworking.org/")
165 self.assertEqual(response['content-type'], 'text/plain')
166 self.assertTrue(content.startswith("Unable to find"))
167 self.assertEqual(response.status, 400)
168
jcgregorioa898f8f2006-12-12 17:16:55 +0000169 def testGetIRI(self):
jcgregoriodebceec2006-12-12 20:26:02 +0000170 if sys.version_info >= (2,3):
171 uri = urlparse.urljoin(base, u"reflector/reflector.cgi?d=\N{CYRILLIC CAPITAL LETTER DJE}")
172 (response, content) = self.http.request(uri, "GET")
173 d = self.reflector(content)
174 self.assertTrue(d.has_key('QUERY_STRING'))
175 self.assertTrue(d['QUERY_STRING'].find('%D0%82') > 0)
jcgregorioa898f8f2006-12-12 17:16:55 +0000176
jcgregorio2d66d4f2006-02-07 05:34:14 +0000177 def testGetIsDefaultMethod(self):
178 # Test that GET is the default method
179 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000180 (response, content) = self.http.request(uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000181 self.assertEqual(response['x-method'], "GET")
182
183 def testDifferentMethods(self):
184 # Test that all methods can be used
185 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
186 for method in ["GET", "PUT", "DELETE", "POST"]:
jcgregorio36140b52006-06-13 02:17:52 +0000187 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000188 self.assertEqual(response['x-method'], method)
189
Joe Gregoriob628c0b2009-07-16 12:28:04 -0400190 def testHeadRead(self):
191 # Test that we don't try to read the response of a HEAD request
192 # since httplib blocks response.read() for HEAD requests.
193 # Oddly enough this doesn't appear as a problem when doing HEAD requests
194 # against Apache servers.
195 uri = "http://www.google.com/"
196 (response, content) = self.http.request(uri, "HEAD")
197 self.assertEqual(response.status, 200)
198 self.assertEqual(content, "")
199
jcgregorio2d66d4f2006-02-07 05:34:14 +0000200 def testGetNoCache(self):
201 # Test that can do a GET w/o the cache turned on.
202 http = httplib2.Http()
203 uri = urlparse.urljoin(base, "304/test_etag.txt")
204 (response, content) = http.request(uri, "GET")
205 self.assertEqual(response.status, 200)
jcgregorioa0713ab2006-07-01 05:21:34 +0000206 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000207
Joe Gregorioe202d212009-07-16 14:57:52 -0400208 def testGetOnlyIfCachedCacheHit(self):
209 # Test that can do a GET with cache and 'only-if-cached'
210 uri = urlparse.urljoin(base, "304/test_etag.txt")
211 (response, content) = self.http.request(uri, "GET")
212 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
213 self.assertEqual(response.fromcache, True)
214 self.assertEqual(response.status, 200)
215
216 def testGetOnlyIfCachedCacheMissCache(self):
217 # Test that can do a GET with cache and 'only-if-cached'
218 uri = urlparse.urljoin(base, "304/test_etag.txt")
219 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
220 self.assertEqual(response.fromcache, False)
221 self.assertEqual(response.status, 504)
222
jcgregorioe4ce13e2006-04-02 03:05:08 +0000223 def testGetOnlyIfCachedCacheMiss(self):
224 # Test that can do a GET with no cache with 'only-if-cached'
jcgregorioe4ce13e2006-04-02 03:05:08 +0000225 uri = urlparse.urljoin(base, "304/test_etag.txt")
Joe Gregorioe202d212009-07-16 14:57:52 -0400226 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
jcgregorioe4ce13e2006-04-02 03:05:08 +0000227 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400228 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000229
230 def testGetOnlyIfCachedNoCacheAtAll(self):
231 # Test that can do a GET with no cache with 'only-if-cached'
232 # Of course, there might be an intermediary beyond us
233 # that responds to the 'only-if-cached', so this
234 # test can't really be guaranteed to pass.
235 http = httplib2.Http()
236 uri = urlparse.urljoin(base, "304/test_etag.txt")
237 (response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
238 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400239 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000240
jcgregorio2d66d4f2006-02-07 05:34:14 +0000241 def testUserAgent(self):
242 # Test that we provide a default user-agent
243 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000244 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000245 self.assertEqual(response.status, 200)
246 self.assertTrue(content.startswith("Python-httplib2/"))
247
248 def testUserAgentNonDefault(self):
249 # Test that the default user-agent can be over-ridden
joe.gregoriof28536d2007-10-23 14:10:11 +0000250
jcgregorio2d66d4f2006-02-07 05:34:14 +0000251 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000252 (response, content) = self.http.request(uri, "GET", headers={'User-Agent': 'fred/1.0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000253 self.assertEqual(response.status, 200)
254 self.assertTrue(content.startswith("fred/1.0"))
255
256 def testGet300WithLocation(self):
257 # Test the we automatically follow 300 redirects if a Location: header is provided
258 uri = urlparse.urljoin(base, "300/with-location-header.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000259 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000260 self.assertEqual(response.status, 200)
261 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000262 self.assertEqual(response.previous.status, 300)
263 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000264
265 # Confirm that the intermediate 300 is not cached
jcgregorio36140b52006-06-13 02:17:52 +0000266 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000267 self.assertEqual(response.status, 200)
268 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000269 self.assertEqual(response.previous.status, 300)
270 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000271
jcgregorio2f1e1422007-05-03 13:17:33 +0000272 def testGet300WithLocationNoRedirect(self):
273 # Test the we automatically follow 300 redirects if a Location: header is provided
274 self.http.follow_redirects = False
275 uri = urlparse.urljoin(base, "300/with-location-header.asis")
276 (response, content) = self.http.request(uri, "GET")
277 self.assertEqual(response.status, 300)
278
jcgregorio2d66d4f2006-02-07 05:34:14 +0000279 def testGet300WithoutLocation(self):
280 # Not giving a Location: header in a 300 response is acceptable
281 # In which case we just return the 300 response
282 uri = urlparse.urljoin(base, "300/without-location-header.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000283 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000284 self.assertEqual(response.status, 300)
285 self.assertTrue(response['content-type'].startswith("text/html"))
jcgregorioa0713ab2006-07-01 05:21:34 +0000286 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000287
288 def testGet301(self):
289 # Test that we automatically follow 301 redirects
290 # and that we cache the 301 response
291 uri = urlparse.urljoin(base, "301/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000292 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000293 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000294 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000295 self.assertTrue(response.has_key('content-location'))
296 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000297 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000298 self.assertEqual(response.previous.status, 301)
299 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000300
jcgregorio36140b52006-06-13 02:17:52 +0000301 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000302 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000303 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000304 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000305 self.assertEqual(response.previous.status, 301)
306 self.assertEqual(response.previous.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000307
jcgregorio2f1e1422007-05-03 13:17:33 +0000308
309 def testGet301NoRedirect(self):
310 # Test that we automatically follow 301 redirects
311 # and that we cache the 301 response
312 self.http.follow_redirects = False
313 uri = urlparse.urljoin(base, "301/onestep.asis")
314 destination = urlparse.urljoin(base, "302/final-destination.txt")
315 (response, content) = self.http.request(uri, "GET")
316 self.assertEqual(response.status, 301)
317
318
jcgregorio2d66d4f2006-02-07 05:34:14 +0000319 def testGet302(self):
320 # Test that we automatically follow 302 redirects
321 # and that we DO NOT cache the 302 response
322 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000323 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000324 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000325 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000326 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000327 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000328 self.assertEqual(response.previous.status, 302)
329 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000330
331 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000332 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000333 self.assertEqual(response.status, 200)
334 self.assertEqual(response.fromcache, True)
jcgregorio772adc82006-11-17 21:52:34 +0000335 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000336 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000337 self.assertEqual(response.previous.status, 302)
338 self.assertEqual(response.previous.fromcache, False)
jcgregorio772adc82006-11-17 21:52:34 +0000339 self.assertEqual(response.previous['content-location'], uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000340
341 uri = urlparse.urljoin(base, "302/twostep.asis")
342
jcgregorio36140b52006-06-13 02:17:52 +0000343 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000344 self.assertEqual(response.status, 200)
345 self.assertEqual(response.fromcache, True)
346 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000347 self.assertEqual(response.previous.status, 302)
348 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000349
350 def testGet302RedirectionLimit(self):
351 # Test that we can set a lower redirection limit
352 # and that we raise an exception when we exceed
353 # that limit.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000354 self.http.force_exception_to_status_code = False
355
jcgregorio2d66d4f2006-02-07 05:34:14 +0000356 uri = urlparse.urljoin(base, "302/twostep.asis")
357 try:
jcgregorio36140b52006-06-13 02:17:52 +0000358 (response, content) = self.http.request(uri, "GET", redirections = 1)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000359 self.fail("This should not happen")
360 except httplib2.RedirectLimit:
361 pass
362 except Exception, e:
363 self.fail("Threw wrong kind of exception ")
364
jcgregorio07a9a4a2007-03-08 21:18:39 +0000365 # Re-run the test with out the exceptions
366 self.http.force_exception_to_status_code = True
367
368 (response, content) = self.http.request(uri, "GET", redirections = 1)
369 self.assertEqual(response.status, 500)
370 self.assertTrue(response.reason.startswith("Redirected more"))
371 self.assertEqual("302", response['status'])
372 self.assertTrue(content.startswith("<html>"))
373 self.assertTrue(response.previous != None)
374
jcgregorio2d66d4f2006-02-07 05:34:14 +0000375 def testGet302NoLocation(self):
376 # Test that we throw an exception when we get
377 # a 302 with no Location: header.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000378 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000379 uri = urlparse.urljoin(base, "302/no-location.asis")
380 try:
jcgregorio36140b52006-06-13 02:17:52 +0000381 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000382 self.fail("Should never reach here")
383 except httplib2.RedirectMissingLocation:
384 pass
385 except Exception, e:
386 self.fail("Threw wrong kind of exception ")
387
jcgregorio07a9a4a2007-03-08 21:18:39 +0000388 # Re-run the test with out the exceptions
389 self.http.force_exception_to_status_code = True
390
391 (response, content) = self.http.request(uri, "GET")
392 self.assertEqual(response.status, 500)
393 self.assertTrue(response.reason.startswith("Redirected but"))
394 self.assertEqual("302", response['status'])
395 self.assertTrue(content.startswith("This is content"))
396
jcgregorio2d66d4f2006-02-07 05:34:14 +0000397 def testGet302ViaHttps(self):
jcgregorioadbb4f82006-05-19 15:17:42 +0000398 # Google always redirects to http://google.com
jcgregorio36140b52006-06-13 02:17:52 +0000399 (response, content) = self.http.request("https://google.com", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000400 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000401 self.assertEqual(302, response.previous.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000402
403 def testGetViaHttps(self):
404 # Test that we can handle HTTPS
jcgregorio36140b52006-06-13 02:17:52 +0000405 (response, content) = self.http.request("https://google.com/adsense/", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000406 self.assertEqual(200, response.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000407
408 def testGetViaHttpsSpecViolationOnLocation(self):
409 # Test that we follow redirects through HTTPS
410 # even if they violate the spec by including
411 # a relative Location: header instead of an
412 # absolute one.
jcgregorio36140b52006-06-13 02:17:52 +0000413 (response, content) = self.http.request("https://google.com/adsense", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000414 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000415 self.assertNotEqual(None, response.previous)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000416
jcgregoriode8238d2007-03-07 19:08:26 +0000417
418 def testGetViaHttpsKeyCert(self):
jcgregorio2f1e1422007-05-03 13:17:33 +0000419 # At this point I can only test
420 # that the key and cert files are passed in
421 # correctly to httplib. It would be nice to have
422 # a real https endpoint to test against.
423 http = httplib2.Http(timeout=2)
jcgregoriode8238d2007-03-07 19:08:26 +0000424
425 http.add_certificate("akeyfile", "acertfile", "bitworking.org")
426 try:
427 (response, content) = http.request("https://bitworking.org", "GET")
428 except:
429 pass
430 self.assertEqual(http.connections["https:bitworking.org"].key_file, "akeyfile")
431 self.assertEqual(http.connections["https:bitworking.org"].cert_file, "acertfile")
432
jcgregorio2f1e1422007-05-03 13:17:33 +0000433 try:
434 (response, content) = http.request("https://notthere.bitworking.org", "GET")
435 except:
436 pass
437 self.assertEqual(http.connections["https:notthere.bitworking.org"].key_file, None)
438 self.assertEqual(http.connections["https:notthere.bitworking.org"].cert_file, None)
439
440
441
jcgregoriode8238d2007-03-07 19:08:26 +0000442
jcgregorio2d66d4f2006-02-07 05:34:14 +0000443 def testGet303(self):
444 # Do a follow-up GET on a Location: header
445 # returned from a POST that gave a 303.
446 uri = urlparse.urljoin(base, "303/303.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000447 (response, content) = self.http.request(uri, "POST", " ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000448 self.assertEqual(response.status, 200)
449 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000450 self.assertEqual(response.previous.status, 303)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000451
jcgregorio2f1e1422007-05-03 13:17:33 +0000452 def testGet303NoRedirect(self):
453 # Do a follow-up GET on a Location: header
454 # returned from a POST that gave a 303.
455 self.http.follow_redirects = False
456 uri = urlparse.urljoin(base, "303/303.cgi")
457 (response, content) = self.http.request(uri, "POST", " ")
458 self.assertEqual(response.status, 303)
459
jcgregorio2d66d4f2006-02-07 05:34:14 +0000460 def test303ForDifferentMethods(self):
461 # Test that all methods can be used
462 uri = urlparse.urljoin(base, "303/redirect-to-reflector.cgi")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000463 for (method, method_on_303) in [("PUT", "GET"), ("DELETE", "GET"), ("POST", "GET"), ("GET", "GET"), ("HEAD", "GET")]:
jcgregorio36140b52006-06-13 02:17:52 +0000464 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000465 self.assertEqual(response['x-method'], method_on_303)
466
467 def testGet304(self):
468 # Test that we use ETags properly to validate our cache
469 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000470 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000471 self.assertNotEqual(response['etag'], "")
472
jcgregorio36140b52006-06-13 02:17:52 +0000473 (response, content) = self.http.request(uri, "GET")
474 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'must-revalidate'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000475 self.assertEqual(response.status, 200)
476 self.assertEqual(response.fromcache, True)
477
jcgregorio90fb4a42006-11-17 16:19:47 +0000478 cache_file_name = os.path.join(cacheDirName, httplib2.safename(httplib2.urlnorm(uri)[-1]))
479 f = open(cache_file_name, "r")
480 status_line = f.readline()
481 f.close()
482
483 self.assertTrue(status_line.startswith("status:"))
484
jcgregorio36140b52006-06-13 02:17:52 +0000485 (response, content) = self.http.request(uri, "HEAD")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000486 self.assertEqual(response.status, 200)
487 self.assertEqual(response.fromcache, True)
488
jcgregorio36140b52006-06-13 02:17:52 +0000489 (response, content) = self.http.request(uri, "GET", headers = {'range': 'bytes=0-0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000490 self.assertEqual(response.status, 206)
491 self.assertEqual(response.fromcache, False)
492
jcgregorio25185622006-10-28 05:12:34 +0000493 def testGetIgnoreEtag(self):
494 # Test that we can forcibly ignore ETags
495 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
496 (response, content) = self.http.request(uri, "GET")
497 self.assertNotEqual(response['etag'], "")
498
499 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
500 d = self.reflector(content)
501 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
502
503 self.http.ignore_etag = True
504 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
505 d = self.reflector(content)
506 self.assertEqual(response.fromcache, False)
507 self.assertFalse(d.has_key('HTTP_IF_NONE_MATCH'))
508
jcgregorio4b145e82007-01-18 19:46:34 +0000509 def testOverrideEtag(self):
510 # Test that we can forcibly ignore ETags
511 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
512 (response, content) = self.http.request(uri, "GET")
513 self.assertNotEqual(response['etag'], "")
514
515 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
516 d = self.reflector(content)
517 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
518 self.assertNotEqual(d['HTTP_IF_NONE_MATCH'], "fred")
519
pilgrim00a352e2009-05-29 04:04:44 +0000520 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0', 'if-none-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000521 d = self.reflector(content)
522 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
523 self.assertEqual(d['HTTP_IF_NONE_MATCH'], "fred")
jcgregorio25185622006-10-28 05:12:34 +0000524
pilgrim00a352e2009-05-29 04:04:44 +0000525#MAP-commented this out because it consistently fails
526# def testGet304EndToEnd(self):
527# # Test that end to end headers get overwritten in the cache
528# uri = urlparse.urljoin(base, "304/end2end.cgi")
529# (response, content) = self.http.request(uri, "GET")
530# self.assertNotEqual(response['etag'], "")
531# old_date = response['date']
532# time.sleep(2)
533#
534# (response, content) = self.http.request(uri, "GET", headers = {'Cache-Control': 'max-age=0'})
535# # The response should be from the cache, but the Date: header should be updated.
536# new_date = response['date']
537# self.assertNotEqual(new_date, old_date)
538# self.assertEqual(response.status, 200)
539# self.assertEqual(response.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000540
541 def testGet304LastModified(self):
542 # Test that we can still handle a 304
543 # by only using the last-modified cache validator.
544 uri = urlparse.urljoin(base, "304/last-modified-only/last-modified-only.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000545 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000546
547 self.assertNotEqual(response['last-modified'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000548 (response, content) = self.http.request(uri, "GET")
549 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000550 self.assertEqual(response.status, 200)
551 self.assertEqual(response.fromcache, True)
552
553 def testGet307(self):
554 # Test that we do follow 307 redirects but
555 # do not cache the 307
556 uri = urlparse.urljoin(base, "307/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000557 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000558 self.assertEqual(response.status, 200)
559 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000560 self.assertEqual(response.previous.status, 307)
561 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000562
jcgregorio36140b52006-06-13 02:17:52 +0000563 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000564 self.assertEqual(response.status, 200)
565 self.assertEqual(response.fromcache, True)
566 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000567 self.assertEqual(response.previous.status, 307)
568 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000569
570 def testGet410(self):
571 # Test that we pass 410's through
572 uri = urlparse.urljoin(base, "410/410.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000573 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000574 self.assertEqual(response.status, 410)
575
chris dent89f15142009-12-24 14:02:57 -0600576 def testVaryHeaderSimple(self):
577 """
578 RFC 2616 13.6
579 When the cache receives a subsequent request whose Request-URI
580 specifies one or more cache entries including a Vary header field,
581 the cache MUST NOT use such a cache entry to construct a response
582 to the new request unless all of the selecting request-headers
583 present in the new request match the corresponding stored
584 request-headers in the original request.
585 """
586 # test that the vary header is sent
587 uri = urlparse.urljoin(base, "vary/accept.asis")
588 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
589 self.assertEqual(response.status, 200)
590 self.assertTrue(response.has_key('vary'))
591
592 # get the resource again, from the cache since accept header in this
593 # request is the same as the request
594 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
595 self.assertEqual(response.status, 200)
596 self.assertEqual(response.fromcache, True, msg="Should be from cache")
597
598 # get the resource again, not from cache since Accept headers does not match
599 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
600 self.assertEqual(response.status, 200)
601 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
602
603 # get the resource again, without any Accept header, so again no match
604 (response, content) = self.http.request(uri, "GET")
605 self.assertEqual(response.status, 200)
606 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
607
608 def testNoVary(self):
609 # when there is no vary, a different Accept header (e.g.) should not
610 # impact if the cache is used
611 # test that the vary header is not sent
612 uri = urlparse.urljoin(base, "vary/no-vary.asis")
613 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
614 self.assertEqual(response.status, 200)
615 self.assertFalse(response.has_key('vary'))
616
617 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
618 self.assertEqual(response.status, 200)
619 self.assertEqual(response.fromcache, True, msg="Should be from cache")
620
621 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
622 self.assertEqual(response.status, 200)
623 self.assertEqual(response.fromcache, True, msg="Should be from cache")
624
625 def testVaryHeaderDouble(self):
626 uri = urlparse.urljoin(base, "vary/accept-double.asis")
627 (response, content) = self.http.request(uri, "GET", headers={
628 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
629 self.assertEqual(response.status, 200)
630 self.assertTrue(response.has_key('vary'))
631
632 # we are from cache
633 (response, content) = self.http.request(uri, "GET", headers={
634 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
635 self.assertEqual(response.fromcache, True, msg="Should be from cache")
636
637 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
638 self.assertEqual(response.status, 200)
639 self.assertEqual(response.fromcache, False)
640
641 # get the resource again, not from cache, varied headers don't match exact
642 (response, content) = self.http.request(uri, "GET", headers={'Accept-Language': 'da'})
643 self.assertEqual(response.status, 200)
644 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
645
646
joe.gregorio0d4a2b82007-10-23 14:28:35 +0000647 def testHeadGZip(self):
648 # Test that we don't try to decompress a HEAD response
649 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
650 (response, content) = self.http.request(uri, "HEAD")
651 self.assertEqual(response.status, 200)
652 self.assertNotEqual(int(response['content-length']), 0)
653 self.assertEqual(content, "")
654
jcgregorio2d66d4f2006-02-07 05:34:14 +0000655 def testGetGZip(self):
656 # Test that we support gzip compression
657 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000658 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000659 self.assertEqual(response.status, 200)
jcgregorio90fb4a42006-11-17 16:19:47 +0000660 self.assertFalse(response.has_key('content-encoding'))
joe.gregorio8b6d2312007-12-16 05:42:07 +0000661 self.assertTrue(response.has_key('-content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000662 self.assertEqual(int(response['content-length']), len("This is the final destination.\n"))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000663 self.assertEqual(content, "This is the final destination.\n")
664
665 def testGetGZipFailure(self):
666 # Test that we raise a good exception when the gzip fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000667 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000668 uri = urlparse.urljoin(base, "gzip/failed-compression.asis")
669 try:
jcgregorio36140b52006-06-13 02:17:52 +0000670 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000671 self.fail("Should never reach here")
672 except httplib2.FailedToDecompressContent:
673 pass
674 except Exception:
675 self.fail("Threw wrong kind of exception")
676
jcgregorio07a9a4a2007-03-08 21:18:39 +0000677 # Re-run the test with out the exceptions
678 self.http.force_exception_to_status_code = True
679
680 (response, content) = self.http.request(uri, "GET")
681 self.assertEqual(response.status, 500)
682 self.assertTrue(response.reason.startswith("Content purported"))
683
684 def testTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000685 self.http.force_exception_to_status_code = True
jcgregorio07a9a4a2007-03-08 21:18:39 +0000686 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
687 try:
688 import socket
689 socket.setdefaulttimeout(1)
690 except:
691 # Don't run the test if we can't set the timeout
692 return
693 (response, content) = self.http.request(uri)
694 self.assertEqual(response.status, 408)
695 self.assertTrue(response.reason.startswith("Request Timeout"))
696 self.assertTrue(content.startswith("Request Timeout"))
697
jcgregoriob2697912007-03-09 02:23:47 +0000698 def testIndividualTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000699 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
700 http = httplib2.Http(timeout=1)
joe.gregoriof28536d2007-10-23 14:10:11 +0000701 http.force_exception_to_status_code = True
jcgregoriob2697912007-03-09 02:23:47 +0000702
703 (response, content) = http.request(uri)
704 self.assertEqual(response.status, 408)
705 self.assertTrue(response.reason.startswith("Request Timeout"))
706 self.assertTrue(content.startswith("Request Timeout"))
707
jcgregorio07a9a4a2007-03-08 21:18:39 +0000708
Joe Gregorio1a7609f2009-07-16 10:59:44 -0400709 def testHTTPSInitTimeout(self):
710 c = httplib2.HTTPSConnectionWithTimeout('localhost', 80, timeout=47)
711 self.assertEqual(47, c.timeout)
712
jcgregorio2d66d4f2006-02-07 05:34:14 +0000713 def testGetDeflate(self):
714 # Test that we support deflate compression
715 uri = urlparse.urljoin(base, "deflate/deflated.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000716 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000717 self.assertEqual(response.status, 200)
jcgregorio90fb4a42006-11-17 16:19:47 +0000718 self.assertFalse(response.has_key('content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000719 self.assertEqual(int(response['content-length']), len("This is the final destination."))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000720 self.assertEqual(content, "This is the final destination.")
721
722 def testGetDeflateFailure(self):
723 # Test that we raise a good exception when the deflate fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000724 self.http.force_exception_to_status_code = False
725
jcgregorio2d66d4f2006-02-07 05:34:14 +0000726 uri = urlparse.urljoin(base, "deflate/failed-compression.asis")
727 try:
jcgregorio36140b52006-06-13 02:17:52 +0000728 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000729 self.fail("Should never reach here")
730 except httplib2.FailedToDecompressContent:
731 pass
732 except Exception:
733 self.fail("Threw wrong kind of exception")
734
jcgregorio07a9a4a2007-03-08 21:18:39 +0000735 # Re-run the test with out the exceptions
736 self.http.force_exception_to_status_code = True
737
738 (response, content) = self.http.request(uri, "GET")
739 self.assertEqual(response.status, 500)
740 self.assertTrue(response.reason.startswith("Content purported"))
741
jcgregorio2d66d4f2006-02-07 05:34:14 +0000742 def testGetDuplicateHeaders(self):
743 # Test that duplicate headers get concatenated via ','
744 uri = urlparse.urljoin(base, "duplicate-headers/multilink.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000745 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000746 self.assertEqual(response.status, 200)
747 self.assertEqual(content, "This is content\n")
748 self.assertEqual(response['link'].split(",")[0], '<http://bitworking.org>; rel="home"; title="BitWorking"')
749
750 def testGetCacheControlNoCache(self):
751 # Test Cache-Control: no-cache on requests
752 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000753 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000754 self.assertNotEqual(response['etag'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000755 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000756 self.assertEqual(response.status, 200)
757 self.assertEqual(response.fromcache, True)
758
jcgregorio36140b52006-06-13 02:17:52 +0000759 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000760 self.assertEqual(response.status, 200)
761 self.assertEqual(response.fromcache, False)
762
763 def testGetCacheControlPragmaNoCache(self):
764 # Test Pragma: no-cache on requests
765 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000766 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000767 self.assertNotEqual(response['etag'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000768 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000769 self.assertEqual(response.status, 200)
770 self.assertEqual(response.fromcache, True)
771
jcgregorio36140b52006-06-13 02:17:52 +0000772 (response, content) = self.http.request(uri, "GET", headers={'Pragma': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000773 self.assertEqual(response.status, 200)
774 self.assertEqual(response.fromcache, False)
775
776 def testGetCacheControlNoStoreRequest(self):
777 # A no-store request means that the response should not be stored.
778 uri = urlparse.urljoin(base, "304/test_etag.txt")
779
jcgregorio36140b52006-06-13 02:17:52 +0000780 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000781 self.assertEqual(response.status, 200)
782 self.assertEqual(response.fromcache, False)
783
jcgregorio36140b52006-06-13 02:17:52 +0000784 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000785 self.assertEqual(response.status, 200)
786 self.assertEqual(response.fromcache, False)
787
788 def testGetCacheControlNoStoreResponse(self):
789 # A no-store response means that the response should not be stored.
790 uri = urlparse.urljoin(base, "no-store/no-store.asis")
791
jcgregorio36140b52006-06-13 02:17:52 +0000792 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000793 self.assertEqual(response.status, 200)
794 self.assertEqual(response.fromcache, False)
795
jcgregorio36140b52006-06-13 02:17:52 +0000796 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000797 self.assertEqual(response.status, 200)
798 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000799
800 def testGetCacheControlNoCacheNoStoreRequest(self):
801 # Test that a no-store, no-cache clears the entry from the cache
802 # even if it was cached previously.
803 uri = urlparse.urljoin(base, "304/test_etag.txt")
804
jcgregorio36140b52006-06-13 02:17:52 +0000805 (response, content) = self.http.request(uri, "GET")
806 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000807 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000808 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
809 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000810 self.assertEqual(response.status, 200)
811 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000812
813 def testUpdateInvalidatesCache(self):
814 # Test that calling PUT or DELETE on a
815 # URI that is cache invalidates that cache.
816 uri = urlparse.urljoin(base, "304/test_etag.txt")
817
jcgregorio36140b52006-06-13 02:17:52 +0000818 (response, content) = self.http.request(uri, "GET")
819 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000820 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000821 (response, content) = self.http.request(uri, "DELETE")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000822 self.assertEqual(response.status, 405)
823
jcgregorio36140b52006-06-13 02:17:52 +0000824 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000825 self.assertEqual(response.fromcache, False)
826
827 def testUpdateUsesCachedETag(self):
828 # Test that we natively support http://www.w3.org/1999/04/Editing/
829 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
830
jcgregorio36140b52006-06-13 02:17:52 +0000831 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000832 self.assertEqual(response.status, 200)
833 self.assertEqual(response.fromcache, False)
jcgregorio36140b52006-06-13 02:17:52 +0000834 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000835 self.assertEqual(response.status, 200)
836 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400837 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000838 self.assertEqual(response.status, 200)
Joe Gregoriocd868102009-09-29 17:09:16 -0400839 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000840 self.assertEqual(response.status, 412)
841
joe.gregorio700f04d2008-09-06 04:46:32 +0000842 def testUpdateUsesCachedETagAndOCMethod(self):
843 # Test that we natively support http://www.w3.org/1999/04/Editing/
844 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
845
846 (response, content) = self.http.request(uri, "GET")
847 self.assertEqual(response.status, 200)
848 self.assertEqual(response.fromcache, False)
849 (response, content) = self.http.request(uri, "GET")
850 self.assertEqual(response.status, 200)
851 self.assertEqual(response.fromcache, True)
852 self.http.optimistic_concurrency_methods.append("DELETE")
853 (response, content) = self.http.request(uri, "DELETE")
854 self.assertEqual(response.status, 200)
855
856
jcgregorio4b145e82007-01-18 19:46:34 +0000857 def testUpdateUsesCachedETagOverridden(self):
858 # Test that we natively support http://www.w3.org/1999/04/Editing/
859 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
860
861 (response, content) = self.http.request(uri, "GET")
862 self.assertEqual(response.status, 200)
863 self.assertEqual(response.fromcache, False)
864 (response, content) = self.http.request(uri, "GET")
865 self.assertEqual(response.status, 200)
866 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400867 (response, content) = self.http.request(uri, "PUT", body="foo", headers={'if-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000868 self.assertEqual(response.status, 412)
869
jcgregorio2d66d4f2006-02-07 05:34:14 +0000870 def testBasicAuth(self):
871 # Test Basic Authentication
872 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000873 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000874 self.assertEqual(response.status, 401)
875
876 uri = urlparse.urljoin(base, "basic/")
jcgregorio36140b52006-06-13 02:17:52 +0000877 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000878 self.assertEqual(response.status, 401)
879
jcgregorio36140b52006-06-13 02:17:52 +0000880 self.http.add_credentials('joe', 'password')
881 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000882 self.assertEqual(response.status, 200)
883
884 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000885 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000886 self.assertEqual(response.status, 200)
887
jcgregoriode8238d2007-03-07 19:08:26 +0000888 def testBasicAuthWithDomain(self):
889 # Test Basic Authentication
890 uri = urlparse.urljoin(base, "basic/file.txt")
891 (response, content) = self.http.request(uri, "GET")
892 self.assertEqual(response.status, 401)
893
894 uri = urlparse.urljoin(base, "basic/")
895 (response, content) = self.http.request(uri, "GET")
896 self.assertEqual(response.status, 401)
897
898 self.http.add_credentials('joe', 'password', "example.org")
899 (response, content) = self.http.request(uri, "GET")
900 self.assertEqual(response.status, 401)
901
902 uri = urlparse.urljoin(base, "basic/file.txt")
903 (response, content) = self.http.request(uri, "GET")
904 self.assertEqual(response.status, 401)
905
906 domain = urlparse.urlparse(base)[1]
907 self.http.add_credentials('joe', 'password', domain)
908 (response, content) = self.http.request(uri, "GET")
909 self.assertEqual(response.status, 200)
910
911 uri = urlparse.urljoin(base, "basic/file.txt")
912 (response, content) = self.http.request(uri, "GET")
913 self.assertEqual(response.status, 200)
914
915
916
917
918
919
jcgregorio2d66d4f2006-02-07 05:34:14 +0000920 def testBasicAuthTwoDifferentCredentials(self):
jcgregorioadbb4f82006-05-19 15:17:42 +0000921 # Test Basic Authentication with multiple sets of credentials
jcgregorio2d66d4f2006-02-07 05:34:14 +0000922 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000923 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000924 self.assertEqual(response.status, 401)
925
926 uri = urlparse.urljoin(base, "basic2/")
jcgregorio36140b52006-06-13 02:17:52 +0000927 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000928 self.assertEqual(response.status, 401)
929
jcgregorio36140b52006-06-13 02:17:52 +0000930 self.http.add_credentials('fred', 'barney')
931 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000932 self.assertEqual(response.status, 200)
933
934 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000935 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000936 self.assertEqual(response.status, 200)
937
938 def testBasicAuthNested(self):
939 # Test Basic Authentication with resources
940 # that are nested
941 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +0000942 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000943 self.assertEqual(response.status, 401)
944
945 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +0000946 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000947 self.assertEqual(response.status, 401)
948
jcgregorioadbb4f82006-05-19 15:17:42 +0000949 # Now add in credentials one at a time and test.
jcgregorio36140b52006-06-13 02:17:52 +0000950 self.http.add_credentials('joe', 'password')
jcgregorio2d66d4f2006-02-07 05:34:14 +0000951
952 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +0000953 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000954 self.assertEqual(response.status, 200)
955
956 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +0000957 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000958 self.assertEqual(response.status, 401)
959
jcgregorio36140b52006-06-13 02:17:52 +0000960 self.http.add_credentials('fred', 'barney')
jcgregorio2d66d4f2006-02-07 05:34:14 +0000961
962 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +0000963 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000964 self.assertEqual(response.status, 200)
965
966 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +0000967 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000968 self.assertEqual(response.status, 200)
969
970 def testDigestAuth(self):
971 # Test that we support Digest Authentication
972 uri = urlparse.urljoin(base, "digest/")
jcgregorio36140b52006-06-13 02:17:52 +0000973 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000974 self.assertEqual(response.status, 401)
975
jcgregorio36140b52006-06-13 02:17:52 +0000976 self.http.add_credentials('joe', 'password')
977 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000978 self.assertEqual(response.status, 200)
979
980 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000981 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000982
983 def testDigestAuthNextNonceAndNC(self):
984 # Test that if the server sets nextnonce that we reset
985 # the nonce count back to 1
986 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000987 self.http.add_credentials('joe', 'password')
988 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000989 info = httplib2._parse_www_authenticate(response, 'authentication-info')
990 self.assertEqual(response.status, 200)
jcgregorio36140b52006-06-13 02:17:52 +0000991 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000992 info2 = httplib2._parse_www_authenticate(response, 'authentication-info')
993 self.assertEqual(response.status, 200)
994
995 if info.has_key('nextnonce'):
996 self.assertEqual(info2['nc'], 1)
997
998 def testDigestAuthStale(self):
999 # Test that we can handle a nonce becoming stale
1000 uri = urlparse.urljoin(base, "digest-expire/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001001 self.http.add_credentials('joe', 'password')
1002 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001003 info = httplib2._parse_www_authenticate(response, 'authentication-info')
1004 self.assertEqual(response.status, 200)
1005
1006 time.sleep(3)
1007 # Sleep long enough that the nonce becomes stale
1008
jcgregorio36140b52006-06-13 02:17:52 +00001009 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001010 self.assertFalse(response.fromcache)
1011 self.assertTrue(response._stale_digest)
1012 info3 = httplib2._parse_www_authenticate(response, 'authentication-info')
1013 self.assertEqual(response.status, 200)
1014
1015 def reflector(self, content):
jcgregorio25185622006-10-28 05:12:34 +00001016 return dict( [tuple(x.split("=", 1)) for x in content.strip().split("\n")] )
jcgregorio2d66d4f2006-02-07 05:34:14 +00001017
1018 def testReflector(self):
1019 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +00001020 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001021 d = self.reflector(content)
1022 self.assertTrue(d.has_key('HTTP_USER_AGENT'))
1023
Joe Gregorio84cc10a2009-09-01 13:02:49 -04001024 def testConnectionClose(self):
1025 uri = "http://www.google.com/"
1026 (response, content) = self.http.request(uri, "GET")
1027 for c in self.http.connections.values():
1028 self.assertNotEqual(None, c.sock)
1029 (response, content) = self.http.request(uri, "GET", headers={"connection": "close"})
1030 for c in self.http.connections.values():
1031 self.assertEqual(None, c.sock)
1032
1033
jcgregorio36140b52006-06-13 02:17:52 +00001034try:
1035 import memcache
1036 class HttpTestMemCached(HttpTest):
1037 def setUp(self):
1038 self.cache = memcache.Client(['127.0.0.1:11211'], debug=0)
jcgregorio47d24672006-06-29 05:18:59 +00001039 #self.cache = memcache.Client(['10.0.0.4:11211'], debug=1)
jcgregorio36140b52006-06-13 02:17:52 +00001040 self.http = httplib2.Http(self.cache)
1041 self.cache.flush_all()
jcgregorio47d24672006-06-29 05:18:59 +00001042 # Not exactly sure why the sleep is needed here, but
1043 # if not present then some unit tests that rely on caching
1044 # fail. Memcached seems to lose some sets immediately
1045 # after a flush_all if the set is to a value that
1046 # was previously cached. (Maybe the flush is handled async?)
1047 time.sleep(1)
jcgregorio36140b52006-06-13 02:17:52 +00001048 self.http.clear_credentials()
1049except:
1050 pass
1051
1052
1053
chris dent89f15142009-12-24 14:02:57 -06001054
jcgregoriodb8dfc82006-03-31 14:59:46 +00001055# ------------------------------------------------------------------------
jcgregorio2d66d4f2006-02-07 05:34:14 +00001056
1057class HttpPrivateTest(unittest.TestCase):
1058
1059 def testParseCacheControl(self):
1060 # Test that we can parse the Cache-Control header
1061 self.assertEqual({}, httplib2._parse_cache_control({}))
1062 self.assertEqual({'no-cache': 1}, httplib2._parse_cache_control({'cache-control': ' no-cache'}))
1063 cc = httplib2._parse_cache_control({'cache-control': ' no-cache, max-age = 7200'})
1064 self.assertEqual(cc['no-cache'], 1)
1065 self.assertEqual(cc['max-age'], '7200')
1066 cc = httplib2._parse_cache_control({'cache-control': ' , '})
1067 self.assertEqual(cc[''], 1)
1068
Joe Gregorioe314e8b2009-07-16 20:11:28 -04001069 try:
1070 cc = httplib2._parse_cache_control({'cache-control': 'Max-age=3600;post-check=1800,pre-check=3600'})
1071 self.assertTrue("max-age" in cc)
1072 except:
1073 self.fail("Should not throw exception")
1074
jcgregorio2d66d4f2006-02-07 05:34:14 +00001075 def testNormalizeHeaders(self):
1076 # Test that we normalize headers to lowercase
1077 h = httplib2._normalize_headers({'Cache-Control': 'no-cache', 'Other': 'Stuff'})
1078 self.assertTrue(h.has_key('cache-control'))
1079 self.assertTrue(h.has_key('other'))
1080 self.assertEqual('Stuff', h['other'])
1081
1082 def testExpirationModelTransparent(self):
1083 # Test that no-cache makes our request TRANSPARENT
1084 response_headers = {
1085 'cache-control': 'max-age=7200'
1086 }
1087 request_headers = {
1088 'cache-control': 'no-cache'
1089 }
1090 self.assertEqual("TRANSPARENT", httplib2._entry_disposition(response_headers, request_headers))
1091
jcgregorio45865012007-01-18 16:38:22 +00001092 def testMaxAgeNonNumeric(self):
1093 # Test that no-cache makes our request TRANSPARENT
1094 response_headers = {
1095 'cache-control': 'max-age=fred, min-fresh=barney'
1096 }
1097 request_headers = {
1098 }
1099 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1100
1101
jcgregorio2d66d4f2006-02-07 05:34:14 +00001102 def testExpirationModelNoCacheResponse(self):
1103 # The date and expires point to an entry that should be
1104 # FRESH, but the no-cache over-rides that.
1105 now = time.time()
1106 response_headers = {
1107 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1108 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1109 'cache-control': 'no-cache'
1110 }
1111 request_headers = {
1112 }
1113 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1114
1115 def testExpirationModelStaleRequestMustReval(self):
1116 # must-revalidate forces STALE
1117 self.assertEqual("STALE", httplib2._entry_disposition({}, {'cache-control': 'must-revalidate'}))
1118
1119 def testExpirationModelStaleResponseMustReval(self):
1120 # must-revalidate forces STALE
1121 self.assertEqual("STALE", httplib2._entry_disposition({'cache-control': 'must-revalidate'}, {}))
1122
1123 def testExpirationModelFresh(self):
1124 response_headers = {
1125 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1126 'cache-control': 'max-age=2'
1127 }
1128 request_headers = {
1129 }
1130 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1131 time.sleep(3)
1132 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1133
1134 def testExpirationMaxAge0(self):
1135 response_headers = {
1136 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1137 'cache-control': 'max-age=0'
1138 }
1139 request_headers = {
1140 }
1141 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1142
1143 def testExpirationModelDateAndExpires(self):
1144 now = time.time()
1145 response_headers = {
1146 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1147 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1148 }
1149 request_headers = {
1150 }
1151 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1152 time.sleep(3)
1153 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1154
jcgregoriof9511052007-06-01 14:56:34 +00001155 def testExpiresZero(self):
1156 now = time.time()
1157 response_headers = {
1158 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1159 'expires': "0",
1160 }
1161 request_headers = {
1162 }
1163 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1164
jcgregorio2d66d4f2006-02-07 05:34:14 +00001165 def testExpirationModelDateOnly(self):
1166 now = time.time()
1167 response_headers = {
1168 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+3)),
1169 }
1170 request_headers = {
1171 }
1172 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1173
1174 def testExpirationModelOnlyIfCached(self):
1175 response_headers = {
1176 }
1177 request_headers = {
1178 'cache-control': 'only-if-cached',
1179 }
1180 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1181
1182 def testExpirationModelMaxAgeBoth(self):
1183 now = time.time()
1184 response_headers = {
1185 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1186 'cache-control': 'max-age=2'
1187 }
1188 request_headers = {
1189 'cache-control': 'max-age=0'
1190 }
1191 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1192
1193 def testExpirationModelDateAndExpiresMinFresh1(self):
1194 now = time.time()
1195 response_headers = {
1196 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1197 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1198 }
1199 request_headers = {
1200 'cache-control': 'min-fresh=2'
1201 }
1202 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1203
1204 def testExpirationModelDateAndExpiresMinFresh2(self):
1205 now = time.time()
1206 response_headers = {
1207 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1208 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1209 }
1210 request_headers = {
1211 'cache-control': 'min-fresh=2'
1212 }
1213 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1214
1215 def testParseWWWAuthenticateEmpty(self):
1216 res = httplib2._parse_www_authenticate({})
1217 self.assertEqual(len(res.keys()), 0)
1218
jcgregoriofd22e432006-04-27 02:00:08 +00001219 def testParseWWWAuthenticate(self):
1220 # different uses of spaces around commas
1221 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="test realm" , foo=foo ,bar="bar", baz=baz,qux=qux'})
1222 self.assertEqual(len(res.keys()), 1)
1223 self.assertEqual(len(res['test'].keys()), 5)
1224
1225 # tokens with non-alphanum
1226 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'T*!%#st realm=to*!%#en, to*!%#en="quoted string"'})
1227 self.assertEqual(len(res.keys()), 1)
1228 self.assertEqual(len(res['t*!%#st'].keys()), 2)
1229
1230 # quoted string with quoted pairs
1231 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="a \\"test\\" realm"'})
1232 self.assertEqual(len(res.keys()), 1)
1233 self.assertEqual(res['test']['realm'], 'a "test" realm')
1234
1235 def testParseWWWAuthenticateStrict(self):
1236 httplib2.USE_WWW_AUTH_STRICT_PARSING = 1;
1237 self.testParseWWWAuthenticate();
1238 httplib2.USE_WWW_AUTH_STRICT_PARSING = 0;
1239
jcgregorio2d66d4f2006-02-07 05:34:14 +00001240 def testParseWWWAuthenticateBasic(self):
1241 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me"'})
1242 basic = res['basic']
1243 self.assertEqual('me', basic['realm'])
1244
1245 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm="MD5"'})
1246 basic = res['basic']
1247 self.assertEqual('me', basic['realm'])
1248 self.assertEqual('MD5', basic['algorithm'])
1249
1250 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm=MD5'})
1251 basic = res['basic']
1252 self.assertEqual('me', basic['realm'])
1253 self.assertEqual('MD5', basic['algorithm'])
1254
1255 def testParseWWWAuthenticateBasic2(self):
1256 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me",other="fred" '})
1257 basic = res['basic']
1258 self.assertEqual('me', basic['realm'])
1259 self.assertEqual('fred', basic['other'])
1260
1261 def testParseWWWAuthenticateBasic3(self):
1262 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic REAlm="me" '})
1263 basic = res['basic']
1264 self.assertEqual('me', basic['realm'])
1265
1266
1267 def testParseWWWAuthenticateDigest(self):
1268 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1269 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"'})
1270 digest = res['digest']
1271 self.assertEqual('testrealm@host.com', digest['realm'])
1272 self.assertEqual('auth,auth-int', digest['qop'])
1273
1274
1275 def testParseWWWAuthenticateMultiple(self):
1276 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1277 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41" Basic REAlm="me" '})
1278 digest = res['digest']
1279 self.assertEqual('testrealm@host.com', digest['realm'])
1280 self.assertEqual('auth,auth-int', digest['qop'])
1281 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1282 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1283 basic = res['basic']
1284 self.assertEqual('me', basic['realm'])
1285
1286 def testParseWWWAuthenticateMultiple2(self):
1287 # Handle an added comma between challenges, which might get thrown in if the challenges were
1288 # originally sent in separate www-authenticate headers.
1289 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1290 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me" '})
1291 digest = res['digest']
1292 self.assertEqual('testrealm@host.com', digest['realm'])
1293 self.assertEqual('auth,auth-int', digest['qop'])
1294 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1295 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1296 basic = res['basic']
1297 self.assertEqual('me', basic['realm'])
1298
1299 def testParseWWWAuthenticateMultiple3(self):
1300 # Handle an added comma between challenges, which might get thrown in if the challenges were
1301 # originally sent in separate www-authenticate headers.
1302 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1303 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1304 digest = res['digest']
1305 self.assertEqual('testrealm@host.com', digest['realm'])
1306 self.assertEqual('auth,auth-int', digest['qop'])
1307 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1308 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1309 basic = res['basic']
1310 self.assertEqual('me', basic['realm'])
1311 wsse = res['wsse']
1312 self.assertEqual('foo', wsse['realm'])
1313 self.assertEqual('UsernameToken', wsse['profile'])
1314
1315 def testParseWWWAuthenticateMultiple4(self):
1316 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1317 'Digest realm="test-real.m@host.com", qop \t=\t"\tauth,auth-int", nonce="(*)&^&$%#",opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1318 digest = res['digest']
1319 self.assertEqual('test-real.m@host.com', digest['realm'])
1320 self.assertEqual('\tauth,auth-int', digest['qop'])
1321 self.assertEqual('(*)&^&$%#', digest['nonce'])
1322
1323 def testParseWWWAuthenticateMoreQuoteCombos(self):
1324 res = httplib2._parse_www_authenticate({'www-authenticate':'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'})
1325 digest = res['digest']
1326 self.assertEqual('myrealm', digest['realm'])
1327
1328 def testDigestObject(self):
1329 credentials = ('joe', 'password')
1330 host = None
1331 request_uri = '/projects/httplib2/test/digest/'
1332 headers = {}
1333 response = {
1334 'www-authenticate': 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth"'
1335 }
1336 content = ""
1337
jcgregorio6cbab7e2006-04-21 20:35:43 +00001338 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001339 d.request("GET", request_uri, headers, content, cnonce="33033375ec278a46")
1340 our_request = "Authorization: %s" % headers['Authorization']
1341 working_request = 'Authorization: Digest username="joe", realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", uri="/projects/httplib2/test/digest/", algorithm=MD5, response="97ed129401f7cdc60e5db58a80f3ea8b", qop=auth, nc=00000001, cnonce="33033375ec278a46"'
1342 self.assertEqual(our_request, working_request)
1343
1344
1345 def testDigestObjectStale(self):
1346 credentials = ('joe', 'password')
1347 host = None
1348 request_uri = '/projects/httplib2/test/digest/'
1349 headers = {}
1350 response = httplib2.Response({ })
1351 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1352 response.status = 401
1353 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001354 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001355 # Returns true to force a retry
1356 self.assertTrue( d.response(response, content) )
1357
1358 def testDigestObjectAuthInfo(self):
1359 credentials = ('joe', 'password')
1360 host = None
1361 request_uri = '/projects/httplib2/test/digest/'
1362 headers = {}
1363 response = httplib2.Response({ })
1364 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1365 response['authentication-info'] = 'nextnonce="fred"'
1366 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001367 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001368 # Returns true to force a retry
1369 self.assertFalse( d.response(response, content) )
1370 self.assertEqual('fred', d.challenge['nonce'])
1371 self.assertEqual(1, d.challenge['nc'])
1372
1373 def testWsseAlgorithm(self):
1374 digest = httplib2._wsse_username_token("d36e316282959a9ed4c89851497a717f", "2003-12-15T14:43:07Z", "taadtaadpstcsm")
1375 expected = "quR/EWLAV4xLf9Zqyw4pDmfV9OY="
1376 self.assertEqual(expected, digest)
1377
jcgregoriodb8dfc82006-03-31 14:59:46 +00001378 def testEnd2End(self):
1379 # one end to end header
1380 response = {'content-type': 'application/atom+xml', 'te': 'deflate'}
1381 end2end = httplib2._get_end2end_headers(response)
1382 self.assertTrue('content-type' in end2end)
1383 self.assertTrue('te' not in end2end)
1384 self.assertTrue('connection' not in end2end)
1385
1386 # one end to end header that gets eliminated
1387 response = {'connection': 'content-type', 'content-type': 'application/atom+xml', 'te': 'deflate'}
1388 end2end = httplib2._get_end2end_headers(response)
1389 self.assertTrue('content-type' not in end2end)
1390 self.assertTrue('te' not in end2end)
1391 self.assertTrue('connection' not in end2end)
1392
1393 # Degenerate case of no headers
1394 response = {}
1395 end2end = httplib2._get_end2end_headers(response)
1396 self.assertEquals(0, len(end2end))
1397
1398 # Degenerate case of connection referrring to a header not passed in
1399 response = {'connection': 'content-type'}
1400 end2end = httplib2._get_end2end_headers(response)
1401 self.assertEquals(0, len(end2end))
jcgregorio2d66d4f2006-02-07 05:34:14 +00001402
chris dent89f15142009-12-24 14:02:57 -06001403if __name__ == '__main__':
1404 unittest.main()