~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/selftest/HTTPTestUtil.py

Change the use of run_bzr to run_bzr_captured in blackbox tests - inspired by David Clymers patch to change run_bzr usage to runbzr

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# along with this program; if not, write to the Free Software
15
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16
16
 
17
 
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time
 
17
import BaseHTTPServer, SimpleHTTPServer
18
18
from bzrlib.selftest import TestCaseInTempDir
19
19
 
20
20
 
27
27
 
28
28
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
29
29
    def log_message(self, format, *args):
30
 
        self.server.test_case.log("webserver - %s - - [%s] %s" %
 
30
        self.server.test_case.log("webserver - %s - - [%s] %s\n" %
31
31
                                  (self.address_string(),
32
32
                                   self.log_date_time_string(),
33
33
                                   format%args))
34
34
 
35
 
    def handle_one_request(self):
36
 
        """Handle a single HTTP request.
37
 
 
38
 
        You normally don't need to override this method; see the class
39
 
        __doc__ string for information on how to handle specific HTTP
40
 
        commands such as GET and POST.
41
 
 
42
 
        """
43
 
        for i in xrange(1,11): # Don't try more than 10 times
44
 
            try:
45
 
                self.raw_requestline = self.rfile.readline()
46
 
            except socket.error, e:
47
 
                if e.args[0] == errno.EAGAIN:
48
 
                    self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
49
 
                    time.sleep(0.01)
50
 
                    continue
51
 
                raise
52
 
            else:
53
 
                break
54
 
        if not self.raw_requestline:
55
 
            self.close_connection = 1
56
 
            return
57
 
        if not self.parse_request(): # An error code has been sent, just exit
58
 
            return
59
 
        mname = 'do_' + self.command
60
 
        if not hasattr(self, mname):
61
 
            self.send_error(501, "Unsupported method (%r)" % self.command)
62
 
            return
63
 
        method = getattr(self, mname)
64
 
        method()
65
 
 
66
35
class TestingHTTPServer(BaseHTTPServer.HTTPServer):
67
36
    def __init__(self, server_address, RequestHandlerClass, test_case):
68
37
        BaseHTTPServer.HTTPServer.__init__(self, server_address,
69
38
                                                RequestHandlerClass)
70
39
        self.test_case = test_case
71
40
 
72
 
 
73
41
class TestCaseWithWebserver(TestCaseInTempDir):
74
42
    """Derived class that starts a localhost-only webserver
75
43
    (in addition to what TestCaseInTempDir does).
102
70
 
103
71
        self._http_base_url = 'http://localhost:%s/' % port
104
72
        self._http_starting.release()
105
 
        httpd.socket.settimeout(0.1)
 
73
        httpd.socket.settimeout(1)
106
74
 
107
75
        while self._http_running:
108
76
            try:
127
95
        return self._http_base_url + remote_path
128
96
 
129
97
    def setUp(self):
130
 
        TestCaseInTempDir.setUp(self)
 
98
        super(TestCaseWithWebserver, self).setUp()
131
99
        import threading, os
132
100
        self._local_path_parts = self.test_dir.split(os.path.sep)
133
101
        self._http_starting = threading.Lock()
137
105
        self._http_thread = threading.Thread(target=self._http_start)
138
106
        self._http_thread.setDaemon(True)
139
107
        self._http_thread.start()
140
 
        self._http_proxy = os.environ.get("http_proxy")
141
 
        if self._http_proxy is not None:
142
 
            del os.environ["http_proxy"]
143
108
 
144
109
    def tearDown(self):
145
110
        self._http_running = False
146
111
        self._http_thread.join()
147
 
        if self._http_proxy is not None:
148
 
            import os
149
 
            os.environ["http_proxy"] = self._http_proxy
150
 
        TestCaseInTempDir.tearDown(self)
151
 
 
 
112
        super(TestCaseWithWebserver, self).tearDown()