330
_single_range_response = ("""HTTP/1.1 206 Partial Content\r
330
_single_range_response = (206, """HTTP/1.1 206 Partial Content\r
331
331
Date: Tue, 11 Jul 2006 04:45:22 GMT\r
332
332
Server: Apache/2.0.54 (Fedora)\r
333
333
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
334
334
ETag: "238a3c-16ec2-805c5540"\r
335
335
Accept-Ranges: bytes\r
336
336
Content-Length: 100\r
337
Content-Range: bytes 0-99/93890\r
337
Content-Range: bytes 100-199/93890\r
338
338
Connection: close\r
339
339
Content-Type: text/plain; charset=UTF-8\r
341
341
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
342
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762
346
_multipart_range_response = ("""HTTP/1.1 206 Partial Content\r
342
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
345
_multipart_range_response = (206, """HTTP/1.1 206 Partial Content\r
347
346
Date: Tue, 11 Jul 2006 04:49:48 GMT\r
348
347
Server: Apache/2.0.54 (Fedora)\r
349
348
Last-Modified: Thu, 06 Jul 2006 20:22:05 GMT\r
396
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
397
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
399
Content-Type: text/html; charset=iso-8859-1\r
401
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
403
<title>404 Not Found</title>
406
<p>I don't know what I'm doing</p>
396
412
# This should be in test_http.py, but the headers we
397
413
# want to parse are here
398
414
class TestExtractHeader(TestCase):
400
416
def use_response(self, response):
401
self.headers = http._extract_headers(StringIO(response[0]))
417
self.headers = http._extract_headers(StringIO(response[1]))
403
419
def check_header(self, header, value):
404
420
self.assertEqual(value, self.headers[header])
431
447
self.check_header('Content-Length', '1534')
432
448
self.check_header('Content-Type',
433
449
'multipart/byteranges; boundary=418470f848b63279b')
452
class TestHandleResponse(TestCase):
454
def get_response(self, a_response):
455
"""Process a supplied response, and return the result."""
456
headers = http._extract_headers(StringIO(a_response[1]))
457
return response.handle_response('http://foo', a_response[0], headers,
458
StringIO(a_response[2]))
460
def test_full_text(self):
461
out = self.get_response(_full_text_response)
462
# It is a StringIO from the original data
463
self.assertEqual(_full_text_response[2], out.read())
465
def test_missing_response(self):
466
self.assertRaises(errors.NoSuchFile,
467
self.get_response, _missing_response)
469
def test_single_range(self):
470
out = self.get_response(_single_range_response)
471
self.assertIsInstance(out, response.HttpRangeResponse)
473
self.assertRaises(errors.InvalidRange, out.read, 20)
476
self.assertEqual(_single_range_response[2], out.read(100))
478
def test_multi_range(self):
479
out = self.get_response(_multipart_range_response)
480
self.assertIsInstance(out, response.HttpMultipartRangeResponse)
482
# Just make sure we can read the right contents
489
def test_invalid_response(self):
490
self.assertRaises(errors.InvalidHttpResponse,
491
self.get_response, _invalid_response)
493
def test_full_text_no_content_type(self):
494
# We should not require Content-Type for a full response
495
a_response = _full_text_response
496
headers = http._extract_headers(StringIO(a_response[1]))
497
del headers['Content-Type']
498
out = response.handle_response('http://foo', a_response[0], headers,
499
StringIO(a_response[2]))
500
self.assertEqual(_full_text_response[2], out.read())
502
def test_missing_no_content_type(self):
503
# Without Content-Type we should still raise NoSuchFile on a 404
504
a_response = _missing_response
505
headers = http._extract_headers(StringIO(a_response[1]))
506
del headers['Content-Type']
507
self.assertRaises(errors.NoSuchFile,
508
response.handle_response, 'http://missing', a_response[0], headers,
509
StringIO(a_response[2]))
511
def test_missing_content_type(self):
512
a_response = _single_range_response
513
headers = http._extract_headers(StringIO(a_response[1]))
514
del headers['Content-Type']
515
self.assertRaises(errors.InvalidHttpContentType,
516
response.handle_response, 'http://nocontent', a_response[0],
517
headers, StringIO(a_response[2]))
519
def test_missing_content_range(self):
520
a_response = _single_range_response
521
headers = http._extract_headers(StringIO(a_response[1]))
522
del headers['Content-Range']
523
self.assertRaises(errors.InvalidHttpResponse,
524
response.handle_response, 'http://nocontent', a_response[0],
525
headers, StringIO(a_response[2]))