~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http_response.py

  • Committer: Martin Pool
  • Date: 2007-12-14 07:35:49 UTC
  • mfrom: (3109 +trunk)
  • mto: This revision was merged to the branch mainline in revision 3111.
  • Revision ID: mbp@sourcefrog.net-20071214073549-wpekccrsxjv77yze
Merge 1.0final back to trunk and bump to 1.1dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
44
44
    errors,
45
45
    tests,
46
46
    )
47
 
from bzrlib.transport.http import response
 
47
from bzrlib.transport.http import (
 
48
    response,
 
49
    _urllib2_wrappers,
 
50
    )
 
51
 
 
52
 
 
53
class ReadSocket(object):
 
54
    """A socket-like object that can be given a predefined content."""
 
55
 
 
56
    def __init__(self, data):
 
57
        self.readfile = StringIO(data)
 
58
 
 
59
    def makefile(self, mode='r', bufsize=None):
 
60
        return self.readfile
 
61
 
 
62
class FakeHTTPConnection(_urllib2_wrappers.HTTPConnection):
 
63
 
 
64
    def __init__(self, sock):
 
65
        _urllib2_wrappers.HTTPConnection.__init__(self, 'localhost')
 
66
        # Set the socket to bypass the connection
 
67
        self.sock = sock
 
68
 
 
69
    def send(self, str):
 
70
        """Ignores the writes on the socket."""
 
71
        pass
 
72
 
 
73
 
 
74
class TestHTTPConnection(tests.TestCase):
 
75
 
 
76
    def test_cleanup_pipe(self):
 
77
        sock = ReadSocket("""HTTP/1.1 200 OK\r
 
78
Content-Type: text/plain; charset=UTF-8\r
 
79
Content-Length: 18
 
80
\r
 
81
0123456789
 
82
garbage""")
 
83
        conn = FakeHTTPConnection(sock)
 
84
        # Simulate the request sending so that the connection will be able to
 
85
        # read the response.
 
86
        conn.putrequest('GET', 'http://localhost/fictious')
 
87
        conn.endheaders()
 
88
        # Now, get the response
 
89
        resp = conn.getresponse()
 
90
        # Read part of the response
 
91
        self.assertEquals('0123456789\n', resp.read(11))
 
92
        # Override the thresold to force the warning emission
 
93
        conn._range_warning_thresold = 6 # There are 7 bytes pending
 
94
        conn.cleanup_pipe()
 
95
        self.assertContainsRe(self._get_log(keep_log_file=True),
 
96
                              'Got a 200 response when asking')
48
97
 
49
98
 
50
99
class TestRangeFileMixin(object):