blob: 1c5c49f90bd04871f2212c2f80ceedfda5bbb6e9 [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
Joe Gregoriob6c90c42011-02-11 01:03:22 -050018import StringIO
19import base64
jcgregoriode8238d2007-03-07 19:08:26 +000020import httplib
21import httplib2
22import os
Joe Gregoriob6c90c42011-02-11 01:03:22 -050023import socket
24import sys
jcgregoriode8238d2007-03-07 19:08:26 +000025import time
Joe Gregoriob6c90c42011-02-11 01:03:22 -050026import unittest
27import urlparse
jcgregorio8421f272006-02-14 18:19:51 +000028
29# Python 2.3 support
30if not hasattr(unittest.TestCase, 'assertTrue'):
31 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
32 unittest.TestCase.assertFalse = unittest.TestCase.failIf
33
jcgregorio2d66d4f2006-02-07 05:34:14 +000034# The test resources base uri
35base = 'http://bitworking.org/projects/httplib2/test/'
36#base = 'http://localhost/projects/httplib2/test/'
jcgregorio90fb4a42006-11-17 16:19:47 +000037cacheDirName = ".cache"
jcgregorio2d66d4f2006-02-07 05:34:14 +000038
jcgregoriode8238d2007-03-07 19:08:26 +000039
40class CredentialsTest(unittest.TestCase):
41 def test(self):
42 c = httplib2.Credentials()
43 c.add("joe", "password")
44 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
45 self.assertEqual(("joe", "password"), list(c.iter(""))[0])
46 c.add("fred", "password2", "wellformedweb.org")
47 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
48 self.assertEqual(1, len(list(c.iter("bitworking.org"))))
49 self.assertEqual(2, len(list(c.iter("wellformedweb.org"))))
50 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
51 c.clear()
52 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
53 c.add("fred", "password2", "wellformedweb.org")
54 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
55 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
56 self.assertEqual(0, len(list(c.iter(""))))
57
58
jcgregorio2d66d4f2006-02-07 05:34:14 +000059class ParserTest(unittest.TestCase):
60 def testFromStd66(self):
61 self.assertEqual( ('http', 'example.com', '', None, None ), httplib2.parse_uri("http://example.com"))
62 self.assertEqual( ('https', 'example.com', '', None, None ), httplib2.parse_uri("https://example.com"))
63 self.assertEqual( ('https', 'example.com:8080', '', None, None ), httplib2.parse_uri("https://example.com:8080"))
64 self.assertEqual( ('http', 'example.com', '/', None, None ), httplib2.parse_uri("http://example.com/"))
65 self.assertEqual( ('http', 'example.com', '/path', None, None ), httplib2.parse_uri("http://example.com/path"))
66 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', None ), httplib2.parse_uri("http://example.com/path?a=1&b=2"))
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 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', 'fred' ), httplib2.parse_uri("http://example.com/path?a=1&b=2#fred"))
69
jcgregorio2d66d4f2006-02-07 05:34:14 +000070
jcgregorioa46fe4e2006-11-16 04:13:45 +000071class UrlNormTest(unittest.TestCase):
72 def test(self):
73 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://example.org")[-1])
74 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://EXAMple.org")[-1])
75 self.assertEqual( "http://example.org/?=b", httplib2.urlnorm("http://EXAMple.org?=b")[-1])
76 self.assertEqual( "http://example.org/mypath?a=b", httplib2.urlnorm("http://EXAMple.org/mypath?a=b")[-1])
77 self.assertEqual( "http://localhost:80/", httplib2.urlnorm("http://localhost:80")[-1])
jcgregoriob4e9ab02006-11-17 15:53:15 +000078 self.assertEqual( httplib2.urlnorm("http://localhost:80/"), httplib2.urlnorm("HTTP://LOCALHOST:80"))
jcgregorio132d28e2007-01-23 16:22:53 +000079 try:
80 httplib2.urlnorm("/")
81 self.fail("Non-absolute URIs should raise an exception")
82 except httplib2.RelativeURIError:
83 pass
jcgregorioa46fe4e2006-11-16 04:13:45 +000084
85class UrlSafenameTest(unittest.TestCase):
86 def test(self):
87 # Test that different URIs end up generating different safe names
88 self.assertEqual( "example.org,fred,a=b,58489f63a7a83c3b7794a6a398ee8b1f", httplib2.safename("http://example.org/fred/?a=b"))
89 self.assertEqual( "example.org,fred,a=b,8c5946d56fec453071f43329ff0be46b", httplib2.safename("http://example.org/fred?/a=b"))
90 self.assertEqual( "www.example.org,fred,a=b,499c44b8d844a011b67ea2c015116968", httplib2.safename("http://www.example.org/fred?/a=b"))
91 self.assertEqual( httplib2.safename(httplib2.urlnorm("http://www")[-1]), httplib2.safename(httplib2.urlnorm("http://WWW")[-1]))
92 self.assertEqual( "www.example.org,fred,a=b,692e843a333484ce0095b070497ab45d", httplib2.safename("https://www.example.org/fred?/a=b"))
93 self.assertNotEqual( httplib2.safename("http://www"), httplib2.safename("https://www"))
94 # Test the max length limits
95 uri = "http://" + ("w" * 200) + ".org"
96 uri2 = "http://" + ("w" * 201) + ".org"
97 self.assertNotEqual( httplib2.safename(uri2), httplib2.safename(uri))
98 # Max length should be 200 + 1 (",") + 32
99 self.assertEqual(233, len(httplib2.safename(uri2)))
100 self.assertEqual(233, len(httplib2.safename(uri)))
101 # Unicode
jcgregoriodebceec2006-12-12 20:26:02 +0000102 if sys.version_info >= (2,3):
103 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 +0000104
jcgregorio14644372007-07-30 14:13:37 +0000105class _MyResponse(StringIO.StringIO):
106 def __init__(self, body, **kwargs):
107 StringIO.StringIO.__init__(self, body)
108 self.headers = kwargs
109
110 def iteritems(self):
111 return self.headers.iteritems()
112
113
114class _MyHTTPConnection(object):
115 "This class is just a mock of httplib.HTTPConnection used for testing"
116
117 def __init__(self, host, port=None, key_file=None, cert_file=None,
joe.gregoriof28536d2007-10-23 14:10:11 +0000118 strict=None, timeout=None, proxy_info=None):
jcgregorio14644372007-07-30 14:13:37 +0000119 self.host = host
120 self.port = port
121 self.timeout = timeout
122 self.log = ""
123
124 def set_debuglevel(self, level):
125 pass
126
127 def connect(self):
128 "Connect to a host on a given port."
129 pass
130
131 def close(self):
132 pass
133
134 def request(self, method, request_uri, body, headers):
135 pass
136
137 def getresponse(self):
138 return _MyResponse("the body", status="200")
jcgregorioa46fe4e2006-11-16 04:13:45 +0000139
jcgregorio90fb4a42006-11-17 16:19:47 +0000140
jcgregorio2d66d4f2006-02-07 05:34:14 +0000141class HttpTest(unittest.TestCase):
142 def setUp(self):
jcgregorio7e3608f2006-06-15 13:01:53 +0000143 if os.path.exists(cacheDirName):
144 [os.remove(os.path.join(cacheDirName, file)) for file in os.listdir(cacheDirName)]
145 self.http = httplib2.Http(cacheDirName)
jcgregorio36140b52006-06-13 02:17:52 +0000146 self.http.clear_credentials()
jcgregorio2d66d4f2006-02-07 05:34:14 +0000147
Joe Gregoriof3ee17b2011-02-13 11:59:51 -0500148 def testIPv6NoSSL(self):
149 try:
150 self.http.request("http://[::1]/")
151 except socket.gaierror:
152 self.fail("should get the address family right for IPv6")
153 except socket.error:
154 # Even if IPv6 isn't installed on a machine it should just raise socket.error
155 pass
156
157 def testIPv6SSL(self):
158 try:
159 self.http.request("https://[::1]/")
160 except socket.gaierror:
161 self.fail("should get the address family right for IPv6")
162 except socket.error:
163 # Even if IPv6 isn't installed on a machine it should just raise socket.error
164 pass
165
jcgregorio14644372007-07-30 14:13:37 +0000166 def testConnectionType(self):
joe.gregoriof28536d2007-10-23 14:10:11 +0000167 self.http.force_exception_to_status_code = False
jcgregorio14644372007-07-30 14:13:37 +0000168 response, content = self.http.request("http://bitworking.org", connection_type=_MyHTTPConnection)
169 self.assertEqual(response['content-location'], "http://bitworking.org")
170 self.assertEqual(content, "the body")
171
jcgregorio6a638172007-01-23 16:40:23 +0000172 def testGetUnknownServer(self):
jcgregorio07a9a4a2007-03-08 21:18:39 +0000173 self.http.force_exception_to_status_code = False
jcgregorio6a638172007-01-23 16:40:23 +0000174 try:
175 self.http.request("http://fred.bitworking.org/")
176 self.fail("An httplib2.ServerNotFoundError Exception must be thrown on an unresolvable server.")
177 except httplib2.ServerNotFoundError:
178 pass
179
jcgregorio07a9a4a2007-03-08 21:18:39 +0000180 # Now test with exceptions turned off
181 self.http.force_exception_to_status_code = True
182
183 (response, content) = self.http.request("http://fred.bitworking.org/")
184 self.assertEqual(response['content-type'], 'text/plain')
185 self.assertTrue(content.startswith("Unable to find"))
186 self.assertEqual(response.status, 400)
187
Joe Gregoriob6c90c42011-02-11 01:03:22 -0500188 def testGetConnectionRefused(self):
189 self.http.force_exception_to_status_code = False
190 try:
191 self.http.request("http://localhost:7777/")
192 self.fail("An socket.error exception must be thrown on Connection Refused.")
193 except socket.error:
194 pass
195
196 # Now test with exceptions turned off
197 self.http.force_exception_to_status_code = True
198
199 (response, content) = self.http.request("http://localhost:7777/")
200 self.assertEqual(response['content-type'], 'text/plain')
201 self.assertTrue("Connection refused" in content)
202 self.assertEqual(response.status, 400)
203
jcgregorioa898f8f2006-12-12 17:16:55 +0000204 def testGetIRI(self):
jcgregoriodebceec2006-12-12 20:26:02 +0000205 if sys.version_info >= (2,3):
206 uri = urlparse.urljoin(base, u"reflector/reflector.cgi?d=\N{CYRILLIC CAPITAL LETTER DJE}")
207 (response, content) = self.http.request(uri, "GET")
208 d = self.reflector(content)
209 self.assertTrue(d.has_key('QUERY_STRING'))
210 self.assertTrue(d['QUERY_STRING'].find('%D0%82') > 0)
jcgregorioa898f8f2006-12-12 17:16:55 +0000211
jcgregorio2d66d4f2006-02-07 05:34:14 +0000212 def testGetIsDefaultMethod(self):
213 # Test that GET is the default method
214 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000215 (response, content) = self.http.request(uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000216 self.assertEqual(response['x-method'], "GET")
217
218 def testDifferentMethods(self):
219 # Test that all methods can be used
220 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
221 for method in ["GET", "PUT", "DELETE", "POST"]:
jcgregorio36140b52006-06-13 02:17:52 +0000222 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000223 self.assertEqual(response['x-method'], method)
224
Joe Gregoriob628c0b2009-07-16 12:28:04 -0400225 def testHeadRead(self):
226 # Test that we don't try to read the response of a HEAD request
227 # since httplib blocks response.read() for HEAD requests.
228 # Oddly enough this doesn't appear as a problem when doing HEAD requests
229 # against Apache servers.
230 uri = "http://www.google.com/"
231 (response, content) = self.http.request(uri, "HEAD")
232 self.assertEqual(response.status, 200)
233 self.assertEqual(content, "")
234
jcgregorio2d66d4f2006-02-07 05:34:14 +0000235 def testGetNoCache(self):
236 # Test that can do a GET w/o the cache turned on.
237 http = httplib2.Http()
238 uri = urlparse.urljoin(base, "304/test_etag.txt")
239 (response, content) = http.request(uri, "GET")
240 self.assertEqual(response.status, 200)
jcgregorioa0713ab2006-07-01 05:21:34 +0000241 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000242
Joe Gregorioe202d212009-07-16 14:57:52 -0400243 def testGetOnlyIfCachedCacheHit(self):
244 # Test that can do a GET with cache and 'only-if-cached'
245 uri = urlparse.urljoin(base, "304/test_etag.txt")
246 (response, content) = self.http.request(uri, "GET")
247 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
248 self.assertEqual(response.fromcache, True)
249 self.assertEqual(response.status, 200)
250
jcgregorioe4ce13e2006-04-02 03:05:08 +0000251 def testGetOnlyIfCachedCacheMiss(self):
252 # Test that can do a GET with no cache with 'only-if-cached'
jcgregorioe4ce13e2006-04-02 03:05:08 +0000253 uri = urlparse.urljoin(base, "304/test_etag.txt")
Joe Gregorioe202d212009-07-16 14:57:52 -0400254 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
jcgregorioe4ce13e2006-04-02 03:05:08 +0000255 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400256 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000257
258 def testGetOnlyIfCachedNoCacheAtAll(self):
259 # Test that can do a GET with no cache with 'only-if-cached'
260 # Of course, there might be an intermediary beyond us
261 # that responds to the 'only-if-cached', so this
262 # test can't really be guaranteed to pass.
263 http = httplib2.Http()
264 uri = urlparse.urljoin(base, "304/test_etag.txt")
265 (response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
266 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400267 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000268
jcgregorio2d66d4f2006-02-07 05:34:14 +0000269 def testUserAgent(self):
270 # Test that we provide a default user-agent
271 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000272 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000273 self.assertEqual(response.status, 200)
274 self.assertTrue(content.startswith("Python-httplib2/"))
275
276 def testUserAgentNonDefault(self):
277 # Test that the default user-agent can be over-ridden
joe.gregoriof28536d2007-10-23 14:10:11 +0000278
jcgregorio2d66d4f2006-02-07 05:34:14 +0000279 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000280 (response, content) = self.http.request(uri, "GET", headers={'User-Agent': 'fred/1.0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000281 self.assertEqual(response.status, 200)
282 self.assertTrue(content.startswith("fred/1.0"))
283
284 def testGet300WithLocation(self):
285 # Test the we automatically follow 300 redirects if a Location: header is provided
286 uri = urlparse.urljoin(base, "300/with-location-header.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000287 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000288 self.assertEqual(response.status, 200)
289 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000290 self.assertEqual(response.previous.status, 300)
291 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000292
293 # Confirm that the intermediate 300 is not cached
jcgregorio36140b52006-06-13 02:17:52 +0000294 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000295 self.assertEqual(response.status, 200)
296 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000297 self.assertEqual(response.previous.status, 300)
298 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000299
jcgregorio2f1e1422007-05-03 13:17:33 +0000300 def testGet300WithLocationNoRedirect(self):
301 # Test the we automatically follow 300 redirects if a Location: header is provided
302 self.http.follow_redirects = False
303 uri = urlparse.urljoin(base, "300/with-location-header.asis")
304 (response, content) = self.http.request(uri, "GET")
305 self.assertEqual(response.status, 300)
306
jcgregorio2d66d4f2006-02-07 05:34:14 +0000307 def testGet300WithoutLocation(self):
308 # Not giving a Location: header in a 300 response is acceptable
309 # In which case we just return the 300 response
310 uri = urlparse.urljoin(base, "300/without-location-header.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000311 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000312 self.assertEqual(response.status, 300)
313 self.assertTrue(response['content-type'].startswith("text/html"))
jcgregorioa0713ab2006-07-01 05:21:34 +0000314 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000315
316 def testGet301(self):
317 # Test that we automatically follow 301 redirects
318 # and that we cache the 301 response
319 uri = urlparse.urljoin(base, "301/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000320 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000321 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000322 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000323 self.assertTrue(response.has_key('content-location'))
324 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000325 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000326 self.assertEqual(response.previous.status, 301)
327 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000328
jcgregorio36140b52006-06-13 02:17:52 +0000329 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000330 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000331 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000332 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000333 self.assertEqual(response.previous.status, 301)
334 self.assertEqual(response.previous.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000335
jcgregorio2f1e1422007-05-03 13:17:33 +0000336
337 def testGet301NoRedirect(self):
338 # Test that we automatically follow 301 redirects
339 # and that we cache the 301 response
340 self.http.follow_redirects = False
341 uri = urlparse.urljoin(base, "301/onestep.asis")
342 destination = urlparse.urljoin(base, "302/final-destination.txt")
343 (response, content) = self.http.request(uri, "GET")
344 self.assertEqual(response.status, 301)
345
346
jcgregorio2d66d4f2006-02-07 05:34:14 +0000347 def testGet302(self):
348 # Test that we automatically follow 302 redirects
349 # and that we DO NOT cache the 302 response
350 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000351 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000352 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000353 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000354 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000355 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000356 self.assertEqual(response.previous.status, 302)
357 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000358
359 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000360 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000361 self.assertEqual(response.status, 200)
362 self.assertEqual(response.fromcache, True)
jcgregorio772adc82006-11-17 21:52:34 +0000363 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000364 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000365 self.assertEqual(response.previous.status, 302)
366 self.assertEqual(response.previous.fromcache, False)
jcgregorio772adc82006-11-17 21:52:34 +0000367 self.assertEqual(response.previous['content-location'], uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000368
369 uri = urlparse.urljoin(base, "302/twostep.asis")
370
jcgregorio36140b52006-06-13 02:17:52 +0000371 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000372 self.assertEqual(response.status, 200)
373 self.assertEqual(response.fromcache, True)
374 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000375 self.assertEqual(response.previous.status, 302)
376 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000377
378 def testGet302RedirectionLimit(self):
379 # Test that we can set a lower redirection limit
380 # and that we raise an exception when we exceed
381 # that limit.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000382 self.http.force_exception_to_status_code = False
383
jcgregorio2d66d4f2006-02-07 05:34:14 +0000384 uri = urlparse.urljoin(base, "302/twostep.asis")
385 try:
jcgregorio36140b52006-06-13 02:17:52 +0000386 (response, content) = self.http.request(uri, "GET", redirections = 1)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000387 self.fail("This should not happen")
388 except httplib2.RedirectLimit:
389 pass
390 except Exception, e:
391 self.fail("Threw wrong kind of exception ")
392
jcgregorio07a9a4a2007-03-08 21:18:39 +0000393 # Re-run the test with out the exceptions
394 self.http.force_exception_to_status_code = True
395
396 (response, content) = self.http.request(uri, "GET", redirections = 1)
397 self.assertEqual(response.status, 500)
398 self.assertTrue(response.reason.startswith("Redirected more"))
399 self.assertEqual("302", response['status'])
400 self.assertTrue(content.startswith("<html>"))
401 self.assertTrue(response.previous != None)
402
jcgregorio2d66d4f2006-02-07 05:34:14 +0000403 def testGet302NoLocation(self):
404 # Test that we throw an exception when we get
405 # a 302 with no Location: header.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000406 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000407 uri = urlparse.urljoin(base, "302/no-location.asis")
408 try:
jcgregorio36140b52006-06-13 02:17:52 +0000409 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000410 self.fail("Should never reach here")
411 except httplib2.RedirectMissingLocation:
412 pass
413 except Exception, e:
414 self.fail("Threw wrong kind of exception ")
415
jcgregorio07a9a4a2007-03-08 21:18:39 +0000416 # Re-run the test with out the exceptions
417 self.http.force_exception_to_status_code = True
418
419 (response, content) = self.http.request(uri, "GET")
420 self.assertEqual(response.status, 500)
421 self.assertTrue(response.reason.startswith("Redirected but"))
422 self.assertEqual("302", response['status'])
423 self.assertTrue(content.startswith("This is content"))
424
jcgregorio2d66d4f2006-02-07 05:34:14 +0000425 def testGet302ViaHttps(self):
jcgregorioadbb4f82006-05-19 15:17:42 +0000426 # Google always redirects to http://google.com
jcgregorio36140b52006-06-13 02:17:52 +0000427 (response, content) = self.http.request("https://google.com", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000428 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000429 self.assertEqual(302, response.previous.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000430
431 def testGetViaHttps(self):
432 # Test that we can handle HTTPS
jcgregorio36140b52006-06-13 02:17:52 +0000433 (response, content) = self.http.request("https://google.com/adsense/", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000434 self.assertEqual(200, response.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000435
436 def testGetViaHttpsSpecViolationOnLocation(self):
437 # Test that we follow redirects through HTTPS
438 # even if they violate the spec by including
439 # a relative Location: header instead of an
440 # absolute one.
jcgregorio36140b52006-06-13 02:17:52 +0000441 (response, content) = self.http.request("https://google.com/adsense", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000442 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000443 self.assertNotEqual(None, response.previous)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000444
jcgregoriode8238d2007-03-07 19:08:26 +0000445
446 def testGetViaHttpsKeyCert(self):
jcgregorio2f1e1422007-05-03 13:17:33 +0000447 # At this point I can only test
448 # that the key and cert files are passed in
449 # correctly to httplib. It would be nice to have
450 # a real https endpoint to test against.
451 http = httplib2.Http(timeout=2)
jcgregoriode8238d2007-03-07 19:08:26 +0000452
453 http.add_certificate("akeyfile", "acertfile", "bitworking.org")
454 try:
455 (response, content) = http.request("https://bitworking.org", "GET")
456 except:
457 pass
458 self.assertEqual(http.connections["https:bitworking.org"].key_file, "akeyfile")
459 self.assertEqual(http.connections["https:bitworking.org"].cert_file, "acertfile")
460
jcgregorio2f1e1422007-05-03 13:17:33 +0000461 try:
462 (response, content) = http.request("https://notthere.bitworking.org", "GET")
463 except:
464 pass
465 self.assertEqual(http.connections["https:notthere.bitworking.org"].key_file, None)
466 self.assertEqual(http.connections["https:notthere.bitworking.org"].cert_file, None)
467
468
469
jcgregoriode8238d2007-03-07 19:08:26 +0000470
jcgregorio2d66d4f2006-02-07 05:34:14 +0000471 def testGet303(self):
472 # Do a follow-up GET on a Location: header
473 # returned from a POST that gave a 303.
474 uri = urlparse.urljoin(base, "303/303.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000475 (response, content) = self.http.request(uri, "POST", " ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000476 self.assertEqual(response.status, 200)
477 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000478 self.assertEqual(response.previous.status, 303)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000479
jcgregorio2f1e1422007-05-03 13:17:33 +0000480 def testGet303NoRedirect(self):
481 # Do a follow-up GET on a Location: header
482 # returned from a POST that gave a 303.
483 self.http.follow_redirects = False
484 uri = urlparse.urljoin(base, "303/303.cgi")
485 (response, content) = self.http.request(uri, "POST", " ")
486 self.assertEqual(response.status, 303)
487
jcgregorio2d66d4f2006-02-07 05:34:14 +0000488 def test303ForDifferentMethods(self):
489 # Test that all methods can be used
490 uri = urlparse.urljoin(base, "303/redirect-to-reflector.cgi")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000491 for (method, method_on_303) in [("PUT", "GET"), ("DELETE", "GET"), ("POST", "GET"), ("GET", "GET"), ("HEAD", "GET")]:
jcgregorio36140b52006-06-13 02:17:52 +0000492 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000493 self.assertEqual(response['x-method'], method_on_303)
494
495 def testGet304(self):
496 # Test that we use ETags properly to validate our cache
497 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000498 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000499 self.assertNotEqual(response['etag'], "")
500
jcgregorio36140b52006-06-13 02:17:52 +0000501 (response, content) = self.http.request(uri, "GET")
502 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'must-revalidate'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000503 self.assertEqual(response.status, 200)
504 self.assertEqual(response.fromcache, True)
505
jcgregorio90fb4a42006-11-17 16:19:47 +0000506 cache_file_name = os.path.join(cacheDirName, httplib2.safename(httplib2.urlnorm(uri)[-1]))
507 f = open(cache_file_name, "r")
508 status_line = f.readline()
509 f.close()
510
511 self.assertTrue(status_line.startswith("status:"))
512
jcgregorio36140b52006-06-13 02:17:52 +0000513 (response, content) = self.http.request(uri, "HEAD")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000514 self.assertEqual(response.status, 200)
515 self.assertEqual(response.fromcache, True)
516
jcgregorio36140b52006-06-13 02:17:52 +0000517 (response, content) = self.http.request(uri, "GET", headers = {'range': 'bytes=0-0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000518 self.assertEqual(response.status, 206)
519 self.assertEqual(response.fromcache, False)
520
jcgregorio25185622006-10-28 05:12:34 +0000521 def testGetIgnoreEtag(self):
522 # Test that we can forcibly ignore ETags
523 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
524 (response, content) = self.http.request(uri, "GET")
525 self.assertNotEqual(response['etag'], "")
526
527 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
528 d = self.reflector(content)
529 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
530
531 self.http.ignore_etag = True
532 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
533 d = self.reflector(content)
534 self.assertEqual(response.fromcache, False)
535 self.assertFalse(d.has_key('HTTP_IF_NONE_MATCH'))
536
jcgregorio4b145e82007-01-18 19:46:34 +0000537 def testOverrideEtag(self):
538 # Test that we can forcibly ignore ETags
539 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
540 (response, content) = self.http.request(uri, "GET")
541 self.assertNotEqual(response['etag'], "")
542
543 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
544 d = self.reflector(content)
545 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
546 self.assertNotEqual(d['HTTP_IF_NONE_MATCH'], "fred")
547
pilgrim00a352e2009-05-29 04:04:44 +0000548 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0', 'if-none-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000549 d = self.reflector(content)
550 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
551 self.assertEqual(d['HTTP_IF_NONE_MATCH'], "fred")
jcgregorio25185622006-10-28 05:12:34 +0000552
pilgrim00a352e2009-05-29 04:04:44 +0000553#MAP-commented this out because it consistently fails
554# def testGet304EndToEnd(self):
555# # Test that end to end headers get overwritten in the cache
556# uri = urlparse.urljoin(base, "304/end2end.cgi")
557# (response, content) = self.http.request(uri, "GET")
558# self.assertNotEqual(response['etag'], "")
559# old_date = response['date']
560# time.sleep(2)
561#
562# (response, content) = self.http.request(uri, "GET", headers = {'Cache-Control': 'max-age=0'})
563# # The response should be from the cache, but the Date: header should be updated.
564# new_date = response['date']
565# self.assertNotEqual(new_date, old_date)
566# self.assertEqual(response.status, 200)
567# self.assertEqual(response.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000568
569 def testGet304LastModified(self):
570 # Test that we can still handle a 304
571 # by only using the last-modified cache validator.
572 uri = urlparse.urljoin(base, "304/last-modified-only/last-modified-only.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000573 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000574
575 self.assertNotEqual(response['last-modified'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000576 (response, content) = self.http.request(uri, "GET")
577 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000578 self.assertEqual(response.status, 200)
579 self.assertEqual(response.fromcache, True)
580
581 def testGet307(self):
582 # Test that we do follow 307 redirects but
583 # do not cache the 307
584 uri = urlparse.urljoin(base, "307/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000585 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000586 self.assertEqual(response.status, 200)
587 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000588 self.assertEqual(response.previous.status, 307)
589 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000590
jcgregorio36140b52006-06-13 02:17:52 +0000591 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000592 self.assertEqual(response.status, 200)
593 self.assertEqual(response.fromcache, True)
594 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000595 self.assertEqual(response.previous.status, 307)
596 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000597
598 def testGet410(self):
599 # Test that we pass 410's through
600 uri = urlparse.urljoin(base, "410/410.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000601 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000602 self.assertEqual(response.status, 410)
603
chris dent89f15142009-12-24 14:02:57 -0600604 def testVaryHeaderSimple(self):
605 """
606 RFC 2616 13.6
607 When the cache receives a subsequent request whose Request-URI
608 specifies one or more cache entries including a Vary header field,
609 the cache MUST NOT use such a cache entry to construct a response
610 to the new request unless all of the selecting request-headers
611 present in the new request match the corresponding stored
612 request-headers in the original request.
613 """
614 # test that the vary header is sent
615 uri = urlparse.urljoin(base, "vary/accept.asis")
616 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
617 self.assertEqual(response.status, 200)
618 self.assertTrue(response.has_key('vary'))
619
620 # get the resource again, from the cache since accept header in this
621 # request is the same as the request
622 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
623 self.assertEqual(response.status, 200)
624 self.assertEqual(response.fromcache, True, msg="Should be from cache")
625
626 # get the resource again, not from cache since Accept headers does not match
627 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
628 self.assertEqual(response.status, 200)
629 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
630
631 # get the resource again, without any Accept header, so again no match
632 (response, content) = self.http.request(uri, "GET")
633 self.assertEqual(response.status, 200)
634 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
635
636 def testNoVary(self):
637 # when there is no vary, a different Accept header (e.g.) should not
638 # impact if the cache is used
639 # test that the vary header is not sent
640 uri = urlparse.urljoin(base, "vary/no-vary.asis")
641 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
642 self.assertEqual(response.status, 200)
643 self.assertFalse(response.has_key('vary'))
644
645 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
646 self.assertEqual(response.status, 200)
647 self.assertEqual(response.fromcache, True, msg="Should be from cache")
648
649 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
650 self.assertEqual(response.status, 200)
651 self.assertEqual(response.fromcache, True, msg="Should be from cache")
652
653 def testVaryHeaderDouble(self):
654 uri = urlparse.urljoin(base, "vary/accept-double.asis")
655 (response, content) = self.http.request(uri, "GET", headers={
656 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
657 self.assertEqual(response.status, 200)
658 self.assertTrue(response.has_key('vary'))
659
660 # we are from cache
661 (response, content) = self.http.request(uri, "GET", headers={
662 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
663 self.assertEqual(response.fromcache, True, msg="Should be from cache")
664
665 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
666 self.assertEqual(response.status, 200)
667 self.assertEqual(response.fromcache, False)
668
669 # get the resource again, not from cache, varied headers don't match exact
670 (response, content) = self.http.request(uri, "GET", headers={'Accept-Language': 'da'})
671 self.assertEqual(response.status, 200)
672 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
673
jcgregorio88ef89b2010-05-13 23:42:11 -0400674 def testVaryUnusedHeader(self):
675 # A header's value is not considered to vary if it's not used at all.
676 uri = urlparse.urljoin(base, "vary/unused-header.asis")
677 (response, content) = self.http.request(uri, "GET", headers={
678 'Accept': 'text/plain'})
679 self.assertEqual(response.status, 200)
680 self.assertTrue(response.has_key('vary'))
681
682 # we are from cache
683 (response, content) = self.http.request(uri, "GET", headers={
684 'Accept': 'text/plain',})
685 self.assertEqual(response.fromcache, True, msg="Should be from cache")
686
chris dent89f15142009-12-24 14:02:57 -0600687
joe.gregorio0d4a2b82007-10-23 14:28:35 +0000688 def testHeadGZip(self):
689 # Test that we don't try to decompress a HEAD response
690 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
691 (response, content) = self.http.request(uri, "HEAD")
692 self.assertEqual(response.status, 200)
693 self.assertNotEqual(int(response['content-length']), 0)
694 self.assertEqual(content, "")
695
jcgregorio2d66d4f2006-02-07 05:34:14 +0000696 def testGetGZip(self):
697 # Test that we support gzip compression
698 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000699 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000700 self.assertEqual(response.status, 200)
jcgregorio90fb4a42006-11-17 16:19:47 +0000701 self.assertFalse(response.has_key('content-encoding'))
joe.gregorio8b6d2312007-12-16 05:42:07 +0000702 self.assertTrue(response.has_key('-content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000703 self.assertEqual(int(response['content-length']), len("This is the final destination.\n"))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000704 self.assertEqual(content, "This is the final destination.\n")
705
706 def testGetGZipFailure(self):
707 # Test that we raise a good exception when the gzip fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000708 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000709 uri = urlparse.urljoin(base, "gzip/failed-compression.asis")
710 try:
jcgregorio36140b52006-06-13 02:17:52 +0000711 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000712 self.fail("Should never reach here")
713 except httplib2.FailedToDecompressContent:
714 pass
715 except Exception:
716 self.fail("Threw wrong kind of exception")
717
jcgregorio07a9a4a2007-03-08 21:18:39 +0000718 # Re-run the test with out the exceptions
719 self.http.force_exception_to_status_code = True
720
721 (response, content) = self.http.request(uri, "GET")
722 self.assertEqual(response.status, 500)
723 self.assertTrue(response.reason.startswith("Content purported"))
724
725 def testTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000726 self.http.force_exception_to_status_code = True
jcgregorio07a9a4a2007-03-08 21:18:39 +0000727 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
728 try:
729 import socket
730 socket.setdefaulttimeout(1)
731 except:
732 # Don't run the test if we can't set the timeout
733 return
734 (response, content) = self.http.request(uri)
735 self.assertEqual(response.status, 408)
736 self.assertTrue(response.reason.startswith("Request Timeout"))
737 self.assertTrue(content.startswith("Request Timeout"))
738
jcgregoriob2697912007-03-09 02:23:47 +0000739 def testIndividualTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000740 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
741 http = httplib2.Http(timeout=1)
joe.gregoriof28536d2007-10-23 14:10:11 +0000742 http.force_exception_to_status_code = True
jcgregoriob2697912007-03-09 02:23:47 +0000743
744 (response, content) = http.request(uri)
745 self.assertEqual(response.status, 408)
746 self.assertTrue(response.reason.startswith("Request Timeout"))
747 self.assertTrue(content.startswith("Request Timeout"))
748
jcgregorio07a9a4a2007-03-08 21:18:39 +0000749
Joe Gregorio1a7609f2009-07-16 10:59:44 -0400750 def testHTTPSInitTimeout(self):
751 c = httplib2.HTTPSConnectionWithTimeout('localhost', 80, timeout=47)
752 self.assertEqual(47, c.timeout)
753
jcgregorio2d66d4f2006-02-07 05:34:14 +0000754 def testGetDeflate(self):
755 # Test that we support deflate compression
756 uri = urlparse.urljoin(base, "deflate/deflated.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000757 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000758 self.assertEqual(response.status, 200)
jcgregorio90fb4a42006-11-17 16:19:47 +0000759 self.assertFalse(response.has_key('content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000760 self.assertEqual(int(response['content-length']), len("This is the final destination."))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000761 self.assertEqual(content, "This is the final destination.")
762
763 def testGetDeflateFailure(self):
764 # Test that we raise a good exception when the deflate fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000765 self.http.force_exception_to_status_code = False
766
jcgregorio2d66d4f2006-02-07 05:34:14 +0000767 uri = urlparse.urljoin(base, "deflate/failed-compression.asis")
768 try:
jcgregorio36140b52006-06-13 02:17:52 +0000769 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000770 self.fail("Should never reach here")
771 except httplib2.FailedToDecompressContent:
772 pass
773 except Exception:
774 self.fail("Threw wrong kind of exception")
775
jcgregorio07a9a4a2007-03-08 21:18:39 +0000776 # Re-run the test with out the exceptions
777 self.http.force_exception_to_status_code = True
778
779 (response, content) = self.http.request(uri, "GET")
780 self.assertEqual(response.status, 500)
781 self.assertTrue(response.reason.startswith("Content purported"))
782
jcgregorio2d66d4f2006-02-07 05:34:14 +0000783 def testGetDuplicateHeaders(self):
784 # Test that duplicate headers get concatenated via ','
785 uri = urlparse.urljoin(base, "duplicate-headers/multilink.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000786 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000787 self.assertEqual(response.status, 200)
788 self.assertEqual(content, "This is content\n")
789 self.assertEqual(response['link'].split(",")[0], '<http://bitworking.org>; rel="home"; title="BitWorking"')
790
791 def testGetCacheControlNoCache(self):
792 # Test Cache-Control: no-cache on requests
793 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000794 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000795 self.assertNotEqual(response['etag'], "")
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, True)
799
jcgregorio36140b52006-06-13 02:17:52 +0000800 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000801 self.assertEqual(response.status, 200)
802 self.assertEqual(response.fromcache, False)
803
804 def testGetCacheControlPragmaNoCache(self):
805 # Test Pragma: no-cache on requests
806 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000807 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000808 self.assertNotEqual(response['etag'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000809 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000810 self.assertEqual(response.status, 200)
811 self.assertEqual(response.fromcache, True)
812
jcgregorio36140b52006-06-13 02:17:52 +0000813 (response, content) = self.http.request(uri, "GET", headers={'Pragma': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000814 self.assertEqual(response.status, 200)
815 self.assertEqual(response.fromcache, False)
816
817 def testGetCacheControlNoStoreRequest(self):
818 # A no-store request means that the response should not be stored.
819 uri = urlparse.urljoin(base, "304/test_etag.txt")
820
jcgregorio36140b52006-06-13 02:17:52 +0000821 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000822 self.assertEqual(response.status, 200)
823 self.assertEqual(response.fromcache, False)
824
jcgregorio36140b52006-06-13 02:17:52 +0000825 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000826 self.assertEqual(response.status, 200)
827 self.assertEqual(response.fromcache, False)
828
829 def testGetCacheControlNoStoreResponse(self):
830 # A no-store response means that the response should not be stored.
831 uri = urlparse.urljoin(base, "no-store/no-store.asis")
832
jcgregorio36140b52006-06-13 02:17:52 +0000833 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000834 self.assertEqual(response.status, 200)
835 self.assertEqual(response.fromcache, False)
836
jcgregorio36140b52006-06-13 02:17:52 +0000837 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000838 self.assertEqual(response.status, 200)
839 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000840
841 def testGetCacheControlNoCacheNoStoreRequest(self):
842 # Test that a no-store, no-cache clears the entry from the cache
843 # even if it was cached previously.
844 uri = urlparse.urljoin(base, "304/test_etag.txt")
845
jcgregorio36140b52006-06-13 02:17:52 +0000846 (response, content) = self.http.request(uri, "GET")
847 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000848 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000849 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
850 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000851 self.assertEqual(response.status, 200)
852 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000853
854 def testUpdateInvalidatesCache(self):
855 # Test that calling PUT or DELETE on a
856 # URI that is cache invalidates that cache.
857 uri = urlparse.urljoin(base, "304/test_etag.txt")
858
jcgregorio36140b52006-06-13 02:17:52 +0000859 (response, content) = self.http.request(uri, "GET")
860 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000861 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000862 (response, content) = self.http.request(uri, "DELETE")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000863 self.assertEqual(response.status, 405)
864
jcgregorio36140b52006-06-13 02:17:52 +0000865 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000866 self.assertEqual(response.fromcache, False)
867
868 def testUpdateUsesCachedETag(self):
869 # Test that we natively support http://www.w3.org/1999/04/Editing/
870 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
871
jcgregorio36140b52006-06-13 02:17:52 +0000872 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000873 self.assertEqual(response.status, 200)
874 self.assertEqual(response.fromcache, False)
jcgregorio36140b52006-06-13 02:17:52 +0000875 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000876 self.assertEqual(response.status, 200)
877 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400878 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000879 self.assertEqual(response.status, 200)
Joe Gregoriocd868102009-09-29 17:09:16 -0400880 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000881 self.assertEqual(response.status, 412)
882
joe.gregorio700f04d2008-09-06 04:46:32 +0000883 def testUpdateUsesCachedETagAndOCMethod(self):
884 # Test that we natively support http://www.w3.org/1999/04/Editing/
885 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
886
887 (response, content) = self.http.request(uri, "GET")
888 self.assertEqual(response.status, 200)
889 self.assertEqual(response.fromcache, False)
890 (response, content) = self.http.request(uri, "GET")
891 self.assertEqual(response.status, 200)
892 self.assertEqual(response.fromcache, True)
893 self.http.optimistic_concurrency_methods.append("DELETE")
894 (response, content) = self.http.request(uri, "DELETE")
895 self.assertEqual(response.status, 200)
896
897
jcgregorio4b145e82007-01-18 19:46:34 +0000898 def testUpdateUsesCachedETagOverridden(self):
899 # Test that we natively support http://www.w3.org/1999/04/Editing/
900 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
901
902 (response, content) = self.http.request(uri, "GET")
903 self.assertEqual(response.status, 200)
904 self.assertEqual(response.fromcache, False)
905 (response, content) = self.http.request(uri, "GET")
906 self.assertEqual(response.status, 200)
907 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400908 (response, content) = self.http.request(uri, "PUT", body="foo", headers={'if-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000909 self.assertEqual(response.status, 412)
910
jcgregorio2d66d4f2006-02-07 05:34:14 +0000911 def testBasicAuth(self):
912 # Test Basic Authentication
913 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000914 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000915 self.assertEqual(response.status, 401)
916
917 uri = urlparse.urljoin(base, "basic/")
jcgregorio36140b52006-06-13 02:17:52 +0000918 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000919 self.assertEqual(response.status, 401)
920
jcgregorio36140b52006-06-13 02:17:52 +0000921 self.http.add_credentials('joe', 'password')
922 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000923 self.assertEqual(response.status, 200)
924
925 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000926 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000927 self.assertEqual(response.status, 200)
928
jcgregoriode8238d2007-03-07 19:08:26 +0000929 def testBasicAuthWithDomain(self):
930 # Test Basic Authentication
931 uri = urlparse.urljoin(base, "basic/file.txt")
932 (response, content) = self.http.request(uri, "GET")
933 self.assertEqual(response.status, 401)
934
935 uri = urlparse.urljoin(base, "basic/")
936 (response, content) = self.http.request(uri, "GET")
937 self.assertEqual(response.status, 401)
938
939 self.http.add_credentials('joe', 'password', "example.org")
940 (response, content) = self.http.request(uri, "GET")
941 self.assertEqual(response.status, 401)
942
943 uri = urlparse.urljoin(base, "basic/file.txt")
944 (response, content) = self.http.request(uri, "GET")
945 self.assertEqual(response.status, 401)
946
947 domain = urlparse.urlparse(base)[1]
948 self.http.add_credentials('joe', 'password', domain)
949 (response, content) = self.http.request(uri, "GET")
950 self.assertEqual(response.status, 200)
951
952 uri = urlparse.urljoin(base, "basic/file.txt")
953 (response, content) = self.http.request(uri, "GET")
954 self.assertEqual(response.status, 200)
955
956
957
958
959
960
jcgregorio2d66d4f2006-02-07 05:34:14 +0000961 def testBasicAuthTwoDifferentCredentials(self):
jcgregorioadbb4f82006-05-19 15:17:42 +0000962 # Test Basic Authentication with multiple sets of credentials
jcgregorio2d66d4f2006-02-07 05:34:14 +0000963 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000964 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000965 self.assertEqual(response.status, 401)
966
967 uri = urlparse.urljoin(base, "basic2/")
jcgregorio36140b52006-06-13 02:17:52 +0000968 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000969 self.assertEqual(response.status, 401)
970
jcgregorio36140b52006-06-13 02:17:52 +0000971 self.http.add_credentials('fred', 'barney')
972 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000973 self.assertEqual(response.status, 200)
974
975 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000976 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000977 self.assertEqual(response.status, 200)
978
979 def testBasicAuthNested(self):
980 # Test Basic Authentication with resources
981 # that are nested
982 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +0000983 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000984 self.assertEqual(response.status, 401)
985
986 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +0000987 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000988 self.assertEqual(response.status, 401)
989
jcgregorioadbb4f82006-05-19 15:17:42 +0000990 # Now add in credentials one at a time and test.
jcgregorio36140b52006-06-13 02:17:52 +0000991 self.http.add_credentials('joe', 'password')
jcgregorio2d66d4f2006-02-07 05:34:14 +0000992
993 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +0000994 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000995 self.assertEqual(response.status, 200)
996
997 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +0000998 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000999 self.assertEqual(response.status, 401)
1000
jcgregorio36140b52006-06-13 02:17:52 +00001001 self.http.add_credentials('fred', 'barney')
jcgregorio2d66d4f2006-02-07 05:34:14 +00001002
1003 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +00001004 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001005 self.assertEqual(response.status, 200)
1006
1007 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +00001008 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001009 self.assertEqual(response.status, 200)
1010
1011 def testDigestAuth(self):
1012 # Test that we support Digest Authentication
1013 uri = urlparse.urljoin(base, "digest/")
jcgregorio36140b52006-06-13 02:17:52 +00001014 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001015 self.assertEqual(response.status, 401)
1016
jcgregorio36140b52006-06-13 02:17:52 +00001017 self.http.add_credentials('joe', 'password')
1018 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001019 self.assertEqual(response.status, 200)
1020
1021 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001022 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001023
1024 def testDigestAuthNextNonceAndNC(self):
1025 # Test that if the server sets nextnonce that we reset
1026 # the nonce count back to 1
1027 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001028 self.http.add_credentials('joe', 'password')
1029 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001030 info = httplib2._parse_www_authenticate(response, 'authentication-info')
1031 self.assertEqual(response.status, 200)
jcgregorio36140b52006-06-13 02:17:52 +00001032 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001033 info2 = httplib2._parse_www_authenticate(response, 'authentication-info')
1034 self.assertEqual(response.status, 200)
1035
1036 if info.has_key('nextnonce'):
1037 self.assertEqual(info2['nc'], 1)
1038
1039 def testDigestAuthStale(self):
1040 # Test that we can handle a nonce becoming stale
1041 uri = urlparse.urljoin(base, "digest-expire/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001042 self.http.add_credentials('joe', 'password')
1043 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001044 info = httplib2._parse_www_authenticate(response, 'authentication-info')
1045 self.assertEqual(response.status, 200)
1046
1047 time.sleep(3)
1048 # Sleep long enough that the nonce becomes stale
1049
jcgregorio36140b52006-06-13 02:17:52 +00001050 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001051 self.assertFalse(response.fromcache)
1052 self.assertTrue(response._stale_digest)
1053 info3 = httplib2._parse_www_authenticate(response, 'authentication-info')
1054 self.assertEqual(response.status, 200)
1055
1056 def reflector(self, content):
jcgregorio25185622006-10-28 05:12:34 +00001057 return dict( [tuple(x.split("=", 1)) for x in content.strip().split("\n")] )
jcgregorio2d66d4f2006-02-07 05:34:14 +00001058
1059 def testReflector(self):
1060 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +00001061 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001062 d = self.reflector(content)
1063 self.assertTrue(d.has_key('HTTP_USER_AGENT'))
1064
Joe Gregorio84cc10a2009-09-01 13:02:49 -04001065 def testConnectionClose(self):
1066 uri = "http://www.google.com/"
1067 (response, content) = self.http.request(uri, "GET")
1068 for c in self.http.connections.values():
1069 self.assertNotEqual(None, c.sock)
1070 (response, content) = self.http.request(uri, "GET", headers={"connection": "close"})
1071 for c in self.http.connections.values():
1072 self.assertEqual(None, c.sock)
1073
1074
jcgregorio36140b52006-06-13 02:17:52 +00001075try:
1076 import memcache
1077 class HttpTestMemCached(HttpTest):
1078 def setUp(self):
1079 self.cache = memcache.Client(['127.0.0.1:11211'], debug=0)
jcgregorio47d24672006-06-29 05:18:59 +00001080 #self.cache = memcache.Client(['10.0.0.4:11211'], debug=1)
jcgregorio36140b52006-06-13 02:17:52 +00001081 self.http = httplib2.Http(self.cache)
1082 self.cache.flush_all()
jcgregorio47d24672006-06-29 05:18:59 +00001083 # Not exactly sure why the sleep is needed here, but
1084 # if not present then some unit tests that rely on caching
1085 # fail. Memcached seems to lose some sets immediately
1086 # after a flush_all if the set is to a value that
1087 # was previously cached. (Maybe the flush is handled async?)
1088 time.sleep(1)
jcgregorio36140b52006-06-13 02:17:52 +00001089 self.http.clear_credentials()
1090except:
1091 pass
1092
1093
1094
chris dent89f15142009-12-24 14:02:57 -06001095
jcgregoriodb8dfc82006-03-31 14:59:46 +00001096# ------------------------------------------------------------------------
jcgregorio2d66d4f2006-02-07 05:34:14 +00001097
1098class HttpPrivateTest(unittest.TestCase):
1099
1100 def testParseCacheControl(self):
1101 # Test that we can parse the Cache-Control header
1102 self.assertEqual({}, httplib2._parse_cache_control({}))
1103 self.assertEqual({'no-cache': 1}, httplib2._parse_cache_control({'cache-control': ' no-cache'}))
1104 cc = httplib2._parse_cache_control({'cache-control': ' no-cache, max-age = 7200'})
1105 self.assertEqual(cc['no-cache'], 1)
1106 self.assertEqual(cc['max-age'], '7200')
1107 cc = httplib2._parse_cache_control({'cache-control': ' , '})
1108 self.assertEqual(cc[''], 1)
1109
Joe Gregorioe314e8b2009-07-16 20:11:28 -04001110 try:
1111 cc = httplib2._parse_cache_control({'cache-control': 'Max-age=3600;post-check=1800,pre-check=3600'})
1112 self.assertTrue("max-age" in cc)
1113 except:
1114 self.fail("Should not throw exception")
1115
jcgregorio2d66d4f2006-02-07 05:34:14 +00001116 def testNormalizeHeaders(self):
1117 # Test that we normalize headers to lowercase
1118 h = httplib2._normalize_headers({'Cache-Control': 'no-cache', 'Other': 'Stuff'})
1119 self.assertTrue(h.has_key('cache-control'))
1120 self.assertTrue(h.has_key('other'))
1121 self.assertEqual('Stuff', h['other'])
1122
1123 def testExpirationModelTransparent(self):
1124 # Test that no-cache makes our request TRANSPARENT
1125 response_headers = {
1126 'cache-control': 'max-age=7200'
1127 }
1128 request_headers = {
1129 'cache-control': 'no-cache'
1130 }
1131 self.assertEqual("TRANSPARENT", httplib2._entry_disposition(response_headers, request_headers))
1132
jcgregorio45865012007-01-18 16:38:22 +00001133 def testMaxAgeNonNumeric(self):
1134 # Test that no-cache makes our request TRANSPARENT
1135 response_headers = {
1136 'cache-control': 'max-age=fred, min-fresh=barney'
1137 }
1138 request_headers = {
1139 }
1140 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1141
1142
jcgregorio2d66d4f2006-02-07 05:34:14 +00001143 def testExpirationModelNoCacheResponse(self):
1144 # The date and expires point to an entry that should be
1145 # FRESH, but the no-cache over-rides that.
1146 now = time.time()
1147 response_headers = {
1148 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1149 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1150 'cache-control': 'no-cache'
1151 }
1152 request_headers = {
1153 }
1154 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1155
1156 def testExpirationModelStaleRequestMustReval(self):
1157 # must-revalidate forces STALE
1158 self.assertEqual("STALE", httplib2._entry_disposition({}, {'cache-control': 'must-revalidate'}))
1159
1160 def testExpirationModelStaleResponseMustReval(self):
1161 # must-revalidate forces STALE
1162 self.assertEqual("STALE", httplib2._entry_disposition({'cache-control': 'must-revalidate'}, {}))
1163
1164 def testExpirationModelFresh(self):
1165 response_headers = {
1166 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1167 'cache-control': 'max-age=2'
1168 }
1169 request_headers = {
1170 }
1171 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1172 time.sleep(3)
1173 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1174
1175 def testExpirationMaxAge0(self):
1176 response_headers = {
1177 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1178 'cache-control': 'max-age=0'
1179 }
1180 request_headers = {
1181 }
1182 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1183
1184 def testExpirationModelDateAndExpires(self):
1185 now = time.time()
1186 response_headers = {
1187 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1188 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1189 }
1190 request_headers = {
1191 }
1192 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1193 time.sleep(3)
1194 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1195
jcgregoriof9511052007-06-01 14:56:34 +00001196 def testExpiresZero(self):
1197 now = time.time()
1198 response_headers = {
1199 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1200 'expires': "0",
1201 }
1202 request_headers = {
1203 }
1204 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1205
jcgregorio2d66d4f2006-02-07 05:34:14 +00001206 def testExpirationModelDateOnly(self):
1207 now = time.time()
1208 response_headers = {
1209 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+3)),
1210 }
1211 request_headers = {
1212 }
1213 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1214
1215 def testExpirationModelOnlyIfCached(self):
1216 response_headers = {
1217 }
1218 request_headers = {
1219 'cache-control': 'only-if-cached',
1220 }
1221 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1222
1223 def testExpirationModelMaxAgeBoth(self):
1224 now = time.time()
1225 response_headers = {
1226 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1227 'cache-control': 'max-age=2'
1228 }
1229 request_headers = {
1230 'cache-control': 'max-age=0'
1231 }
1232 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1233
1234 def testExpirationModelDateAndExpiresMinFresh1(self):
1235 now = time.time()
1236 response_headers = {
1237 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1238 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1239 }
1240 request_headers = {
1241 'cache-control': 'min-fresh=2'
1242 }
1243 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1244
1245 def testExpirationModelDateAndExpiresMinFresh2(self):
1246 now = time.time()
1247 response_headers = {
1248 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1249 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1250 }
1251 request_headers = {
1252 'cache-control': 'min-fresh=2'
1253 }
1254 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1255
1256 def testParseWWWAuthenticateEmpty(self):
1257 res = httplib2._parse_www_authenticate({})
1258 self.assertEqual(len(res.keys()), 0)
1259
jcgregoriofd22e432006-04-27 02:00:08 +00001260 def testParseWWWAuthenticate(self):
1261 # different uses of spaces around commas
1262 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="test realm" , foo=foo ,bar="bar", baz=baz,qux=qux'})
1263 self.assertEqual(len(res.keys()), 1)
1264 self.assertEqual(len(res['test'].keys()), 5)
1265
1266 # tokens with non-alphanum
1267 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'T*!%#st realm=to*!%#en, to*!%#en="quoted string"'})
1268 self.assertEqual(len(res.keys()), 1)
1269 self.assertEqual(len(res['t*!%#st'].keys()), 2)
1270
1271 # quoted string with quoted pairs
1272 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="a \\"test\\" realm"'})
1273 self.assertEqual(len(res.keys()), 1)
1274 self.assertEqual(res['test']['realm'], 'a "test" realm')
1275
1276 def testParseWWWAuthenticateStrict(self):
1277 httplib2.USE_WWW_AUTH_STRICT_PARSING = 1;
1278 self.testParseWWWAuthenticate();
1279 httplib2.USE_WWW_AUTH_STRICT_PARSING = 0;
1280
jcgregorio2d66d4f2006-02-07 05:34:14 +00001281 def testParseWWWAuthenticateBasic(self):
1282 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me"'})
1283 basic = res['basic']
1284 self.assertEqual('me', basic['realm'])
1285
1286 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm="MD5"'})
1287 basic = res['basic']
1288 self.assertEqual('me', basic['realm'])
1289 self.assertEqual('MD5', basic['algorithm'])
1290
1291 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm=MD5'})
1292 basic = res['basic']
1293 self.assertEqual('me', basic['realm'])
1294 self.assertEqual('MD5', basic['algorithm'])
1295
1296 def testParseWWWAuthenticateBasic2(self):
1297 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me",other="fred" '})
1298 basic = res['basic']
1299 self.assertEqual('me', basic['realm'])
1300 self.assertEqual('fred', basic['other'])
1301
1302 def testParseWWWAuthenticateBasic3(self):
1303 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic REAlm="me" '})
1304 basic = res['basic']
1305 self.assertEqual('me', basic['realm'])
1306
1307
1308 def testParseWWWAuthenticateDigest(self):
1309 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1310 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"'})
1311 digest = res['digest']
1312 self.assertEqual('testrealm@host.com', digest['realm'])
1313 self.assertEqual('auth,auth-int', digest['qop'])
1314
1315
1316 def testParseWWWAuthenticateMultiple(self):
1317 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1318 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41" Basic REAlm="me" '})
1319 digest = res['digest']
1320 self.assertEqual('testrealm@host.com', digest['realm'])
1321 self.assertEqual('auth,auth-int', digest['qop'])
1322 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1323 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1324 basic = res['basic']
1325 self.assertEqual('me', basic['realm'])
1326
1327 def testParseWWWAuthenticateMultiple2(self):
1328 # Handle an added comma between challenges, which might get thrown in if the challenges were
1329 # originally sent in separate www-authenticate headers.
1330 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1331 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me" '})
1332 digest = res['digest']
1333 self.assertEqual('testrealm@host.com', digest['realm'])
1334 self.assertEqual('auth,auth-int', digest['qop'])
1335 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1336 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1337 basic = res['basic']
1338 self.assertEqual('me', basic['realm'])
1339
1340 def testParseWWWAuthenticateMultiple3(self):
1341 # Handle an added comma between challenges, which might get thrown in if the challenges were
1342 # originally sent in separate www-authenticate headers.
1343 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1344 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1345 digest = res['digest']
1346 self.assertEqual('testrealm@host.com', digest['realm'])
1347 self.assertEqual('auth,auth-int', digest['qop'])
1348 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1349 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1350 basic = res['basic']
1351 self.assertEqual('me', basic['realm'])
1352 wsse = res['wsse']
1353 self.assertEqual('foo', wsse['realm'])
1354 self.assertEqual('UsernameToken', wsse['profile'])
1355
1356 def testParseWWWAuthenticateMultiple4(self):
1357 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1358 'Digest realm="test-real.m@host.com", qop \t=\t"\tauth,auth-int", nonce="(*)&^&$%#",opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1359 digest = res['digest']
1360 self.assertEqual('test-real.m@host.com', digest['realm'])
1361 self.assertEqual('\tauth,auth-int', digest['qop'])
1362 self.assertEqual('(*)&^&$%#', digest['nonce'])
1363
1364 def testParseWWWAuthenticateMoreQuoteCombos(self):
1365 res = httplib2._parse_www_authenticate({'www-authenticate':'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'})
1366 digest = res['digest']
1367 self.assertEqual('myrealm', digest['realm'])
1368
1369 def testDigestObject(self):
1370 credentials = ('joe', 'password')
1371 host = None
1372 request_uri = '/projects/httplib2/test/digest/'
1373 headers = {}
1374 response = {
1375 'www-authenticate': 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth"'
1376 }
1377 content = ""
1378
jcgregorio6cbab7e2006-04-21 20:35:43 +00001379 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001380 d.request("GET", request_uri, headers, content, cnonce="33033375ec278a46")
1381 our_request = "Authorization: %s" % headers['Authorization']
1382 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"'
1383 self.assertEqual(our_request, working_request)
1384
1385
1386 def testDigestObjectStale(self):
1387 credentials = ('joe', 'password')
1388 host = None
1389 request_uri = '/projects/httplib2/test/digest/'
1390 headers = {}
1391 response = httplib2.Response({ })
1392 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1393 response.status = 401
1394 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001395 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001396 # Returns true to force a retry
1397 self.assertTrue( d.response(response, content) )
1398
1399 def testDigestObjectAuthInfo(self):
1400 credentials = ('joe', 'password')
1401 host = None
1402 request_uri = '/projects/httplib2/test/digest/'
1403 headers = {}
1404 response = httplib2.Response({ })
1405 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1406 response['authentication-info'] = 'nextnonce="fred"'
1407 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001408 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001409 # Returns true to force a retry
1410 self.assertFalse( d.response(response, content) )
1411 self.assertEqual('fred', d.challenge['nonce'])
1412 self.assertEqual(1, d.challenge['nc'])
1413
1414 def testWsseAlgorithm(self):
1415 digest = httplib2._wsse_username_token("d36e316282959a9ed4c89851497a717f", "2003-12-15T14:43:07Z", "taadtaadpstcsm")
1416 expected = "quR/EWLAV4xLf9Zqyw4pDmfV9OY="
1417 self.assertEqual(expected, digest)
1418
jcgregoriodb8dfc82006-03-31 14:59:46 +00001419 def testEnd2End(self):
1420 # one end to end header
1421 response = {'content-type': 'application/atom+xml', 'te': 'deflate'}
1422 end2end = httplib2._get_end2end_headers(response)
1423 self.assertTrue('content-type' in end2end)
1424 self.assertTrue('te' not in end2end)
1425 self.assertTrue('connection' not in end2end)
1426
1427 # one end to end header that gets eliminated
1428 response = {'connection': 'content-type', 'content-type': 'application/atom+xml', 'te': 'deflate'}
1429 end2end = httplib2._get_end2end_headers(response)
1430 self.assertTrue('content-type' not in end2end)
1431 self.assertTrue('te' not in end2end)
1432 self.assertTrue('connection' not in end2end)
1433
1434 # Degenerate case of no headers
1435 response = {}
1436 end2end = httplib2._get_end2end_headers(response)
1437 self.assertEquals(0, len(end2end))
1438
1439 # Degenerate case of connection referrring to a header not passed in
1440 response = {'connection': 'content-type'}
1441 end2end = httplib2._get_end2end_headers(response)
1442 self.assertEquals(0, len(end2end))
jcgregorio2d66d4f2006-02-07 05:34:14 +00001443
chris dent89f15142009-12-24 14:02:57 -06001444if __name__ == '__main__':
1445 unittest.main()