1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
1 |
# Copyright (C) 2005 by Canonical Ltd
|
2 |
||
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
||
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
||
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
1185.11.15
by John Arbash Meinel
Got HttpTransport tests to pass. Check for EAGAIN, pass permit_failure around, etc |
17 |
import BaseHTTPServer, SimpleHTTPServer, socket, errno, time |
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
18 |
from bzrlib.selftest import TestCaseInTempDir |
19 |
||
20 |
||
21 |
class WebserverNotAvailable(Exception): |
|
22 |
pass
|
|
23 |
||
24 |
class BadWebserverPath(ValueError): |
|
25 |
def __str__(self): |
|
26 |
return 'path %s is not in %s' % self.args |
|
27 |
||
28 |
class TestingHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): |
|
29 |
def log_message(self, format, *args): |
|
1185.11.12
by John Arbash Meinel
Remove spurious newline from logging code. |
30 |
self.server.test_case.log("webserver - %s - - [%s] %s" % |
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
31 |
(self.address_string(), |
32 |
self.log_date_time_string(), |
|
33 |
format%args)) |
|
34 |
||
1185.11.15
by John Arbash Meinel
Got HttpTransport tests to pass. Check for EAGAIN, pass permit_failure around, etc |
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 |
"""
|
|
1185.11.24
by John Arbash Meinel
Made it so that we don't loop forever on EAGAIN. |
43 |
for i in xrange(1,11): # Don't try more than 10 times |
1185.11.15
by John Arbash Meinel
Got HttpTransport tests to pass. Check for EAGAIN, pass permit_failure around, etc |
44 |
try: |
45 |
self.raw_requestline = self.rfile.readline() |
|
46 |
except socket.error, e: |
|
1185.38.6
by John Arbash Meinel
Applying fix for EWOULDBLOCK under win32 |
47 |
if e.args[0] in (errno.EAGAIN, errno.EWOULDBLOCK): |
1185.16.89
by mbp at sourcefrog
Fix http server tests for win32/macos (John) |
48 |
# omitted for now because some tests look at the log of
|
49 |
# the server and expect to see no errors. see recent
|
|
50 |
# email thread. -- mbp 20051021.
|
|
51 |
## self.log_message('EAGAIN (%d) while reading from raw_requestline' % i)
|
|
1185.11.15
by John Arbash Meinel
Got HttpTransport tests to pass. Check for EAGAIN, pass permit_failure around, etc |
52 |
time.sleep(0.01) |
53 |
continue
|
|
54 |
raise
|
|
55 |
else: |
|
56 |
break
|
|
57 |
if not self.raw_requestline: |
|
58 |
self.close_connection = 1 |
|
59 |
return
|
|
60 |
if not self.parse_request(): # An error code has been sent, just exit |
|
61 |
return
|
|
62 |
mname = 'do_' + self.command |
|
63 |
if not hasattr(self, mname): |
|
64 |
self.send_error(501, "Unsupported method (%r)" % self.command) |
|
65 |
return
|
|
66 |
method = getattr(self, mname) |
|
67 |
method() |
|
68 |
||
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
69 |
class TestingHTTPServer(BaseHTTPServer.HTTPServer): |
70 |
def __init__(self, server_address, RequestHandlerClass, test_case): |
|
71 |
BaseHTTPServer.HTTPServer.__init__(self, server_address, |
|
72 |
RequestHandlerClass) |
|
73 |
self.test_case = test_case |
|
74 |
||
1185.11.15
by John Arbash Meinel
Got HttpTransport tests to pass. Check for EAGAIN, pass permit_failure around, etc |
75 |
|
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
76 |
class TestCaseWithWebserver(TestCaseInTempDir): |
77 |
"""Derived class that starts a localhost-only webserver
|
|
78 |
(in addition to what TestCaseInTempDir does).
|
|
79 |
||
80 |
This is useful for testing RemoteBranch.
|
|
81 |
"""
|
|
82 |
||
83 |
_HTTP_PORTS = range(13000, 0x8000) |
|
84 |
||
85 |
def _http_start(self): |
|
86 |
import SimpleHTTPServer, BaseHTTPServer, socket, errno |
|
87 |
httpd = None |
|
88 |
for port in self._HTTP_PORTS: |
|
89 |
try: |
|
90 |
httpd = TestingHTTPServer(('localhost', port), |
|
91 |
TestingHTTPRequestHandler, |
|
92 |
self) |
|
93 |
except socket.error, e: |
|
94 |
if e.args[0] == errno.EADDRINUSE: |
|
95 |
continue
|
|
96 |
print >>sys.stderr, "Cannot run webserver :-(" |
|
97 |
raise
|
|
98 |
else: |
|
99 |
break
|
|
100 |
||
101 |
if httpd is None: |
|
102 |
raise WebserverNotAvailable("Cannot run webserver :-( " |
|
103 |
"no free ports in range %s..%s" % |
|
104 |
(_HTTP_PORTS[0], _HTTP_PORTS[-1])) |
|
105 |
||
106 |
self._http_base_url = 'http://localhost:%s/' % port |
|
107 |
self._http_starting.release() |
|
1185.11.23
by John Arbash Meinel
Changed test timeout to 0.1s to make tests finish much faster. |
108 |
httpd.socket.settimeout(0.1) |
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
109 |
|
110 |
while self._http_running: |
|
111 |
try: |
|
112 |
httpd.handle_request() |
|
113 |
except socket.timeout: |
|
114 |
pass
|
|
115 |
||
116 |
def get_remote_url(self, path): |
|
117 |
import os |
|
118 |
||
119 |
path_parts = path.split(os.path.sep) |
|
120 |
if os.path.isabs(path): |
|
121 |
if path_parts[:len(self._local_path_parts)] != \ |
|
122 |
self._local_path_parts: |
|
123 |
raise BadWebserverPath(path, self.test_dir) |
|
124 |
remote_path = '/'.join(path_parts[len(self._local_path_parts):]) |
|
125 |
else: |
|
126 |
remote_path = '/'.join(path_parts) |
|
127 |
||
128 |
self._http_starting.acquire() |
|
129 |
self._http_starting.release() |
|
130 |
return self._http_base_url + remote_path |
|
131 |
||
132 |
def setUp(self): |
|
1185.11.14
by John Arbash Meinel
Working on getting tests to run. TestFetch only works if named runTest |
133 |
TestCaseInTempDir.setUp(self) |
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
134 |
import threading, os |
135 |
self._local_path_parts = self.test_dir.split(os.path.sep) |
|
136 |
self._http_starting = threading.Lock() |
|
137 |
self._http_starting.acquire() |
|
138 |
self._http_running = True |
|
139 |
self._http_base_url = None |
|
140 |
self._http_thread = threading.Thread(target=self._http_start) |
|
141 |
self._http_thread.setDaemon(True) |
|
142 |
self._http_thread.start() |
|
1426
by Robert Collins
merge gustavos http_proxy fix for test cases. |
143 |
self._http_proxy = os.environ.get("http_proxy") |
144 |
if self._http_proxy is not None: |
|
145 |
del os.environ["http_proxy"] |
|
1185.1.18
by Robert Collins
Lalo Martins remotebranch patch |
146 |
|
147 |
def tearDown(self): |
|
148 |
self._http_running = False |
|
149 |
self._http_thread.join() |
|
1426
by Robert Collins
merge gustavos http_proxy fix for test cases. |
150 |
if self._http_proxy is not None: |
151 |
import os |
|
152 |
os.environ["http_proxy"] = self._http_proxy |
|
1185.11.14
by John Arbash Meinel
Working on getting tests to run. TestFetch only works if named runTest |
153 |
TestCaseInTempDir.tearDown(self) |
154 |