diff options
Diffstat (limited to 'dummyserver')
-rw-r--r-- | dummyserver/handlers.py | 11 | ||||
-rwxr-xr-x | dummyserver/server.py | 33 | ||||
-rw-r--r-- | dummyserver/testcase.py | 14 |
3 files changed, 55 insertions, 3 deletions
diff --git a/dummyserver/handlers.py b/dummyserver/handlers.py index 53fbe4a..ffa1dd3 100644 --- a/dummyserver/handlers.py +++ b/dummyserver/handlers.py @@ -73,6 +73,10 @@ class TestingApp(RequestHandler): """ Handle OPTIONS requests """ self._call_method() + def head(self): + """ Handle HEAD requests """ + self._call_method() + def _call_method(self): """ Call the correct method in this class based on the incoming URI """ req = self.request @@ -232,6 +236,13 @@ class TestingApp(RequestHandler): return Response(chunks, headers=[('Content-Encoding', 'gzip')]) + def nbytes(self, request): + length = int(request.params.get('length')) + data = b'1' * length + return Response( + data, + headers=[('Content-Type', 'application/octet-stream')]) + def shutdown(self, request): sys.exit() diff --git a/dummyserver/server.py b/dummyserver/server.py index 63124d3..1999474 100755 --- a/dummyserver/server.py +++ b/dummyserver/server.py @@ -38,6 +38,35 @@ DEFAULT_CA = os.path.join(CERTS_PATH, 'cacert.pem') DEFAULT_CA_BAD = os.path.join(CERTS_PATH, 'client_bad.pem') NO_SAN_CA = os.path.join(CERTS_PATH, 'cacert.no_san.pem') +def _has_ipv6(host): + """ Returns True if the system can bind an IPv6 address. """ + sock = None + has_ipv6 = False + + if socket.has_ipv6: + # has_ipv6 returns true if cPython was compiled with IPv6 support. + # It does not tell us if the system has IPv6 support enabled. To + # determine that we must bind to an IPv6 address. + # https://github.com/shazow/urllib3/pull/611 + # https://bugs.python.org/issue658327 + try: + sock = socket.socket(socket.AF_INET6) + sock.bind((host, 0)) + has_ipv6 = True + except: + pass + + if sock: + sock.close() + return has_ipv6 + +# Some systems may have IPv6 support but DNS may not be configured +# properly. We can not count that localhost will resolve to ::1 on all +# systems. See https://github.com/shazow/urllib3/pull/611 and +# https://bugs.python.org/issue18792 +HAS_IPV6_AND_DNS = _has_ipv6('localhost') +HAS_IPV6 = _has_ipv6('::1') + # Different types of servers we have: @@ -64,7 +93,7 @@ class SocketServerThread(threading.Thread): self.ready_event = ready_event def _start_server(self): - if socket.has_ipv6: + if HAS_IPV6_AND_DNS: sock = socket.socket(socket.AF_INET6) else: warnings.warn("No IPv6 support. Falling back to IPv4.", @@ -117,7 +146,7 @@ def bind_sockets(port, address=None, family=socket.AF_UNSPEC, backlog=128, sockets = [] if address == "": address = None - if not socket.has_ipv6 and family == socket.AF_UNSPEC: + if not HAS_IPV6 and family == socket.AF_UNSPEC: # Python can be compiled with --disable-ipv6, which causes # operations on AF_INET6 sockets to fail, but does not # automatically exclude those results from getaddrinfo diff --git a/dummyserver/testcase.py b/dummyserver/testcase.py index 67e62cf..de6aedd 100644 --- a/dummyserver/testcase.py +++ b/dummyserver/testcase.py @@ -14,7 +14,6 @@ from dummyserver.handlers import TestingApp from dummyserver.proxy import ProxyHandler - class SocketDummyServerTestCase(unittest.TestCase): """ A simple socket-based server is created for this class that is good for @@ -131,3 +130,16 @@ class IPv6HTTPDummyServerTestCase(HTTPDummyServerTestCase): raise SkipTest('IPv6 not available') else: super(IPv6HTTPDummyServerTestCase, cls).setUpClass() + + +class IPv6HTTPDummyProxyTestCase(HTTPDummyProxyTestCase): + + http_host = 'localhost' + http_host_alt = '127.0.0.1' + + https_host = 'localhost' + https_host_alt = '127.0.0.1' + https_certs = DEFAULT_CERTS + + proxy_host = '::1' + proxy_host_alt = '127.0.0.1' |