163
152
self.zopeless_helper.tearDown()
166
# Webserver helper was based on the bzr code for doing http tests.
168
class BadWebserverPath(ValueError):
170
return 'path %s is not in %s' % self.args
172
class TestHTTPRequestHandler(SimpleHTTPRequestHandler):
173
def log_message(self, *args):
177
class WebserverHelper(SandboxHelper):
179
def _http_start(self):
180
httpd = HTTPServer(('localhost', 0), TestHTTPRequestHandler)
181
host, port = httpd.socket.getsockname()
183
self._http_base_url = 'http://localhost:%s/' % port
184
self._http_starting.release()
185
httpd.socket.settimeout(0.1)
187
while self._http_running:
189
httpd.handle_request()
190
except socket.timeout:
193
def get_remote_url(self, path):
196
path_parts = path.split(os.path.sep)
197
if os.path.isabs(path):
198
if path_parts[:len(self._local_path_parts)] != \
199
self._local_path_parts:
200
raise BadWebserverPath(path, self.path)
201
remote_path = '/'.join(path_parts[len(self._local_path_parts):])
203
remote_path = '/'.join(path_parts)
205
self._http_starting.acquire()
206
self._http_starting.release()
207
return self._http_base_url + remote_path
210
SandboxHelper.setUp(self)
212
self._local_path_parts = self.path.split(os.path.sep)
213
self._http_starting = threading.Lock()
214
self._http_starting.acquire()
215
self._http_running = True
216
self._http_base_url = None
217
self._http_thread = threading.Thread(target=self._http_start)
218
self._http_thread.setDaemon(True)
219
self._http_thread.start()
220
self._http_proxy = os.environ.get("http_proxy")
221
if self._http_proxy is not None:
222
del os.environ["http_proxy"]
225
self._http_running = False
226
self._http_thread.join()
227
if self._http_proxy is not None:
229
os.environ["http_proxy"] = self._http_proxy
230
SandboxHelper.tearDown(self)