~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/http_utils.py

  • Committer: Vincent Ladeuil
  • Date: 2007-12-28 15:33:47 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-20071228153347-k84pqeepvol23tj5
Cancel RecordingServer move, that was useless.

Show diffs side-by-side

added added

removed removed

Lines of Context:
443
443
        self.init_proxy_auth()
444
444
 
445
445
 
446
 
class RecordingServer(object):
447
 
    """A fake HTTP server.
448
 
    
449
 
    It records the bytes sent to it, and replies with a 200.
450
 
    """
451
 
 
452
 
    def __init__(self, expect_body_tail=None):
453
 
        """Constructor.
454
 
 
455
 
        :type expect_body_tail: str
456
 
        :param expect_body_tail: a reply won't be sent until this string is
457
 
            received.
458
 
        """
459
 
        self._expect_body_tail = expect_body_tail
460
 
        self.host = None
461
 
        self.port = None
462
 
        self.received_bytes = ''
463
 
 
464
 
    def setUp(self):
465
 
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
466
 
        self._sock.bind(('127.0.0.1', 0))
467
 
        self.host, self.port = self._sock.getsockname()
468
 
        self._ready = threading.Event()
469
 
        self._thread = threading.Thread(target=self._accept_read_and_reply)
470
 
        self._thread.setDaemon(True)
471
 
        self._thread.start()
472
 
        self._ready.wait(5)
473
 
 
474
 
    def _accept_read_and_reply(self):
475
 
        self._sock.listen(1)
476
 
        self._ready.set()
477
 
        self._sock.settimeout(5)
478
 
        try:
479
 
            conn, address = self._sock.accept()
480
 
            # On win32, the accepted connection will be non-blocking to start
481
 
            # with because we're using settimeout.
482
 
            conn.setblocking(True)
483
 
            while not self.received_bytes.endswith(self._expect_body_tail):
484
 
                self.received_bytes += conn.recv(4096)
485
 
            conn.sendall('HTTP/1.1 200 OK\r\n')
486
 
        except socket.timeout:
487
 
            # Make sure the client isn't stuck waiting for us to e.g. accept.
488
 
            self._sock.close()
489
 
        except socket.error:
490
 
            # The client may have already closed the socket.
491
 
            pass
492
 
 
493
 
    def tearDown(self):
494
 
        try:
495
 
            self._sock.close()
496
 
        except socket.error:
497
 
            # We might have already closed it.  We don't care.
498
 
            pass
499
 
        self.host = None
500
 
        self.port = None
501
 
 
502