blob: 9a9e792bc5cf826cbb7381004053b0a89ec81369 [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
Joe Gregoriob53de9b2011-06-07 15:44:51 -040029try:
30 import ssl
31except ImportError:
32 pass
Joe Gregorio694a8122011-02-13 21:40:09 -050033
jcgregorio8421f272006-02-14 18:19:51 +000034# Python 2.3 support
35if not hasattr(unittest.TestCase, 'assertTrue'):
36 unittest.TestCase.assertTrue = unittest.TestCase.failUnless
37 unittest.TestCase.assertFalse = unittest.TestCase.failIf
38
jcgregorio2d66d4f2006-02-07 05:34:14 +000039# The test resources base uri
40base = 'http://bitworking.org/projects/httplib2/test/'
41#base = 'http://localhost/projects/httplib2/test/'
jcgregorio90fb4a42006-11-17 16:19:47 +000042cacheDirName = ".cache"
jcgregorio2d66d4f2006-02-07 05:34:14 +000043
jcgregoriode8238d2007-03-07 19:08:26 +000044
45class CredentialsTest(unittest.TestCase):
46 def test(self):
47 c = httplib2.Credentials()
48 c.add("joe", "password")
49 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
50 self.assertEqual(("joe", "password"), list(c.iter(""))[0])
51 c.add("fred", "password2", "wellformedweb.org")
52 self.assertEqual(("joe", "password"), list(c.iter("bitworking.org"))[0])
53 self.assertEqual(1, len(list(c.iter("bitworking.org"))))
54 self.assertEqual(2, len(list(c.iter("wellformedweb.org"))))
55 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
56 c.clear()
57 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
58 c.add("fred", "password2", "wellformedweb.org")
59 self.assertTrue(("fred", "password2") in list(c.iter("wellformedweb.org")))
60 self.assertEqual(0, len(list(c.iter("bitworking.org"))))
61 self.assertEqual(0, len(list(c.iter(""))))
62
63
jcgregorio2d66d4f2006-02-07 05:34:14 +000064class ParserTest(unittest.TestCase):
65 def testFromStd66(self):
66 self.assertEqual( ('http', 'example.com', '', None, None ), httplib2.parse_uri("http://example.com"))
67 self.assertEqual( ('https', 'example.com', '', None, None ), httplib2.parse_uri("https://example.com"))
68 self.assertEqual( ('https', 'example.com:8080', '', None, None ), httplib2.parse_uri("https://example.com:8080"))
69 self.assertEqual( ('http', 'example.com', '/', None, None ), httplib2.parse_uri("http://example.com/"))
70 self.assertEqual( ('http', 'example.com', '/path', None, None ), httplib2.parse_uri("http://example.com/path"))
71 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', None ), httplib2.parse_uri("http://example.com/path?a=1&b=2"))
72 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', 'fred' ), httplib2.parse_uri("http://example.com/path?a=1&b=2#fred"))
73 self.assertEqual( ('http', 'example.com', '/path', 'a=1&b=2', 'fred' ), httplib2.parse_uri("http://example.com/path?a=1&b=2#fred"))
74
jcgregorio2d66d4f2006-02-07 05:34:14 +000075
jcgregorioa46fe4e2006-11-16 04:13:45 +000076class UrlNormTest(unittest.TestCase):
77 def test(self):
78 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://example.org")[-1])
79 self.assertEqual( "http://example.org/", httplib2.urlnorm("http://EXAMple.org")[-1])
80 self.assertEqual( "http://example.org/?=b", httplib2.urlnorm("http://EXAMple.org?=b")[-1])
81 self.assertEqual( "http://example.org/mypath?a=b", httplib2.urlnorm("http://EXAMple.org/mypath?a=b")[-1])
82 self.assertEqual( "http://localhost:80/", httplib2.urlnorm("http://localhost:80")[-1])
jcgregoriob4e9ab02006-11-17 15:53:15 +000083 self.assertEqual( httplib2.urlnorm("http://localhost:80/"), httplib2.urlnorm("HTTP://LOCALHOST:80"))
jcgregorio132d28e2007-01-23 16:22:53 +000084 try:
85 httplib2.urlnorm("/")
86 self.fail("Non-absolute URIs should raise an exception")
87 except httplib2.RelativeURIError:
88 pass
jcgregorioa46fe4e2006-11-16 04:13:45 +000089
90class UrlSafenameTest(unittest.TestCase):
91 def test(self):
92 # Test that different URIs end up generating different safe names
93 self.assertEqual( "example.org,fred,a=b,58489f63a7a83c3b7794a6a398ee8b1f", httplib2.safename("http://example.org/fred/?a=b"))
94 self.assertEqual( "example.org,fred,a=b,8c5946d56fec453071f43329ff0be46b", httplib2.safename("http://example.org/fred?/a=b"))
95 self.assertEqual( "www.example.org,fred,a=b,499c44b8d844a011b67ea2c015116968", httplib2.safename("http://www.example.org/fred?/a=b"))
96 self.assertEqual( httplib2.safename(httplib2.urlnorm("http://www")[-1]), httplib2.safename(httplib2.urlnorm("http://WWW")[-1]))
97 self.assertEqual( "www.example.org,fred,a=b,692e843a333484ce0095b070497ab45d", httplib2.safename("https://www.example.org/fred?/a=b"))
98 self.assertNotEqual( httplib2.safename("http://www"), httplib2.safename("https://www"))
99 # Test the max length limits
100 uri = "http://" + ("w" * 200) + ".org"
101 uri2 = "http://" + ("w" * 201) + ".org"
102 self.assertNotEqual( httplib2.safename(uri2), httplib2.safename(uri))
103 # Max length should be 200 + 1 (",") + 32
104 self.assertEqual(233, len(httplib2.safename(uri2)))
105 self.assertEqual(233, len(httplib2.safename(uri)))
106 # Unicode
jcgregoriodebceec2006-12-12 20:26:02 +0000107 if sys.version_info >= (2,3):
108 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 +0000109
jcgregorio14644372007-07-30 14:13:37 +0000110class _MyResponse(StringIO.StringIO):
111 def __init__(self, body, **kwargs):
112 StringIO.StringIO.__init__(self, body)
113 self.headers = kwargs
114
115 def iteritems(self):
116 return self.headers.iteritems()
117
118
119class _MyHTTPConnection(object):
120 "This class is just a mock of httplib.HTTPConnection used for testing"
121
122 def __init__(self, host, port=None, key_file=None, cert_file=None,
joe.gregoriof28536d2007-10-23 14:10:11 +0000123 strict=None, timeout=None, proxy_info=None):
jcgregorio14644372007-07-30 14:13:37 +0000124 self.host = host
125 self.port = port
126 self.timeout = timeout
127 self.log = ""
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400128 self.sock = None
jcgregorio14644372007-07-30 14:13:37 +0000129
130 def set_debuglevel(self, level):
131 pass
132
133 def connect(self):
134 "Connect to a host on a given port."
135 pass
136
137 def close(self):
138 pass
139
140 def request(self, method, request_uri, body, headers):
141 pass
142
143 def getresponse(self):
144 return _MyResponse("the body", status="200")
jcgregorioa46fe4e2006-11-16 04:13:45 +0000145
jcgregorio90fb4a42006-11-17 16:19:47 +0000146
jcgregorio2d66d4f2006-02-07 05:34:14 +0000147class HttpTest(unittest.TestCase):
148 def setUp(self):
jcgregorio7e3608f2006-06-15 13:01:53 +0000149 if os.path.exists(cacheDirName):
150 [os.remove(os.path.join(cacheDirName, file)) for file in os.listdir(cacheDirName)]
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400151
152 if sys.version_info < (2, 6):
153 disable_cert_validation = True
154 else:
155 disable_cert_validation = False
156 self.http = httplib2.Http(
157 cacheDirName,
158 disable_ssl_certificate_validation=disable_cert_validation)
jcgregorio36140b52006-06-13 02:17:52 +0000159 self.http.clear_credentials()
jcgregorio2d66d4f2006-02-07 05:34:14 +0000160
Joe Gregoriof3ee17b2011-02-13 11:59:51 -0500161 def testIPv6NoSSL(self):
162 try:
163 self.http.request("http://[::1]/")
164 except socket.gaierror:
165 self.fail("should get the address family right for IPv6")
166 except socket.error:
167 # Even if IPv6 isn't installed on a machine it should just raise socket.error
168 pass
169
170 def testIPv6SSL(self):
171 try:
172 self.http.request("https://[::1]/")
173 except socket.gaierror:
174 self.fail("should get the address family right for IPv6")
175 except socket.error:
176 # Even if IPv6 isn't installed on a machine it should just raise socket.error
177 pass
178
jcgregorio14644372007-07-30 14:13:37 +0000179 def testConnectionType(self):
joe.gregoriof28536d2007-10-23 14:10:11 +0000180 self.http.force_exception_to_status_code = False
jcgregorio14644372007-07-30 14:13:37 +0000181 response, content = self.http.request("http://bitworking.org", connection_type=_MyHTTPConnection)
182 self.assertEqual(response['content-location'], "http://bitworking.org")
183 self.assertEqual(content, "the body")
184
jcgregorio6a638172007-01-23 16:40:23 +0000185 def testGetUnknownServer(self):
jcgregorio07a9a4a2007-03-08 21:18:39 +0000186 self.http.force_exception_to_status_code = False
jcgregorio6a638172007-01-23 16:40:23 +0000187 try:
188 self.http.request("http://fred.bitworking.org/")
189 self.fail("An httplib2.ServerNotFoundError Exception must be thrown on an unresolvable server.")
190 except httplib2.ServerNotFoundError:
191 pass
192
jcgregorio07a9a4a2007-03-08 21:18:39 +0000193 # Now test with exceptions turned off
194 self.http.force_exception_to_status_code = True
195
196 (response, content) = self.http.request("http://fred.bitworking.org/")
197 self.assertEqual(response['content-type'], 'text/plain')
198 self.assertTrue(content.startswith("Unable to find"))
199 self.assertEqual(response.status, 400)
200
Joe Gregoriob6c90c42011-02-11 01:03:22 -0500201 def testGetConnectionRefused(self):
202 self.http.force_exception_to_status_code = False
203 try:
204 self.http.request("http://localhost:7777/")
205 self.fail("An socket.error exception must be thrown on Connection Refused.")
206 except socket.error:
207 pass
208
209 # Now test with exceptions turned off
210 self.http.force_exception_to_status_code = True
211
212 (response, content) = self.http.request("http://localhost:7777/")
213 self.assertEqual(response['content-type'], 'text/plain')
214 self.assertTrue("Connection refused" in content)
215 self.assertEqual(response.status, 400)
216
jcgregorioa898f8f2006-12-12 17:16:55 +0000217 def testGetIRI(self):
jcgregoriodebceec2006-12-12 20:26:02 +0000218 if sys.version_info >= (2,3):
219 uri = urlparse.urljoin(base, u"reflector/reflector.cgi?d=\N{CYRILLIC CAPITAL LETTER DJE}")
220 (response, content) = self.http.request(uri, "GET")
221 d = self.reflector(content)
222 self.assertTrue(d.has_key('QUERY_STRING'))
223 self.assertTrue(d['QUERY_STRING'].find('%D0%82') > 0)
jcgregorioa898f8f2006-12-12 17:16:55 +0000224
jcgregorio2d66d4f2006-02-07 05:34:14 +0000225 def testGetIsDefaultMethod(self):
226 # Test that GET is the default method
227 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000228 (response, content) = self.http.request(uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000229 self.assertEqual(response['x-method'], "GET")
230
231 def testDifferentMethods(self):
232 # Test that all methods can be used
233 uri = urlparse.urljoin(base, "methods/method_reflector.cgi")
234 for method in ["GET", "PUT", "DELETE", "POST"]:
jcgregorio36140b52006-06-13 02:17:52 +0000235 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000236 self.assertEqual(response['x-method'], method)
237
Joe Gregoriob628c0b2009-07-16 12:28:04 -0400238 def testHeadRead(self):
239 # Test that we don't try to read the response of a HEAD request
240 # since httplib blocks response.read() for HEAD requests.
241 # Oddly enough this doesn't appear as a problem when doing HEAD requests
242 # against Apache servers.
243 uri = "http://www.google.com/"
244 (response, content) = self.http.request(uri, "HEAD")
245 self.assertEqual(response.status, 200)
246 self.assertEqual(content, "")
247
jcgregorio2d66d4f2006-02-07 05:34:14 +0000248 def testGetNoCache(self):
249 # Test that can do a GET w/o the cache turned on.
250 http = httplib2.Http()
251 uri = urlparse.urljoin(base, "304/test_etag.txt")
252 (response, content) = http.request(uri, "GET")
253 self.assertEqual(response.status, 200)
jcgregorioa0713ab2006-07-01 05:21:34 +0000254 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000255
Joe Gregorioe202d212009-07-16 14:57:52 -0400256 def testGetOnlyIfCachedCacheHit(self):
257 # Test that can do a GET with cache and 'only-if-cached'
258 uri = urlparse.urljoin(base, "304/test_etag.txt")
259 (response, content) = self.http.request(uri, "GET")
260 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
261 self.assertEqual(response.fromcache, True)
262 self.assertEqual(response.status, 200)
263
jcgregorioe4ce13e2006-04-02 03:05:08 +0000264 def testGetOnlyIfCachedCacheMiss(self):
265 # Test that can do a GET with no cache with 'only-if-cached'
jcgregorioe4ce13e2006-04-02 03:05:08 +0000266 uri = urlparse.urljoin(base, "304/test_etag.txt")
Joe Gregorioe202d212009-07-16 14:57:52 -0400267 (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
jcgregorioe4ce13e2006-04-02 03:05:08 +0000268 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400269 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000270
271 def testGetOnlyIfCachedNoCacheAtAll(self):
272 # Test that can do a GET with no cache with 'only-if-cached'
273 # Of course, there might be an intermediary beyond us
274 # that responds to the 'only-if-cached', so this
275 # test can't really be guaranteed to pass.
276 http = httplib2.Http()
277 uri = urlparse.urljoin(base, "304/test_etag.txt")
278 (response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
279 self.assertEqual(response.fromcache, False)
Joe Gregorioe202d212009-07-16 14:57:52 -0400280 self.assertEqual(response.status, 504)
jcgregorioe4ce13e2006-04-02 03:05:08 +0000281
jcgregorio2d66d4f2006-02-07 05:34:14 +0000282 def testUserAgent(self):
283 # Test that we provide a default user-agent
284 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000285 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000286 self.assertEqual(response.status, 200)
287 self.assertTrue(content.startswith("Python-httplib2/"))
288
289 def testUserAgentNonDefault(self):
290 # Test that the default user-agent can be over-ridden
joe.gregoriof28536d2007-10-23 14:10:11 +0000291
jcgregorio2d66d4f2006-02-07 05:34:14 +0000292 uri = urlparse.urljoin(base, "user-agent/test.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000293 (response, content) = self.http.request(uri, "GET", headers={'User-Agent': 'fred/1.0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000294 self.assertEqual(response.status, 200)
295 self.assertTrue(content.startswith("fred/1.0"))
296
297 def testGet300WithLocation(self):
298 # Test the we automatically follow 300 redirects if a Location: header is provided
299 uri = urlparse.urljoin(base, "300/with-location-header.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000300 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000301 self.assertEqual(response.status, 200)
302 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000303 self.assertEqual(response.previous.status, 300)
304 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000305
306 # Confirm that the intermediate 300 is not cached
jcgregorio36140b52006-06-13 02:17:52 +0000307 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000308 self.assertEqual(response.status, 200)
309 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000310 self.assertEqual(response.previous.status, 300)
311 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000312
jcgregorio2f1e1422007-05-03 13:17:33 +0000313 def testGet300WithLocationNoRedirect(self):
314 # Test the we automatically follow 300 redirects if a Location: header is provided
315 self.http.follow_redirects = False
316 uri = urlparse.urljoin(base, "300/with-location-header.asis")
317 (response, content) = self.http.request(uri, "GET")
318 self.assertEqual(response.status, 300)
319
jcgregorio2d66d4f2006-02-07 05:34:14 +0000320 def testGet300WithoutLocation(self):
321 # Not giving a Location: header in a 300 response is acceptable
322 # In which case we just return the 300 response
323 uri = urlparse.urljoin(base, "300/without-location-header.asis")
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, 300)
326 self.assertTrue(response['content-type'].startswith("text/html"))
jcgregorioa0713ab2006-07-01 05:21:34 +0000327 self.assertEqual(response.previous, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000328
329 def testGet301(self):
330 # Test that we automatically follow 301 redirects
331 # and that we cache the 301 response
332 uri = urlparse.urljoin(base, "301/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000333 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000334 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000335 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000336 self.assertTrue(response.has_key('content-location'))
337 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000338 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000339 self.assertEqual(response.previous.status, 301)
340 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000341
jcgregorio36140b52006-06-13 02:17:52 +0000342 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000343 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000344 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000345 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000346 self.assertEqual(response.previous.status, 301)
347 self.assertEqual(response.previous.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000348
Joe Gregorio694a8122011-02-13 21:40:09 -0500349 def testHead301(self):
350 # Test that we automatically follow 301 redirects
351 uri = urlparse.urljoin(base, "301/onestep.asis")
352 destination = urlparse.urljoin(base, "302/final-destination.txt")
353 (response, content) = self.http.request(uri, "HEAD")
354 self.assertEqual(response.status, 200)
355 self.assertEqual(response.previous.status, 301)
356 self.assertEqual(response.previous.fromcache, False)
jcgregorio2f1e1422007-05-03 13:17:33 +0000357
358 def testGet301NoRedirect(self):
359 # Test that we automatically follow 301 redirects
360 # and that we cache the 301 response
361 self.http.follow_redirects = False
362 uri = urlparse.urljoin(base, "301/onestep.asis")
363 destination = urlparse.urljoin(base, "302/final-destination.txt")
364 (response, content) = self.http.request(uri, "GET")
365 self.assertEqual(response.status, 301)
366
367
jcgregorio2d66d4f2006-02-07 05:34:14 +0000368 def testGet302(self):
369 # Test that we automatically follow 302 redirects
370 # and that we DO NOT cache the 302 response
371 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio8e300b92006-11-07 16:44:35 +0000372 destination = urlparse.urljoin(base, "302/final-destination.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000373 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000374 self.assertEqual(response.status, 200)
jcgregorio772adc82006-11-17 21:52:34 +0000375 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000376 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000377 self.assertEqual(response.previous.status, 302)
378 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000379
380 uri = urlparse.urljoin(base, "302/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000381 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000382 self.assertEqual(response.status, 200)
383 self.assertEqual(response.fromcache, True)
jcgregorio772adc82006-11-17 21:52:34 +0000384 self.assertEqual(response['content-location'], destination)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000385 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000386 self.assertEqual(response.previous.status, 302)
387 self.assertEqual(response.previous.fromcache, False)
jcgregorio772adc82006-11-17 21:52:34 +0000388 self.assertEqual(response.previous['content-location'], uri)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000389
390 uri = urlparse.urljoin(base, "302/twostep.asis")
391
jcgregorio36140b52006-06-13 02:17:52 +0000392 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000393 self.assertEqual(response.status, 200)
394 self.assertEqual(response.fromcache, True)
395 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000396 self.assertEqual(response.previous.status, 302)
397 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000398
399 def testGet302RedirectionLimit(self):
400 # Test that we can set a lower redirection limit
401 # and that we raise an exception when we exceed
402 # that limit.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000403 self.http.force_exception_to_status_code = False
404
jcgregorio2d66d4f2006-02-07 05:34:14 +0000405 uri = urlparse.urljoin(base, "302/twostep.asis")
406 try:
jcgregorio36140b52006-06-13 02:17:52 +0000407 (response, content) = self.http.request(uri, "GET", redirections = 1)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000408 self.fail("This should not happen")
409 except httplib2.RedirectLimit:
410 pass
411 except Exception, e:
412 self.fail("Threw wrong kind of exception ")
413
jcgregorio07a9a4a2007-03-08 21:18:39 +0000414 # Re-run the test with out the exceptions
415 self.http.force_exception_to_status_code = True
416
417 (response, content) = self.http.request(uri, "GET", redirections = 1)
418 self.assertEqual(response.status, 500)
419 self.assertTrue(response.reason.startswith("Redirected more"))
420 self.assertEqual("302", response['status'])
421 self.assertTrue(content.startswith("<html>"))
422 self.assertTrue(response.previous != None)
423
jcgregorio2d66d4f2006-02-07 05:34:14 +0000424 def testGet302NoLocation(self):
425 # Test that we throw an exception when we get
426 # a 302 with no Location: header.
jcgregorio07a9a4a2007-03-08 21:18:39 +0000427 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000428 uri = urlparse.urljoin(base, "302/no-location.asis")
429 try:
jcgregorio36140b52006-06-13 02:17:52 +0000430 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000431 self.fail("Should never reach here")
432 except httplib2.RedirectMissingLocation:
433 pass
434 except Exception, e:
435 self.fail("Threw wrong kind of exception ")
436
jcgregorio07a9a4a2007-03-08 21:18:39 +0000437 # Re-run the test with out the exceptions
438 self.http.force_exception_to_status_code = True
439
440 (response, content) = self.http.request(uri, "GET")
441 self.assertEqual(response.status, 500)
442 self.assertTrue(response.reason.startswith("Redirected but"))
443 self.assertEqual("302", response['status'])
444 self.assertTrue(content.startswith("This is content"))
Joe Gregorio84e33252011-05-03 09:09:13 -0400445
jcgregorio2d66d4f2006-02-07 05:34:14 +0000446 def testGet302ViaHttps(self):
jcgregorioadbb4f82006-05-19 15:17:42 +0000447 # Google always redirects to http://google.com
Joe Gregorio84e33252011-05-03 09:09:13 -0400448 (response, content) = self.http.request("https://www.google.com", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000449 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000450 self.assertEqual(302, response.previous.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000451
452 def testGetViaHttps(self):
453 # Test that we can handle HTTPS
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400454 (response, content) = self.http.request("https://www.google.com/adsense/", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000455 self.assertEqual(200, response.status)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000456
457 def testGetViaHttpsSpecViolationOnLocation(self):
458 # Test that we follow redirects through HTTPS
459 # even if they violate the spec by including
460 # a relative Location: header instead of an
461 # absolute one.
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400462 (response, content) = self.http.request("https://www.google.com/adsense", "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000463 self.assertEqual(200, response.status)
jcgregorioa0713ab2006-07-01 05:21:34 +0000464 self.assertNotEqual(None, response.previous)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000465
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400466 def testSslCertValidation(self):
467 if sys.version_info >= (2, 6):
468 # Test that we get an ssl.SSLError when specifying a non-existent CA
469 # certs file.
470 http = httplib2.Http(ca_certs='/nosuchfile')
471 self.assertRaises(ssl.SSLError,
472 http.request, "https://www.google.com/", "GET")
473
474 # Test that we get a SSLHandshakeError if we try to access
475 # https;//www.google.com, using a CA cert file that doesn't contain
476 # the CA Gogole uses (i.e., simulating a cert that's not signed by a
477 # trusted CA).
478 other_ca_certs = os.path.join(
479 os.path.dirname(os.path.abspath(httplib2.__file__ )),
480 "test", "other_cacerts.txt")
481 http = httplib2.Http(ca_certs=other_ca_certs)
482 self.assertRaises(httplib2.SSLHandshakeError,
483 http.request, "https://www.google.com/", "GET")
484
485 def testSslHostnameValidation(self):
486 if sys.version_info >= (2, 6):
487 # The SSL server at google.com:443 returns a certificate for
488 # 'www.google.com', which results in a host name mismatch.
489 # Note that this test only works because the ssl module and httplib2
490 # do not support SNI; for requests specifying a server name of
491 # 'google.com' via SNI, a matching cert would be returned.
492 self.assertRaises(httplib2.CertificateHostnameMismatch,
493 self.http.request, "https://google.com/", "GET")
494
495 def testSslCertValidationWithoutSslModuleFails(self):
496 if sys.version_info < (2, 6):
497 http = httplib2.Http(disable_ssl_certificate_validation=False)
498 self.assertRaises(httplib2.CertificateValidationUnsupported,
499 http.request, "https://www.google.com/", "GET")
jcgregoriode8238d2007-03-07 19:08:26 +0000500
501 def testGetViaHttpsKeyCert(self):
jcgregorio2f1e1422007-05-03 13:17:33 +0000502 # At this point I can only test
503 # that the key and cert files are passed in
504 # correctly to httplib. It would be nice to have
505 # a real https endpoint to test against.
Joe Gregoriob53de9b2011-06-07 15:44:51 -0400506
507 # bitworking.org presents an certificate for a non-matching host
508 # (*.webfaction.com), so we need to disable cert checking for this test.
509 http = httplib2.Http(timeout=2, disable_ssl_certificate_validation=True)
jcgregoriode8238d2007-03-07 19:08:26 +0000510
511 http.add_certificate("akeyfile", "acertfile", "bitworking.org")
512 try:
513 (response, content) = http.request("https://bitworking.org", "GET")
514 except:
515 pass
516 self.assertEqual(http.connections["https:bitworking.org"].key_file, "akeyfile")
517 self.assertEqual(http.connections["https:bitworking.org"].cert_file, "acertfile")
518
jcgregorio2f1e1422007-05-03 13:17:33 +0000519 try:
520 (response, content) = http.request("https://notthere.bitworking.org", "GET")
521 except:
522 pass
523 self.assertEqual(http.connections["https:notthere.bitworking.org"].key_file, None)
524 self.assertEqual(http.connections["https:notthere.bitworking.org"].cert_file, None)
525
526
527
jcgregoriode8238d2007-03-07 19:08:26 +0000528
jcgregorio2d66d4f2006-02-07 05:34:14 +0000529 def testGet303(self):
530 # Do a follow-up GET on a Location: header
531 # returned from a POST that gave a 303.
532 uri = urlparse.urljoin(base, "303/303.cgi")
jcgregorio36140b52006-06-13 02:17:52 +0000533 (response, content) = self.http.request(uri, "POST", " ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000534 self.assertEqual(response.status, 200)
535 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000536 self.assertEqual(response.previous.status, 303)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000537
jcgregorio2f1e1422007-05-03 13:17:33 +0000538 def testGet303NoRedirect(self):
539 # Do a follow-up GET on a Location: header
540 # returned from a POST that gave a 303.
541 self.http.follow_redirects = False
542 uri = urlparse.urljoin(base, "303/303.cgi")
543 (response, content) = self.http.request(uri, "POST", " ")
544 self.assertEqual(response.status, 303)
545
jcgregorio2d66d4f2006-02-07 05:34:14 +0000546 def test303ForDifferentMethods(self):
547 # Test that all methods can be used
548 uri = urlparse.urljoin(base, "303/redirect-to-reflector.cgi")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000549 for (method, method_on_303) in [("PUT", "GET"), ("DELETE", "GET"), ("POST", "GET"), ("GET", "GET"), ("HEAD", "GET")]:
jcgregorio36140b52006-06-13 02:17:52 +0000550 (response, content) = self.http.request(uri, method, body=" ")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000551 self.assertEqual(response['x-method'], method_on_303)
552
553 def testGet304(self):
554 # Test that we use ETags properly to validate our cache
555 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000556 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000557 self.assertNotEqual(response['etag'], "")
558
jcgregorio36140b52006-06-13 02:17:52 +0000559 (response, content) = self.http.request(uri, "GET")
560 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'must-revalidate'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000561 self.assertEqual(response.status, 200)
562 self.assertEqual(response.fromcache, True)
563
jcgregorio90fb4a42006-11-17 16:19:47 +0000564 cache_file_name = os.path.join(cacheDirName, httplib2.safename(httplib2.urlnorm(uri)[-1]))
565 f = open(cache_file_name, "r")
566 status_line = f.readline()
567 f.close()
568
569 self.assertTrue(status_line.startswith("status:"))
570
jcgregorio36140b52006-06-13 02:17:52 +0000571 (response, content) = self.http.request(uri, "HEAD")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000572 self.assertEqual(response.status, 200)
573 self.assertEqual(response.fromcache, True)
574
jcgregorio36140b52006-06-13 02:17:52 +0000575 (response, content) = self.http.request(uri, "GET", headers = {'range': 'bytes=0-0'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000576 self.assertEqual(response.status, 206)
577 self.assertEqual(response.fromcache, False)
578
jcgregorio25185622006-10-28 05:12:34 +0000579 def testGetIgnoreEtag(self):
580 # Test that we can forcibly ignore ETags
581 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
582 (response, content) = self.http.request(uri, "GET")
583 self.assertNotEqual(response['etag'], "")
584
585 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
586 d = self.reflector(content)
587 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
588
589 self.http.ignore_etag = True
590 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
591 d = self.reflector(content)
592 self.assertEqual(response.fromcache, False)
593 self.assertFalse(d.has_key('HTTP_IF_NONE_MATCH'))
594
jcgregorio4b145e82007-01-18 19:46:34 +0000595 def testOverrideEtag(self):
596 # Test that we can forcibly ignore ETags
597 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
598 (response, content) = self.http.request(uri, "GET")
599 self.assertNotEqual(response['etag'], "")
600
601 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0'})
602 d = self.reflector(content)
603 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
604 self.assertNotEqual(d['HTTP_IF_NONE_MATCH'], "fred")
605
pilgrim00a352e2009-05-29 04:04:44 +0000606 (response, content) = self.http.request(uri, "GET", headers = {'cache-control': 'max-age=0', 'if-none-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000607 d = self.reflector(content)
608 self.assertTrue(d.has_key('HTTP_IF_NONE_MATCH'))
609 self.assertEqual(d['HTTP_IF_NONE_MATCH'], "fred")
jcgregorio25185622006-10-28 05:12:34 +0000610
pilgrim00a352e2009-05-29 04:04:44 +0000611#MAP-commented this out because it consistently fails
612# def testGet304EndToEnd(self):
613# # Test that end to end headers get overwritten in the cache
614# uri = urlparse.urljoin(base, "304/end2end.cgi")
615# (response, content) = self.http.request(uri, "GET")
616# self.assertNotEqual(response['etag'], "")
617# old_date = response['date']
618# time.sleep(2)
619#
620# (response, content) = self.http.request(uri, "GET", headers = {'Cache-Control': 'max-age=0'})
621# # The response should be from the cache, but the Date: header should be updated.
622# new_date = response['date']
623# self.assertNotEqual(new_date, old_date)
624# self.assertEqual(response.status, 200)
625# self.assertEqual(response.fromcache, True)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000626
627 def testGet304LastModified(self):
628 # Test that we can still handle a 304
629 # by only using the last-modified cache validator.
630 uri = urlparse.urljoin(base, "304/last-modified-only/last-modified-only.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000631 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000632
633 self.assertNotEqual(response['last-modified'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000634 (response, content) = self.http.request(uri, "GET")
635 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000636 self.assertEqual(response.status, 200)
637 self.assertEqual(response.fromcache, True)
638
639 def testGet307(self):
640 # Test that we do follow 307 redirects but
641 # do not cache the 307
642 uri = urlparse.urljoin(base, "307/onestep.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000643 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000644 self.assertEqual(response.status, 200)
645 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000646 self.assertEqual(response.previous.status, 307)
647 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000648
jcgregorio36140b52006-06-13 02:17:52 +0000649 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000650 self.assertEqual(response.status, 200)
651 self.assertEqual(response.fromcache, True)
652 self.assertEqual(content, "This is the final destination.\n")
jcgregorioa0713ab2006-07-01 05:21:34 +0000653 self.assertEqual(response.previous.status, 307)
654 self.assertEqual(response.previous.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000655
656 def testGet410(self):
657 # Test that we pass 410's through
658 uri = urlparse.urljoin(base, "410/410.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000659 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000660 self.assertEqual(response.status, 410)
661
chris dent89f15142009-12-24 14:02:57 -0600662 def testVaryHeaderSimple(self):
663 """
664 RFC 2616 13.6
665 When the cache receives a subsequent request whose Request-URI
666 specifies one or more cache entries including a Vary header field,
667 the cache MUST NOT use such a cache entry to construct a response
668 to the new request unless all of the selecting request-headers
669 present in the new request match the corresponding stored
670 request-headers in the original request.
671 """
672 # test that the vary header is sent
673 uri = urlparse.urljoin(base, "vary/accept.asis")
674 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
675 self.assertEqual(response.status, 200)
676 self.assertTrue(response.has_key('vary'))
677
678 # get the resource again, from the cache since accept header in this
679 # request is the same as the request
680 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
681 self.assertEqual(response.status, 200)
682 self.assertEqual(response.fromcache, True, msg="Should be from cache")
683
684 # get the resource again, not from cache since Accept headers does not match
685 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
686 self.assertEqual(response.status, 200)
687 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
688
689 # get the resource again, without any Accept header, so again no match
690 (response, content) = self.http.request(uri, "GET")
691 self.assertEqual(response.status, 200)
692 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
693
694 def testNoVary(self):
695 # when there is no vary, a different Accept header (e.g.) should not
696 # impact if the cache is used
697 # test that the vary header is not sent
698 uri = urlparse.urljoin(base, "vary/no-vary.asis")
699 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
700 self.assertEqual(response.status, 200)
701 self.assertFalse(response.has_key('vary'))
702
703 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
704 self.assertEqual(response.status, 200)
705 self.assertEqual(response.fromcache, True, msg="Should be from cache")
706
707 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/html'})
708 self.assertEqual(response.status, 200)
709 self.assertEqual(response.fromcache, True, msg="Should be from cache")
710
711 def testVaryHeaderDouble(self):
712 uri = urlparse.urljoin(base, "vary/accept-double.asis")
713 (response, content) = self.http.request(uri, "GET", headers={
714 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
715 self.assertEqual(response.status, 200)
716 self.assertTrue(response.has_key('vary'))
717
718 # we are from cache
719 (response, content) = self.http.request(uri, "GET", headers={
720 'Accept': 'text/plain', 'Accept-Language': 'da, en-gb;q=0.8, en;q=0.7'})
721 self.assertEqual(response.fromcache, True, msg="Should be from cache")
722
723 (response, content) = self.http.request(uri, "GET", headers={'Accept': 'text/plain'})
724 self.assertEqual(response.status, 200)
725 self.assertEqual(response.fromcache, False)
726
727 # get the resource again, not from cache, varied headers don't match exact
728 (response, content) = self.http.request(uri, "GET", headers={'Accept-Language': 'da'})
729 self.assertEqual(response.status, 200)
730 self.assertEqual(response.fromcache, False, msg="Should not be from cache")
731
jcgregorio88ef89b2010-05-13 23:42:11 -0400732 def testVaryUnusedHeader(self):
733 # A header's value is not considered to vary if it's not used at all.
734 uri = urlparse.urljoin(base, "vary/unused-header.asis")
735 (response, content) = self.http.request(uri, "GET", headers={
736 'Accept': 'text/plain'})
737 self.assertEqual(response.status, 200)
738 self.assertTrue(response.has_key('vary'))
739
740 # we are from cache
741 (response, content) = self.http.request(uri, "GET", headers={
742 'Accept': 'text/plain',})
743 self.assertEqual(response.fromcache, True, msg="Should be from cache")
744
chris dent89f15142009-12-24 14:02:57 -0600745
joe.gregorio0d4a2b82007-10-23 14:28:35 +0000746 def testHeadGZip(self):
747 # Test that we don't try to decompress a HEAD response
748 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
749 (response, content) = self.http.request(uri, "HEAD")
750 self.assertEqual(response.status, 200)
751 self.assertNotEqual(int(response['content-length']), 0)
752 self.assertEqual(content, "")
753
jcgregorio2d66d4f2006-02-07 05:34:14 +0000754 def testGetGZip(self):
755 # Test that we support gzip compression
756 uri = urlparse.urljoin(base, "gzip/final-destination.txt")
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'))
joe.gregorio8b6d2312007-12-16 05:42:07 +0000760 self.assertTrue(response.has_key('-content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000761 self.assertEqual(int(response['content-length']), len("This is the final destination.\n"))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000762 self.assertEqual(content, "This is the final destination.\n")
763
Joe Gregoriod1137c52011-02-13 19:27:35 -0500764 def testPostAndGZipResponse(self):
765 uri = urlparse.urljoin(base, "gzip/post.cgi")
766 (response, content) = self.http.request(uri, "POST", body=" ")
767 self.assertEqual(response.status, 200)
768 self.assertFalse(response.has_key('content-encoding'))
769 self.assertTrue(response.has_key('-content-encoding'))
770
jcgregorio2d66d4f2006-02-07 05:34:14 +0000771 def testGetGZipFailure(self):
772 # Test that we raise a good exception when the gzip fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000773 self.http.force_exception_to_status_code = False
jcgregorio2d66d4f2006-02-07 05:34:14 +0000774 uri = urlparse.urljoin(base, "gzip/failed-compression.asis")
775 try:
jcgregorio36140b52006-06-13 02:17:52 +0000776 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000777 self.fail("Should never reach here")
778 except httplib2.FailedToDecompressContent:
779 pass
780 except Exception:
781 self.fail("Threw wrong kind of exception")
782
jcgregorio07a9a4a2007-03-08 21:18:39 +0000783 # Re-run the test with out the exceptions
784 self.http.force_exception_to_status_code = True
785
786 (response, content) = self.http.request(uri, "GET")
787 self.assertEqual(response.status, 500)
788 self.assertTrue(response.reason.startswith("Content purported"))
789
790 def testTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000791 self.http.force_exception_to_status_code = True
jcgregorio07a9a4a2007-03-08 21:18:39 +0000792 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
793 try:
794 import socket
795 socket.setdefaulttimeout(1)
796 except:
797 # Don't run the test if we can't set the timeout
798 return
799 (response, content) = self.http.request(uri)
800 self.assertEqual(response.status, 408)
801 self.assertTrue(response.reason.startswith("Request Timeout"))
802 self.assertTrue(content.startswith("Request Timeout"))
803
jcgregoriob2697912007-03-09 02:23:47 +0000804 def testIndividualTimeout(self):
jcgregoriob2697912007-03-09 02:23:47 +0000805 uri = urlparse.urljoin(base, "timeout/timeout.cgi")
806 http = httplib2.Http(timeout=1)
joe.gregoriof28536d2007-10-23 14:10:11 +0000807 http.force_exception_to_status_code = True
jcgregoriob2697912007-03-09 02:23:47 +0000808
809 (response, content) = http.request(uri)
810 self.assertEqual(response.status, 408)
811 self.assertTrue(response.reason.startswith("Request Timeout"))
812 self.assertTrue(content.startswith("Request Timeout"))
813
jcgregorio07a9a4a2007-03-08 21:18:39 +0000814
Joe Gregorio1a7609f2009-07-16 10:59:44 -0400815 def testHTTPSInitTimeout(self):
816 c = httplib2.HTTPSConnectionWithTimeout('localhost', 80, timeout=47)
817 self.assertEqual(47, c.timeout)
818
jcgregorio2d66d4f2006-02-07 05:34:14 +0000819 def testGetDeflate(self):
820 # Test that we support deflate compression
821 uri = urlparse.urljoin(base, "deflate/deflated.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000822 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000823 self.assertEqual(response.status, 200)
jcgregorio90fb4a42006-11-17 16:19:47 +0000824 self.assertFalse(response.has_key('content-encoding'))
jcgregorio153f5882006-11-06 03:33:24 +0000825 self.assertEqual(int(response['content-length']), len("This is the final destination."))
jcgregorio2d66d4f2006-02-07 05:34:14 +0000826 self.assertEqual(content, "This is the final destination.")
827
828 def testGetDeflateFailure(self):
829 # Test that we raise a good exception when the deflate fails
jcgregorio07a9a4a2007-03-08 21:18:39 +0000830 self.http.force_exception_to_status_code = False
831
jcgregorio2d66d4f2006-02-07 05:34:14 +0000832 uri = urlparse.urljoin(base, "deflate/failed-compression.asis")
833 try:
jcgregorio36140b52006-06-13 02:17:52 +0000834 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000835 self.fail("Should never reach here")
836 except httplib2.FailedToDecompressContent:
837 pass
838 except Exception:
839 self.fail("Threw wrong kind of exception")
840
jcgregorio07a9a4a2007-03-08 21:18:39 +0000841 # Re-run the test with out the exceptions
842 self.http.force_exception_to_status_code = True
843
844 (response, content) = self.http.request(uri, "GET")
845 self.assertEqual(response.status, 500)
846 self.assertTrue(response.reason.startswith("Content purported"))
847
jcgregorio2d66d4f2006-02-07 05:34:14 +0000848 def testGetDuplicateHeaders(self):
849 # Test that duplicate headers get concatenated via ','
850 uri = urlparse.urljoin(base, "duplicate-headers/multilink.asis")
jcgregorio36140b52006-06-13 02:17:52 +0000851 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000852 self.assertEqual(response.status, 200)
853 self.assertEqual(content, "This is content\n")
854 self.assertEqual(response['link'].split(",")[0], '<http://bitworking.org>; rel="home"; title="BitWorking"')
855
856 def testGetCacheControlNoCache(self):
857 # Test Cache-Control: no-cache on requests
858 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000859 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000860 self.assertNotEqual(response['etag'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000861 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000862 self.assertEqual(response.status, 200)
863 self.assertEqual(response.fromcache, True)
864
jcgregorio36140b52006-06-13 02:17:52 +0000865 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000866 self.assertEqual(response.status, 200)
867 self.assertEqual(response.fromcache, False)
868
869 def testGetCacheControlPragmaNoCache(self):
870 # Test Pragma: no-cache on requests
871 uri = urlparse.urljoin(base, "304/test_etag.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000872 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000873 self.assertNotEqual(response['etag'], "")
jcgregorio36140b52006-06-13 02:17:52 +0000874 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000875 self.assertEqual(response.status, 200)
876 self.assertEqual(response.fromcache, True)
877
jcgregorio36140b52006-06-13 02:17:52 +0000878 (response, content) = self.http.request(uri, "GET", headers={'Pragma': 'no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000879 self.assertEqual(response.status, 200)
880 self.assertEqual(response.fromcache, False)
881
882 def testGetCacheControlNoStoreRequest(self):
883 # A no-store request means that the response should not be stored.
884 uri = urlparse.urljoin(base, "304/test_etag.txt")
885
jcgregorio36140b52006-06-13 02:17:52 +0000886 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000887 self.assertEqual(response.status, 200)
888 self.assertEqual(response.fromcache, False)
889
jcgregorio36140b52006-06-13 02:17:52 +0000890 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000891 self.assertEqual(response.status, 200)
892 self.assertEqual(response.fromcache, False)
893
894 def testGetCacheControlNoStoreResponse(self):
895 # A no-store response means that the response should not be stored.
896 uri = urlparse.urljoin(base, "no-store/no-store.asis")
897
jcgregorio36140b52006-06-13 02:17:52 +0000898 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000899 self.assertEqual(response.status, 200)
900 self.assertEqual(response.fromcache, False)
901
jcgregorio36140b52006-06-13 02:17:52 +0000902 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000903 self.assertEqual(response.status, 200)
904 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000905
906 def testGetCacheControlNoCacheNoStoreRequest(self):
907 # Test that a no-store, no-cache clears the entry from the cache
908 # even if it was cached previously.
909 uri = urlparse.urljoin(base, "304/test_etag.txt")
910
jcgregorio36140b52006-06-13 02:17:52 +0000911 (response, content) = self.http.request(uri, "GET")
912 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000913 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000914 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
915 (response, content) = self.http.request(uri, "GET", headers={'Cache-Control': 'no-store, no-cache'})
jcgregorio2d66d4f2006-02-07 05:34:14 +0000916 self.assertEqual(response.status, 200)
917 self.assertEqual(response.fromcache, False)
jcgregorio2d66d4f2006-02-07 05:34:14 +0000918
919 def testUpdateInvalidatesCache(self):
920 # Test that calling PUT or DELETE on a
921 # URI that is cache invalidates that cache.
922 uri = urlparse.urljoin(base, "304/test_etag.txt")
923
jcgregorio36140b52006-06-13 02:17:52 +0000924 (response, content) = self.http.request(uri, "GET")
925 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000926 self.assertEqual(response.fromcache, True)
jcgregorio36140b52006-06-13 02:17:52 +0000927 (response, content) = self.http.request(uri, "DELETE")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000928 self.assertEqual(response.status, 405)
929
jcgregorio36140b52006-06-13 02:17:52 +0000930 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000931 self.assertEqual(response.fromcache, False)
932
933 def testUpdateUsesCachedETag(self):
Joe Gregoriobd682082011-05-24 14:06:09 -0400934 # Test that we natively support http://www.w3.org/1999/04/Editing/
jcgregorio2d66d4f2006-02-07 05:34:14 +0000935 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
936
jcgregorio36140b52006-06-13 02:17:52 +0000937 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000938 self.assertEqual(response.status, 200)
939 self.assertEqual(response.fromcache, False)
jcgregorio36140b52006-06-13 02:17:52 +0000940 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000941 self.assertEqual(response.status, 200)
942 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400943 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000944 self.assertEqual(response.status, 200)
Joe Gregoriocd868102009-09-29 17:09:16 -0400945 (response, content) = self.http.request(uri, "PUT", body="foo")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000946 self.assertEqual(response.status, 412)
947
Joe Gregoriobd682082011-05-24 14:06:09 -0400948 def testUpdatePatchUsesCachedETag(self):
949 # Test that we natively support http://www.w3.org/1999/04/Editing/
950 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
951
952 (response, content) = self.http.request(uri, "GET")
953 self.assertEqual(response.status, 200)
954 self.assertEqual(response.fromcache, False)
955 (response, content) = self.http.request(uri, "GET")
956 self.assertEqual(response.status, 200)
957 self.assertEqual(response.fromcache, True)
958 (response, content) = self.http.request(uri, "PATCH", body="foo")
959 self.assertEqual(response.status, 200)
960 (response, content) = self.http.request(uri, "PATCH", body="foo")
961 self.assertEqual(response.status, 412)
962
963
joe.gregorio700f04d2008-09-06 04:46:32 +0000964 def testUpdateUsesCachedETagAndOCMethod(self):
965 # Test that we natively support http://www.w3.org/1999/04/Editing/
966 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
967
968 (response, content) = self.http.request(uri, "GET")
969 self.assertEqual(response.status, 200)
970 self.assertEqual(response.fromcache, False)
971 (response, content) = self.http.request(uri, "GET")
972 self.assertEqual(response.status, 200)
973 self.assertEqual(response.fromcache, True)
974 self.http.optimistic_concurrency_methods.append("DELETE")
975 (response, content) = self.http.request(uri, "DELETE")
976 self.assertEqual(response.status, 200)
977
978
jcgregorio4b145e82007-01-18 19:46:34 +0000979 def testUpdateUsesCachedETagOverridden(self):
980 # Test that we natively support http://www.w3.org/1999/04/Editing/
981 uri = urlparse.urljoin(base, "conditional-updates/test.cgi")
982
983 (response, content) = self.http.request(uri, "GET")
984 self.assertEqual(response.status, 200)
985 self.assertEqual(response.fromcache, False)
986 (response, content) = self.http.request(uri, "GET")
987 self.assertEqual(response.status, 200)
988 self.assertEqual(response.fromcache, True)
Joe Gregoriocd868102009-09-29 17:09:16 -0400989 (response, content) = self.http.request(uri, "PUT", body="foo", headers={'if-match': 'fred'})
jcgregorio4b145e82007-01-18 19:46:34 +0000990 self.assertEqual(response.status, 412)
991
jcgregorio2d66d4f2006-02-07 05:34:14 +0000992 def testBasicAuth(self):
993 # Test Basic Authentication
994 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +0000995 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +0000996 self.assertEqual(response.status, 401)
997
998 uri = urlparse.urljoin(base, "basic/")
jcgregorio36140b52006-06-13 02:17:52 +0000999 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001000 self.assertEqual(response.status, 401)
1001
jcgregorio36140b52006-06-13 02:17:52 +00001002 self.http.add_credentials('joe', 'password')
1003 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001004 self.assertEqual(response.status, 200)
1005
1006 uri = urlparse.urljoin(base, "basic/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001007 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001008 self.assertEqual(response.status, 200)
1009
jcgregoriode8238d2007-03-07 19:08:26 +00001010 def testBasicAuthWithDomain(self):
1011 # Test Basic Authentication
1012 uri = urlparse.urljoin(base, "basic/file.txt")
1013 (response, content) = self.http.request(uri, "GET")
1014 self.assertEqual(response.status, 401)
1015
1016 uri = urlparse.urljoin(base, "basic/")
1017 (response, content) = self.http.request(uri, "GET")
1018 self.assertEqual(response.status, 401)
1019
1020 self.http.add_credentials('joe', 'password', "example.org")
1021 (response, content) = self.http.request(uri, "GET")
1022 self.assertEqual(response.status, 401)
1023
1024 uri = urlparse.urljoin(base, "basic/file.txt")
1025 (response, content) = self.http.request(uri, "GET")
1026 self.assertEqual(response.status, 401)
1027
1028 domain = urlparse.urlparse(base)[1]
1029 self.http.add_credentials('joe', 'password', domain)
1030 (response, content) = self.http.request(uri, "GET")
1031 self.assertEqual(response.status, 200)
1032
1033 uri = urlparse.urljoin(base, "basic/file.txt")
1034 (response, content) = self.http.request(uri, "GET")
1035 self.assertEqual(response.status, 200)
1036
1037
1038
1039
1040
1041
jcgregorio2d66d4f2006-02-07 05:34:14 +00001042 def testBasicAuthTwoDifferentCredentials(self):
jcgregorioadbb4f82006-05-19 15:17:42 +00001043 # Test Basic Authentication with multiple sets of credentials
jcgregorio2d66d4f2006-02-07 05:34:14 +00001044 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001045 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001046 self.assertEqual(response.status, 401)
1047
1048 uri = urlparse.urljoin(base, "basic2/")
jcgregorio36140b52006-06-13 02:17:52 +00001049 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001050 self.assertEqual(response.status, 401)
1051
jcgregorio36140b52006-06-13 02:17:52 +00001052 self.http.add_credentials('fred', 'barney')
1053 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001054 self.assertEqual(response.status, 200)
1055
1056 uri = urlparse.urljoin(base, "basic2/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001057 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001058 self.assertEqual(response.status, 200)
1059
1060 def testBasicAuthNested(self):
1061 # Test Basic Authentication with resources
1062 # that are nested
1063 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +00001064 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001065 self.assertEqual(response.status, 401)
1066
1067 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +00001068 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001069 self.assertEqual(response.status, 401)
1070
jcgregorioadbb4f82006-05-19 15:17:42 +00001071 # Now add in credentials one at a time and test.
jcgregorio36140b52006-06-13 02:17:52 +00001072 self.http.add_credentials('joe', 'password')
jcgregorio2d66d4f2006-02-07 05:34:14 +00001073
1074 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +00001075 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001076 self.assertEqual(response.status, 200)
1077
1078 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +00001079 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001080 self.assertEqual(response.status, 401)
1081
jcgregorio36140b52006-06-13 02:17:52 +00001082 self.http.add_credentials('fred', 'barney')
jcgregorio2d66d4f2006-02-07 05:34:14 +00001083
1084 uri = urlparse.urljoin(base, "basic-nested/")
jcgregorio36140b52006-06-13 02:17:52 +00001085 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001086 self.assertEqual(response.status, 200)
1087
1088 uri = urlparse.urljoin(base, "basic-nested/subdir")
jcgregorio36140b52006-06-13 02:17:52 +00001089 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001090 self.assertEqual(response.status, 200)
1091
1092 def testDigestAuth(self):
1093 # Test that we support Digest Authentication
1094 uri = urlparse.urljoin(base, "digest/")
jcgregorio36140b52006-06-13 02:17:52 +00001095 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001096 self.assertEqual(response.status, 401)
1097
jcgregorio36140b52006-06-13 02:17:52 +00001098 self.http.add_credentials('joe', 'password')
1099 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001100 self.assertEqual(response.status, 200)
1101
1102 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001103 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001104
1105 def testDigestAuthNextNonceAndNC(self):
1106 # Test that if the server sets nextnonce that we reset
1107 # the nonce count back to 1
1108 uri = urlparse.urljoin(base, "digest/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001109 self.http.add_credentials('joe', 'password')
1110 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001111 info = httplib2._parse_www_authenticate(response, 'authentication-info')
1112 self.assertEqual(response.status, 200)
jcgregorio36140b52006-06-13 02:17:52 +00001113 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001114 info2 = httplib2._parse_www_authenticate(response, 'authentication-info')
1115 self.assertEqual(response.status, 200)
1116
1117 if info.has_key('nextnonce'):
1118 self.assertEqual(info2['nc'], 1)
1119
1120 def testDigestAuthStale(self):
1121 # Test that we can handle a nonce becoming stale
1122 uri = urlparse.urljoin(base, "digest-expire/file.txt")
jcgregorio36140b52006-06-13 02:17:52 +00001123 self.http.add_credentials('joe', 'password')
1124 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001125 info = httplib2._parse_www_authenticate(response, 'authentication-info')
1126 self.assertEqual(response.status, 200)
1127
1128 time.sleep(3)
1129 # Sleep long enough that the nonce becomes stale
1130
jcgregorio36140b52006-06-13 02:17:52 +00001131 (response, content) = self.http.request(uri, "GET", headers = {"cache-control":"no-cache"})
jcgregorio2d66d4f2006-02-07 05:34:14 +00001132 self.assertFalse(response.fromcache)
1133 self.assertTrue(response._stale_digest)
1134 info3 = httplib2._parse_www_authenticate(response, 'authentication-info')
1135 self.assertEqual(response.status, 200)
1136
1137 def reflector(self, content):
jcgregorio25185622006-10-28 05:12:34 +00001138 return dict( [tuple(x.split("=", 1)) for x in content.strip().split("\n")] )
jcgregorio2d66d4f2006-02-07 05:34:14 +00001139
1140 def testReflector(self):
1141 uri = urlparse.urljoin(base, "reflector/reflector.cgi")
jcgregorio36140b52006-06-13 02:17:52 +00001142 (response, content) = self.http.request(uri, "GET")
jcgregorio2d66d4f2006-02-07 05:34:14 +00001143 d = self.reflector(content)
1144 self.assertTrue(d.has_key('HTTP_USER_AGENT'))
1145
Joe Gregorio84cc10a2009-09-01 13:02:49 -04001146 def testConnectionClose(self):
1147 uri = "http://www.google.com/"
1148 (response, content) = self.http.request(uri, "GET")
1149 for c in self.http.connections.values():
1150 self.assertNotEqual(None, c.sock)
1151 (response, content) = self.http.request(uri, "GET", headers={"connection": "close"})
1152 for c in self.http.connections.values():
1153 self.assertEqual(None, c.sock)
1154
1155
jcgregorio36140b52006-06-13 02:17:52 +00001156try:
1157 import memcache
1158 class HttpTestMemCached(HttpTest):
1159 def setUp(self):
1160 self.cache = memcache.Client(['127.0.0.1:11211'], debug=0)
jcgregorio47d24672006-06-29 05:18:59 +00001161 #self.cache = memcache.Client(['10.0.0.4:11211'], debug=1)
jcgregorio36140b52006-06-13 02:17:52 +00001162 self.http = httplib2.Http(self.cache)
1163 self.cache.flush_all()
jcgregorio47d24672006-06-29 05:18:59 +00001164 # Not exactly sure why the sleep is needed here, but
1165 # if not present then some unit tests that rely on caching
1166 # fail. Memcached seems to lose some sets immediately
1167 # after a flush_all if the set is to a value that
1168 # was previously cached. (Maybe the flush is handled async?)
1169 time.sleep(1)
jcgregorio36140b52006-06-13 02:17:52 +00001170 self.http.clear_credentials()
1171except:
1172 pass
1173
1174
1175
chris dent89f15142009-12-24 14:02:57 -06001176
jcgregoriodb8dfc82006-03-31 14:59:46 +00001177# ------------------------------------------------------------------------
jcgregorio2d66d4f2006-02-07 05:34:14 +00001178
1179class HttpPrivateTest(unittest.TestCase):
1180
1181 def testParseCacheControl(self):
1182 # Test that we can parse the Cache-Control header
1183 self.assertEqual({}, httplib2._parse_cache_control({}))
1184 self.assertEqual({'no-cache': 1}, httplib2._parse_cache_control({'cache-control': ' no-cache'}))
1185 cc = httplib2._parse_cache_control({'cache-control': ' no-cache, max-age = 7200'})
1186 self.assertEqual(cc['no-cache'], 1)
1187 self.assertEqual(cc['max-age'], '7200')
1188 cc = httplib2._parse_cache_control({'cache-control': ' , '})
1189 self.assertEqual(cc[''], 1)
1190
Joe Gregorioe314e8b2009-07-16 20:11:28 -04001191 try:
1192 cc = httplib2._parse_cache_control({'cache-control': 'Max-age=3600;post-check=1800,pre-check=3600'})
1193 self.assertTrue("max-age" in cc)
1194 except:
1195 self.fail("Should not throw exception")
1196
jcgregorio2d66d4f2006-02-07 05:34:14 +00001197 def testNormalizeHeaders(self):
1198 # Test that we normalize headers to lowercase
1199 h = httplib2._normalize_headers({'Cache-Control': 'no-cache', 'Other': 'Stuff'})
1200 self.assertTrue(h.has_key('cache-control'))
1201 self.assertTrue(h.has_key('other'))
1202 self.assertEqual('Stuff', h['other'])
1203
1204 def testExpirationModelTransparent(self):
1205 # Test that no-cache makes our request TRANSPARENT
1206 response_headers = {
1207 'cache-control': 'max-age=7200'
1208 }
1209 request_headers = {
1210 'cache-control': 'no-cache'
1211 }
1212 self.assertEqual("TRANSPARENT", httplib2._entry_disposition(response_headers, request_headers))
1213
jcgregorio45865012007-01-18 16:38:22 +00001214 def testMaxAgeNonNumeric(self):
1215 # Test that no-cache makes our request TRANSPARENT
1216 response_headers = {
1217 'cache-control': 'max-age=fred, min-fresh=barney'
1218 }
1219 request_headers = {
1220 }
1221 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1222
1223
jcgregorio2d66d4f2006-02-07 05:34:14 +00001224 def testExpirationModelNoCacheResponse(self):
1225 # The date and expires point to an entry that should be
1226 # FRESH, but the no-cache over-rides that.
1227 now = time.time()
1228 response_headers = {
1229 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1230 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1231 'cache-control': 'no-cache'
1232 }
1233 request_headers = {
1234 }
1235 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1236
1237 def testExpirationModelStaleRequestMustReval(self):
1238 # must-revalidate forces STALE
1239 self.assertEqual("STALE", httplib2._entry_disposition({}, {'cache-control': 'must-revalidate'}))
1240
1241 def testExpirationModelStaleResponseMustReval(self):
1242 # must-revalidate forces STALE
1243 self.assertEqual("STALE", httplib2._entry_disposition({'cache-control': 'must-revalidate'}, {}))
1244
1245 def testExpirationModelFresh(self):
1246 response_headers = {
1247 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1248 'cache-control': 'max-age=2'
1249 }
1250 request_headers = {
1251 }
1252 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1253 time.sleep(3)
1254 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1255
1256 def testExpirationMaxAge0(self):
1257 response_headers = {
1258 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime()),
1259 'cache-control': 'max-age=0'
1260 }
1261 request_headers = {
1262 }
1263 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1264
1265 def testExpirationModelDateAndExpires(self):
1266 now = time.time()
1267 response_headers = {
1268 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1269 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1270 }
1271 request_headers = {
1272 }
1273 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1274 time.sleep(3)
1275 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1276
jcgregoriof9511052007-06-01 14:56:34 +00001277 def testExpiresZero(self):
1278 now = time.time()
1279 response_headers = {
1280 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1281 'expires': "0",
1282 }
1283 request_headers = {
1284 }
1285 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1286
jcgregorio2d66d4f2006-02-07 05:34:14 +00001287 def testExpirationModelDateOnly(self):
1288 now = time.time()
1289 response_headers = {
1290 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+3)),
1291 }
1292 request_headers = {
1293 }
1294 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1295
1296 def testExpirationModelOnlyIfCached(self):
1297 response_headers = {
1298 }
1299 request_headers = {
1300 'cache-control': 'only-if-cached',
1301 }
1302 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1303
1304 def testExpirationModelMaxAgeBoth(self):
1305 now = time.time()
1306 response_headers = {
1307 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1308 'cache-control': 'max-age=2'
1309 }
1310 request_headers = {
1311 'cache-control': 'max-age=0'
1312 }
1313 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1314
1315 def testExpirationModelDateAndExpiresMinFresh1(self):
1316 now = time.time()
1317 response_headers = {
1318 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1319 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+2)),
1320 }
1321 request_headers = {
1322 'cache-control': 'min-fresh=2'
1323 }
1324 self.assertEqual("STALE", httplib2._entry_disposition(response_headers, request_headers))
1325
1326 def testExpirationModelDateAndExpiresMinFresh2(self):
1327 now = time.time()
1328 response_headers = {
1329 'date': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now)),
1330 'expires': time.strftime("%a, %d %b %Y %H:%M:%S GMT", time.gmtime(now+4)),
1331 }
1332 request_headers = {
1333 'cache-control': 'min-fresh=2'
1334 }
1335 self.assertEqual("FRESH", httplib2._entry_disposition(response_headers, request_headers))
1336
1337 def testParseWWWAuthenticateEmpty(self):
1338 res = httplib2._parse_www_authenticate({})
1339 self.assertEqual(len(res.keys()), 0)
1340
jcgregoriofd22e432006-04-27 02:00:08 +00001341 def testParseWWWAuthenticate(self):
1342 # different uses of spaces around commas
1343 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="test realm" , foo=foo ,bar="bar", baz=baz,qux=qux'})
1344 self.assertEqual(len(res.keys()), 1)
1345 self.assertEqual(len(res['test'].keys()), 5)
1346
1347 # tokens with non-alphanum
1348 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'T*!%#st realm=to*!%#en, to*!%#en="quoted string"'})
1349 self.assertEqual(len(res.keys()), 1)
1350 self.assertEqual(len(res['t*!%#st'].keys()), 2)
1351
1352 # quoted string with quoted pairs
1353 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Test realm="a \\"test\\" realm"'})
1354 self.assertEqual(len(res.keys()), 1)
1355 self.assertEqual(res['test']['realm'], 'a "test" realm')
1356
1357 def testParseWWWAuthenticateStrict(self):
1358 httplib2.USE_WWW_AUTH_STRICT_PARSING = 1;
1359 self.testParseWWWAuthenticate();
1360 httplib2.USE_WWW_AUTH_STRICT_PARSING = 0;
1361
jcgregorio2d66d4f2006-02-07 05:34:14 +00001362 def testParseWWWAuthenticateBasic(self):
1363 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me"'})
1364 basic = res['basic']
1365 self.assertEqual('me', basic['realm'])
1366
1367 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm="MD5"'})
1368 basic = res['basic']
1369 self.assertEqual('me', basic['realm'])
1370 self.assertEqual('MD5', basic['algorithm'])
1371
1372 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me", algorithm=MD5'})
1373 basic = res['basic']
1374 self.assertEqual('me', basic['realm'])
1375 self.assertEqual('MD5', basic['algorithm'])
1376
1377 def testParseWWWAuthenticateBasic2(self):
1378 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic realm="me",other="fred" '})
1379 basic = res['basic']
1380 self.assertEqual('me', basic['realm'])
1381 self.assertEqual('fred', basic['other'])
1382
1383 def testParseWWWAuthenticateBasic3(self):
1384 res = httplib2._parse_www_authenticate({ 'www-authenticate': 'Basic REAlm="me" '})
1385 basic = res['basic']
1386 self.assertEqual('me', basic['realm'])
1387
1388
1389 def testParseWWWAuthenticateDigest(self):
1390 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1391 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"'})
1392 digest = res['digest']
1393 self.assertEqual('testrealm@host.com', digest['realm'])
1394 self.assertEqual('auth,auth-int', digest['qop'])
1395
1396
1397 def testParseWWWAuthenticateMultiple(self):
1398 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1399 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41" Basic REAlm="me" '})
1400 digest = res['digest']
1401 self.assertEqual('testrealm@host.com', digest['realm'])
1402 self.assertEqual('auth,auth-int', digest['qop'])
1403 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1404 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1405 basic = res['basic']
1406 self.assertEqual('me', basic['realm'])
1407
1408 def testParseWWWAuthenticateMultiple2(self):
1409 # Handle an added comma between challenges, which might get thrown in if the challenges were
1410 # originally sent in separate www-authenticate headers.
1411 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1412 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me" '})
1413 digest = res['digest']
1414 self.assertEqual('testrealm@host.com', digest['realm'])
1415 self.assertEqual('auth,auth-int', digest['qop'])
1416 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1417 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1418 basic = res['basic']
1419 self.assertEqual('me', basic['realm'])
1420
1421 def testParseWWWAuthenticateMultiple3(self):
1422 # Handle an added comma between challenges, which might get thrown in if the challenges were
1423 # originally sent in separate www-authenticate headers.
1424 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1425 'Digest realm="testrealm@host.com", qop="auth,auth-int", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1426 digest = res['digest']
1427 self.assertEqual('testrealm@host.com', digest['realm'])
1428 self.assertEqual('auth,auth-int', digest['qop'])
1429 self.assertEqual('dcd98b7102dd2f0e8b11d0f600bfb0c093', digest['nonce'])
1430 self.assertEqual('5ccc069c403ebaf9f0171e9517f40e41', digest['opaque'])
1431 basic = res['basic']
1432 self.assertEqual('me', basic['realm'])
1433 wsse = res['wsse']
1434 self.assertEqual('foo', wsse['realm'])
1435 self.assertEqual('UsernameToken', wsse['profile'])
1436
1437 def testParseWWWAuthenticateMultiple4(self):
1438 res = httplib2._parse_www_authenticate({ 'www-authenticate':
1439 'Digest realm="test-real.m@host.com", qop \t=\t"\tauth,auth-int", nonce="(*)&^&$%#",opaque="5ccc069c403ebaf9f0171e9517f40e41", Basic REAlm="me", WSSE realm="foo", profile="UsernameToken"'})
1440 digest = res['digest']
1441 self.assertEqual('test-real.m@host.com', digest['realm'])
1442 self.assertEqual('\tauth,auth-int', digest['qop'])
1443 self.assertEqual('(*)&^&$%#', digest['nonce'])
1444
1445 def testParseWWWAuthenticateMoreQuoteCombos(self):
1446 res = httplib2._parse_www_authenticate({'www-authenticate':'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'})
1447 digest = res['digest']
1448 self.assertEqual('myrealm', digest['realm'])
1449
Joe Gregorio6fa3cf22011-02-13 22:45:06 -05001450 def testParseWWWAuthenticateMalformed(self):
1451 try:
1452 res = httplib2._parse_www_authenticate({'www-authenticate':'OAuth "Facebook Platform" "invalid_token" "Invalid OAuth access token."'})
1453 self.fail("should raise an exception")
1454 except httplib2.MalformedHeader:
1455 pass
1456
jcgregorio2d66d4f2006-02-07 05:34:14 +00001457 def testDigestObject(self):
1458 credentials = ('joe', 'password')
1459 host = None
1460 request_uri = '/projects/httplib2/test/digest/'
1461 headers = {}
1462 response = {
1463 'www-authenticate': 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth"'
1464 }
1465 content = ""
1466
jcgregorio6cbab7e2006-04-21 20:35:43 +00001467 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001468 d.request("GET", request_uri, headers, content, cnonce="33033375ec278a46")
1469 our_request = "Authorization: %s" % headers['Authorization']
1470 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"'
1471 self.assertEqual(our_request, working_request)
1472
1473
1474 def testDigestObjectStale(self):
1475 credentials = ('joe', 'password')
1476 host = None
1477 request_uri = '/projects/httplib2/test/digest/'
1478 headers = {}
1479 response = httplib2.Response({ })
1480 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1481 response.status = 401
1482 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001483 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001484 # Returns true to force a retry
1485 self.assertTrue( d.response(response, content) )
1486
1487 def testDigestObjectAuthInfo(self):
1488 credentials = ('joe', 'password')
1489 host = None
1490 request_uri = '/projects/httplib2/test/digest/'
1491 headers = {}
1492 response = httplib2.Response({ })
1493 response['www-authenticate'] = 'Digest realm="myrealm", nonce="Ygk86AsKBAA=3516200d37f9a3230352fde99977bd6d472d4306", algorithm=MD5, qop="auth", stale=true'
1494 response['authentication-info'] = 'nextnonce="fred"'
1495 content = ""
jcgregorio6cbab7e2006-04-21 20:35:43 +00001496 d = httplib2.DigestAuthentication(credentials, host, request_uri, headers, response, content, None)
jcgregorio2d66d4f2006-02-07 05:34:14 +00001497 # Returns true to force a retry
1498 self.assertFalse( d.response(response, content) )
1499 self.assertEqual('fred', d.challenge['nonce'])
1500 self.assertEqual(1, d.challenge['nc'])
1501
1502 def testWsseAlgorithm(self):
1503 digest = httplib2._wsse_username_token("d36e316282959a9ed4c89851497a717f", "2003-12-15T14:43:07Z", "taadtaadpstcsm")
1504 expected = "quR/EWLAV4xLf9Zqyw4pDmfV9OY="
1505 self.assertEqual(expected, digest)
1506
jcgregoriodb8dfc82006-03-31 14:59:46 +00001507 def testEnd2End(self):
1508 # one end to end header
1509 response = {'content-type': 'application/atom+xml', 'te': 'deflate'}
1510 end2end = httplib2._get_end2end_headers(response)
1511 self.assertTrue('content-type' in end2end)
1512 self.assertTrue('te' not in end2end)
1513 self.assertTrue('connection' not in end2end)
1514
1515 # one end to end header that gets eliminated
1516 response = {'connection': 'content-type', 'content-type': 'application/atom+xml', 'te': 'deflate'}
1517 end2end = httplib2._get_end2end_headers(response)
1518 self.assertTrue('content-type' not in end2end)
1519 self.assertTrue('te' not in end2end)
1520 self.assertTrue('connection' not in end2end)
1521
1522 # Degenerate case of no headers
1523 response = {}
1524 end2end = httplib2._get_end2end_headers(response)
1525 self.assertEquals(0, len(end2end))
1526
1527 # Degenerate case of connection referrring to a header not passed in
1528 response = {'connection': 'content-type'}
1529 end2end = httplib2._get_end2end_headers(response)
1530 self.assertEquals(0, len(end2end))
jcgregorio2d66d4f2006-02-07 05:34:14 +00001531
chris dent89f15142009-12-24 14:02:57 -06001532if __name__ == '__main__':
1533 unittest.main()