~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http_response.py

  • Committer: John Arbash Meinel
  • Date: 2006-07-19 14:17:49 UTC
  • mto: This revision was merged to the branch mainline in revision 1869.
  • Revision ID: john@arbash-meinel.com-20060719141749-5c8a2a433f966f29
Update _extract_headers, make it less generic, and non recursive.

Show diffs side-by-side

added added

removed removed

Lines of Context:
414
414
Connection: Keep-Alive\r
415
415
Content-Type: text/plain; charset=UTF-8\r
416
416
\r
 
417
""", """this data intentionally removed, 
 
418
this is not meant to be tested by
 
419
handle_response, just _extract_headers
417
420
""")
418
421
 
419
422
 
438
441
# want to parse are here
439
442
class TestExtractHeader(TestCase):
440
443
    
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')
443
446
 
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')
481
484
 
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')
489
 
                       
 
485
    def test_empty(self):
 
486
        self.assertRaises(errors.InvalidHttpResponse,
 
487
            http._extract_headers, '', 'bad url')
 
488
 
 
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')
 
494
 
 
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))
 
500
 
 
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')
 
504
 
 
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))
 
510
 
 
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')
 
515
 
 
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')
 
525
 
490
526
 
491
527
class TestHandleResponse(TestCase):
492
528
    
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]))
498
534
 
532
568
    def test_full_text_no_content_type(self):
533
569
        # We should not require Content-Type for a full response
534
570
        a_response = _full_text_response
535
 
        headers = http._extract_headers(StringIO(a_response[1]))
 
571
        headers = http._extract_headers(a_response[1], 'http://foo')
536
572
        del headers['Content-Type']
537
573
        out = response.handle_response('http://foo', a_response[0], headers,
538
574
                                        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,
549
585
 
550
586
    def test_missing_content_type(self):
551
587
        a_response = _single_range_response
552
 
        headers = http._extract_headers(StringIO(a_response[1]))
 
588
        headers = http._extract_headers(a_response[1], 'http://nocontent')
553
589
        del headers['Content-Type']
554
590
        self.assertRaises(errors.InvalidHttpContentType,
555
591
            response.handle_response, 'http://nocontent', a_response[0],
557
593
 
558
594
    def test_missing_content_range(self):
559
595
        a_response = _single_range_response
560
 
        headers = http._extract_headers(StringIO(a_response[1]))
 
596
        headers = http._extract_headers(a_response[1], 'http://nocontent')
561
597
        del headers['Content-Range']
562
598
        self.assertRaises(errors.InvalidHttpResponse,
563
599
            response.handle_response, 'http://nocontent', a_response[0],