~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_http.py

Merge previous attempt into current trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
226
226
        self._thread = threading.Thread(target=self._accept_read_and_reply)
227
227
        self._thread.setDaemon(True)
228
228
        self._thread.start()
229
 
        self._ready.wait(5)
 
229
        if 'threads' in tests.selftest_debug_flags:
 
230
            print 'Thread started: %s' % (self._thread.ident,)
 
231
        self._ready.wait()
230
232
 
231
233
    def _accept_read_and_reply(self):
232
234
        self._sock.listen(1)
 
235
        self._sock.settimeout(5)
233
236
        self._ready.set()
234
 
        self._sock.settimeout(5)
235
237
        try:
236
238
            conn, address = self._sock.accept()
237
239
            # On win32, the accepted connection will be non-blocking to start
238
240
            # with because we're using settimeout.
239
241
            conn.setblocking(True)
240
 
            while not self.received_bytes.endswith(self._expect_body_tail):
241
 
                self.received_bytes += conn.recv(4096)
242
 
            conn.sendall('HTTP/1.1 200 OK\r\n')
 
242
            if self._expect_body_tail is not None:
 
243
                while not self.received_bytes.endswith(self._expect_body_tail):
 
244
                    self.received_bytes += conn.recv(4096)
 
245
                conn.sendall('HTTP/1.1 200 OK\r\n')
243
246
        except socket.timeout:
244
247
            # Make sure the client isn't stuck waiting for us to e.g. accept.
 
248
            pass
 
249
        try:
245
250
            self._sock.close()
246
251
        except socket.error:
247
252
            # The client may have already closed the socket.
248
253
            pass
249
254
 
 
255
    def connect_socket(self):
 
256
        err = socket.error('getaddrinfo returns an empty list')
 
257
        for res in socket.getaddrinfo(self.host, self.port):
 
258
            af, socktype, proto, canonname, sa = res
 
259
            sock = None
 
260
            try:
 
261
                sock = socket.socket(af, socktype, proto)
 
262
                sock.connect(sa)
 
263
                return sock
 
264
 
 
265
            except socket.error, err:
 
266
                # err is now the most recent error
 
267
                if sock is not None:
 
268
                    sock.close()
 
269
        raise err
 
270
 
250
271
    def stop_server(self):
251
272
        try:
252
 
            self._sock.close()
 
273
            # Issue a fake connection to wake up the server and allow it to
 
274
            # finish quickly
 
275
            fake_conn = self.connect_socket()
 
276
            fake_conn.close()
253
277
        except socket.error:
254
278
            # We might have already closed it.  We don't care.
255
279
            pass
256
280
        self.host = None
257
281
        self.port = None
 
282
        self._thread.join()
 
283
        if 'threads' in tests.selftest_debug_flags:
 
284
            print 'Thread  joined: %s' % (self._thread.ident,)
258
285
 
259
286
 
260
287
class TestAuthHeader(tests.TestCase):
323
350
        server = http_server.HttpServer()
324
351
        server.start_server()
325
352
        try:
326
 
            self.assertTrue(server._http_running)
 
353
            self.assertTrue(server._httpd is not None)
 
354
            self.assertTrue(server._httpd.serving is not None)
 
355
            self.assertTrue(server._httpd.serving.isSet())
327
356
        finally:
328
357
            server.stop_server()
329
 
        self.assertFalse(server._http_running)
330
358
 
331
359
    def test_create_http_server_one_zero(self):
332
360
        class RequestHandlerOneZero(http_server.TestingHTTPRequestHandler):
600
628
class WallRequestHandler(http_server.TestingHTTPRequestHandler):
601
629
    """Whatever request comes in, close the connection"""
602
630
 
603
 
    def handle_one_request(self):
 
631
    def _handle_one_request(self):
604
632
        """Handle a single HTTP request, by abruptly closing the connection"""
605
633
        self.close_connection = 1
606
634
 
1896
1924
    line.
1897
1925
    """
1898
1926
 
1899
 
    def handle_one_request(self):
 
1927
    def _handle_one_request(self):
1900
1928
        tcs = self.server.test_case_server
1901
1929
        requestline = self.rfile.readline()
1902
1930
        headers = self.MessageClass(self.rfile, 0)
2086
2114
'''
2087
2115
        t = self.get_transport()
2088
2116
        # We must send a single line of body bytes, see
2089
 
        # PredefinedRequestHandler.handle_one_request
 
2117
        # PredefinedRequestHandler._handle_one_request
2090
2118
        code, f = t._post('abc def end-of-body\n')
2091
2119
        self.assertEqual('lalala whatever as long as itsssss\n', f.read())
2092
2120
        self.assertActivitiesMatch()