~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

  • Committer: Vincent Ladeuil
  • Date: 2007-12-21 21:58:06 UTC
  • mto: (3146.3.1 179368) (3156.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 3158.
  • Revision ID: v.ladeuil+lp@free.fr-20071221215806-c2kdsnqsi3tvqjr4
Make all the test pass. Looks like we are HTTP/1.1 compliant.

* bzrlib/transport/http/_urllib2_wrappers.py:
Review the debug prints and layered them.
(AbstractAuthHandler.auth_required): Clean up the http pipe before
issuing the new request after a 401 or 407 auth required error.

* bzrlib/tests/test_http.py:
Fix the remaining imports.
(BadStatusRequestHandler.parse_request): Simplified. Close
connection.
(TestInvalidStatusServer.test_http_has,
TestInvalidStatusServer.test_http_get): Document pycurl
limitations.

* bzrlib/tests/http_utils.py:
(RedirectRequestHandler.parse_request, AuthRequestHandler.do_GET):
Add a Content-Length header.

* bzrlib/tests/http_server.py:
(TestingHTTPRequestHandler.get_multiple_ranges): Close the
connection since we didnt specify a Content-Length header.

* bzrlib/tests/http_utils.py:
(RedirectRequestHandler.parse_request): Add a Content-Length
header.

* bzrlib/tests/http_server.py:
(TestingHTTPRequestHandler.handle_one_request): Any socket error
close the connection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import httplib
28
28
import os
29
29
import select
 
30
import SimpleHTTPServer
30
31
import socket
31
32
import sys
32
33
import threading
45
46
    http_server,
46
47
    http_utils,
47
48
    )
48
 
 
 
49
from bzrlib.transport import http
49
50
from bzrlib.transport.http import (
50
 
    extract_auth,
51
51
    _urllib,
52
52
    _urllib2_wrappers,
53
53
    )
259
259
 
260
260
    def test_url_parsing(self):
261
261
        f = FakeManager()
262
 
        url = extract_auth('http://example.com', f)
 
262
        url = http.extract_auth('http://example.com', f)
263
263
        self.assertEquals('http://example.com', url)
264
264
        self.assertEquals(0, len(f.credentials))
265
 
        url = extract_auth('http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
 
265
        url = http.extract_auth('http://user:pass@www.bazaar-vcs.org/bzr/bzr.dev', f)
266
266
        self.assertEquals('http://www.bazaar-vcs.org/bzr/bzr.dev', url)
267
267
        self.assertEquals(1, len(f.credentials))
268
268
        self.assertEquals([None, 'www.bazaar-vcs.org', 'user', 'pass'],
442
442
        offsets = [ (start, end - start + 1) for start, end in ranges]
443
443
        coalesce = transport.Transport._coalesce_offsets
444
444
        coalesced = list(coalesce(offsets, limit=0, fudge_factor=0))
445
 
        range_header = HttpTransportBase._range_header
 
445
        range_header = http.HttpTransportBase._range_header
446
446
        self.assertEqual(value, range_header(coalesced, tail))
447
447
 
448
448
    def test_range_header_single(self):
477
477
        return http_server.HttpServer(self._req_handler_class,
478
478
                                      protocol_version=self._protocol_version)
479
479
 
 
480
    def _testing_pycurl(self):
 
481
        return pycurl_present and self._transport == PyCurlTransport
 
482
 
480
483
 
481
484
class WallRequestHandler(http_server.TestingHTTPRequestHandler):
482
485
    """Whatever request comes in, close the connection"""
515
518
    def parse_request(self):
516
519
        """Fakes handling a single HTTP request, returns a bad status"""
517
520
        ignored = http_server.TestingHTTPRequestHandler.parse_request(self)
518
 
        try:
519
 
            self.send_response(0, "Bad status")
520
 
            self.end_headers()
521
 
        except socket.error, e:
522
 
            # We don't want to pollute the test results with
523
 
            # spurious server errors while test succeed. In our
524
 
            # case, it may occur that the test has already read
525
 
            # the 'Bad Status' and closed the socket while we are
526
 
            # still trying to send some headers... So the test is
527
 
            # ok, but if we raise the exception, the output is
528
 
            # dirty. So we don't raise, but we close the
529
 
            # connection, just to be safe :)
530
 
            spurious = [errno.EPIPE,
531
 
                        errno.ECONNRESET,
532
 
                        errno.ECONNABORTED,
533
 
                        ]
534
 
            if (len(e.args) > 0) and (e.args[0] in spurious):
535
 
                self.close_connection = 1
536
 
                pass
537
 
            else:
538
 
                raise
 
521
        self.send_response(0, "Bad status")
 
522
        self.close_connection = 1
539
523
        return False
540
524
 
541
525
 
556
540
 
557
541
 
558
542
class InvalidStatusRequestHandler(http_server.TestingHTTPRequestHandler):
559
 
    """Whatever request comes in, returns am invalid status"""
 
543
    """Whatever request comes in, returns an invalid status"""
560
544
 
561
545
    def parse_request(self):
562
546
        """Fakes handling a single HTTP request, returns a bad status"""
573
557
 
574
558
    _req_handler_class = InvalidStatusRequestHandler
575
559
 
 
560
    def test_http_has(self):
 
561
        if self._testing_pycurl() and self._protocol_version == 'HTTP/1.1':
 
562
            raise tests.KnownFailure('pycurl hangs if the server send back garbage')
 
563
        super(TestInvalidStatusServer, self).test_http_has()
 
564
 
 
565
    def test_http_get(self):
 
566
        if self._testing_pycurl() and self._protocol_version == 'HTTP/1.1':
 
567
            raise tests.KnownFailure('pycurl hangs if the server send back garbage')
 
568
        super(TestInvalidStatusServer, self).test_http_get()
 
569
 
576
570
 
577
571
class BadProtocolRequestHandler(http_server.TestingHTTPRequestHandler):
578
572
    """Whatever request comes in, returns a bad protocol version"""
880
874
            osutils.set_or_unset_env(name, value)
881
875
 
882
876
    def _proxied_request(self):
883
 
        handler = ProxyHandler()
884
 
        request = Request('GET','http://baz/buzzle')
 
877
        handler = _urllib2_wrappers.ProxyHandler()
 
878
        request = _urllib2_wrappers.Request('GET','http://baz/buzzle')
885
879
        handler.set_proxy(request, 'http')
886
880
        return request
887
881
 
1170
1164
                                       self.old_server.port)
1171
1165
        new_prefix = 'http://%s:%s' % (self.new_server.host,
1172
1166
                                       self.new_server.port)
1173
 
        self.old_server.redirections = \
1174
 
            [('/1(.*)', r'%s/2\1' % (old_prefix), 302),
1175
 
             ('/2(.*)', r'%s/3\1' % (old_prefix), 303),
1176
 
             ('/3(.*)', r'%s/4\1' % (old_prefix), 307),
1177
 
             ('/4(.*)', r'%s/5\1' % (new_prefix), 301),
1178
 
             ('(/[^/]+)', r'%s/1\1' % (old_prefix), 301),
1179
 
             ]
 
1167
        self.old_server.redirections = [
 
1168
            ('/1(.*)', r'%s/2\1' % (old_prefix), 302),
 
1169
            ('/2(.*)', r'%s/3\1' % (old_prefix), 303),
 
1170
            ('/3(.*)', r'%s/4\1' % (old_prefix), 307),
 
1171
            ('/4(.*)', r'%s/5\1' % (new_prefix), 301),
 
1172
            ('(/[^/]+)', r'%s/1\1' % (old_prefix), 301),
 
1173
            ]
1180
1174
        self.assertEquals('redirected 5 times',t._perform(req).read())
1181
1175
 
1182
1176