438
441
# want to parse are here
439
442
class TestExtractHeader(TestCase):
441
def use_response(self, response, **kwargs):
442
self.headers = http._extract_headers(StringIO(response[1]), **kwargs)
444
def use_response(self, response):
445
self.headers = http._extract_headers(response[1], 'http://foo')
444
447
def check_header(self, header, value):
445
448
self.assertEqual(value, self.headers[header])
479
482
self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
480
483
self.check_header('Content-Type', 'text/plain; charset=UTF-8')
482
def test_redirect_body_is_body(self):
483
"""Check that we parse the right portion if body_is_header is True"""
484
self.use_response(_redirect_response, body_is_header=False)
485
self.assertRaises(KeyError, self.headers.__getitem__, 'Content-Range')
486
self.check_header('Content-Type', 'text/html; charset=iso-8859-1')
487
self.check_header('Location',
488
'http://bazaar-vcs.org/bzr/bzr.dev/.bzr/repository/inventory.knit')
485
def test_empty(self):
486
self.assertRaises(errors.InvalidHttpResponse,
487
http._extract_headers, '', 'bad url')
489
def test_no_opening_http(self):
490
# Remove the HTTP line from the header
491
first, txt = _full_text_response[1].split('\r\n', 1)
492
self.assertRaises(errors.InvalidHttpResponse,
493
http._extract_headers, txt, 'missing HTTTP')
495
def test_trailing_whitespace(self):
496
# Test that we ignore bogus whitespace on the end
497
code, txt, body = _full_text_response
498
txt += '\r\n\n\n\n\n'
499
self.use_response((code, txt, body))
501
self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
502
self.check_header('Content-Length', '35')
503
self.check_header('Content-Type', 'text/plain; charset=UTF-8')
505
def test_trailing_non_http(self):
506
# Test that we ignore bogus stuff on the end
507
code, txt, body = _full_text_response
508
txt = txt + 'Foo: Bar\r\nBaz: Bling\r\n\r\n'
509
self.use_response((code, txt, body))
511
self.check_header('Date', 'Tue, 11 Jul 2006 04:32:56 GMT')
512
self.check_header('Content-Length', '35')
513
self.check_header('Content-Type', 'text/plain; charset=UTF-8')
514
self.assertRaises(KeyError, self.headers.__getitem__, 'Foo')
516
def test_extra_whitespace(self):
517
# Test that we read an HTTP response, even with extra whitespace
518
code, txt, body = _redirect_response
519
# Find the second HTTP location
520
loc = txt.find('HTTP', 5)
521
txt = txt[:loc] + '\r\n\n' + txt[loc:]
522
self.use_response((code, txt, body))
523
self.check_header('Content-Range', 'bytes 8623075-8623499/8623500')
524
self.check_header('Content-Type', 'text/plain; charset=UTF-8')
491
527
class TestHandleResponse(TestCase):
493
529
def get_response(self, a_response):
494
530
"""Process a supplied response, and return the result."""
495
headers = http._extract_headers(StringIO(a_response[1]))
531
headers = http._extract_headers(a_response[1], 'http://foo')
496
532
return response.handle_response('http://foo', a_response[0], headers,
497
533
StringIO(a_response[2]))
541
577
def test_missing_no_content_type(self):
542
578
# Without Content-Type we should still raise NoSuchFile on a 404
543
579
a_response = _missing_response
544
headers = http._extract_headers(StringIO(a_response[1]))
580
headers = http._extract_headers(a_response[1], 'http://missing')
545
581
del headers['Content-Type']
546
582
self.assertRaises(errors.NoSuchFile,
547
583
response.handle_response, 'http://missing', a_response[0], headers,