~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-13 17:31:35 UTC
  • mto: This revision was merged to the branch mainline in revision 1869.
  • Revision ID: john@arbash-meinel.com-20060713173135-451d2f4f5c6c8909
Update and test handle_response.

Show diffs side-by-side

added added

removed removed

Lines of Context:
294
294
 
295
295
 
296
296
# Taken from real request responses
297
 
_full_text_response = ("""HTTP/1.1 200 OK\r
 
297
_full_text_response = (200, """HTTP/1.1 200 OK\r
298
298
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
299
299
Server: Apache/2.0.54 (Fedora)\r
300
300
Last-Modified: Sun, 23 Apr 2006 19:35:20 GMT\r
308
308
""")
309
309
 
310
310
 
311
 
_missing_response = ("""HTTP/1.1 404 Not Found\r
 
311
_missing_response = (404, """HTTP/1.1 404 Not Found\r
312
312
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
313
313
Server: Apache/2.0.54 (Fedora)\r
314
314
Content-Length: 336\r
327
327
""")
328
328
 
329
329
 
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
340
340
\r
341
341
""", """mbp@sourcefrog.net-20050309040815-13242001617e4a06
342
 
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762
343
 
""")
344
 
 
345
 
 
346
 
_multipart_range_response = ("""HTTP/1.1 206 Partial Content\r
 
342
mbp@sourcefrog.net-20050309040929-eee0eb3e6d1e762""")
 
343
 
 
344
 
 
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
393
392
""")
394
393
 
395
394
 
 
395
# This is made up
 
396
_invalid_response = (444, """HTTP/1.1 444 Bad Response\r
 
397
Date: Tue, 11 Jul 2006 04:32:56 GMT\r
 
398
Connection: close\r
 
399
Content-Type: text/html; charset=iso-8859-1\r
 
400
\r
 
401
""", """<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
 
402
<html><head>
 
403
<title>404 Not Found</title>
 
404
</head><body>
 
405
<h1>Not Found</h1>
 
406
<p>I don't know what I'm doing</p>
 
407
<hr>
 
408
</body></html>
 
409
""")
 
410
 
 
411
 
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):
399
415
    
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]))
402
418
 
403
419
    def check_header(self, header, value):
404
420
        self.assertEqual(value, self.headers[header])
422
438
        self.use_response(_single_range_response)
423
439
 
424
440
        self.check_header('Content-Length', '100')
425
 
        self.check_header('Content-Range', 'bytes 0-99/93890')
 
441
        self.check_header('Content-Range', 'bytes 100-199/93890')
426
442
        self.check_header('Content-Type', 'text/plain; charset=UTF-8')
427
443
 
428
444
    def test_multi_range(self):
431
447
        self.check_header('Content-Length', '1534')
432
448
        self.check_header('Content-Type',
433
449
                          'multipart/byteranges; boundary=418470f848b63279b')
 
450
 
 
451
 
 
452
class TestHandleResponse(TestCase):
 
453
    
 
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]))
 
459
 
 
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())
 
464
 
 
465
    def test_missing_response(self):
 
466
        self.assertRaises(errors.NoSuchFile,
 
467
            self.get_response, _missing_response)
 
468
 
 
469
    def test_single_range(self):
 
470
        out = self.get_response(_single_range_response)
 
471
        self.assertIsInstance(out, response.HttpRangeResponse)
 
472
 
 
473
        self.assertRaises(errors.InvalidRange, out.read, 20)
 
474
 
 
475
        out.seek(100)
 
476
        self.assertEqual(_single_range_response[2], out.read(100))
 
477
 
 
478
    def test_multi_range(self):
 
479
        out = self.get_response(_multipart_range_response)
 
480
        self.assertIsInstance(out, response.HttpMultipartRangeResponse)
 
481
 
 
482
        # Just make sure we can read the right contents
 
483
        out.seek(0)
 
484
        out.read(255)
 
485
 
 
486
        out.seek(1000)
 
487
        out.read(1050)
 
488
 
 
489
    def test_invalid_response(self):
 
490
        self.assertRaises(errors.InvalidHttpResponse,
 
491
            self.get_response, _invalid_response)
 
492
 
 
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())
 
501
 
 
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]))
 
510
 
 
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]))
 
518
 
 
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]))