diff options
author | Joel Rivera <rivera@joel.mx> | 2015-10-24 16:49:35 -0500 |
---|---|---|
committer | Joel Rivera <rivera@joel.mx> | 2015-10-24 16:49:35 -0500 |
commit | 76c3eb6806fed1be0d8ef7ccbaee92dd250e45ef (patch) | |
tree | 9509b69846bc3e6067cb948a6511f2f1f9e0b888 | |
parent | abbe9a259ed656d72b5c45c4ac7b8861f4e6e8bd (diff) | |
download | cherrypy-76c3eb6806fed1be0d8ef7ccbaee92dd250e45ef.tar.gz |
Add a basic test case for the unix socket support in cherrypy.
-rw-r--r-- | cherrypy/test/test_wsgi_unix_socket.py | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/cherrypy/test/test_wsgi_unix_socket.py b/cherrypy/test/test_wsgi_unix_socket.py new file mode 100644 index 00000000..0e98a4c5 --- /dev/null +++ b/cherrypy/test/test_wsgi_unix_socket.py @@ -0,0 +1,104 @@ +import os +import sys +import socket +import atexit + +import cherrypy +from cherrypy.test import helper +from cherrypy._cpcompat import HTTPConnection + + +USOCKET_PATH = os.path.join( + os.path.dirname(os.path.realpath(__file__)), + 'cp_test.sock' +) + + +class USocketHTTPConnection(HTTPConnection): + """ + HTTPConnection over a unix socket. + """ + + def __init__(self, path): + HTTPConnection.__init__(self, 'localhost') + self.path = path + + def __call__(self, *args, **kwargs): + """ + Catch-all method just to present itself as a constructor for the + HTTPConnection. + """ + return self + + def connect(self): + """ + Override the connect method and assign a unix socket as a transport. + """ + sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) + sock.connect(self.path) + self.sock = sock + atexit.register(lambda: os.remove(self.path)) + + +def skip_on_windows(method): + """ + Decorator to skip the method call if the test is executing on Windows. + """ + def wrapper(self): + if sys.platform == "win32": + return self.skip("No UNIX Socket support in Windows.") + else: + return method(self) + wrapper.__doc__ = method.__doc__ + wrapper.__name__ = method.__name__ + return wrapper + + + +class WSGI_UnixSocket_Test(helper.CPWebCase): + """ + Test basic behavior on a cherrypy wsgi server listening + on a unix socket. + + It exercises the config option `server.socket_file`. + """ + HTTP_CONN = USocketHTTPConnection(USOCKET_PATH) + + + @staticmethod + def setup_server(): + class Root(object): + + @cherrypy.expose + def index(self): + return "Test OK" + + @cherrypy.expose + def error(self): + raise Exception("Invalid page") + + config = { + 'server.socket_file': USOCKET_PATH + } + cherrypy.config.update(config) + cherrypy.tree.mount(Root()) + + def tearDown(self): + cherrypy.config.update({'server.socket_file': None}) + + @skip_on_windows + def test_simple_request(self): + self.getPage("/") + self.assertStatus("200 OK") + self.assertInBody("Test OK") + + @skip_on_windows + def test_not_found(self): + self.getPage("/invalid_path") + self.assertStatus("404 Not Found") + + @skip_on_windows + def test_internal_error(self): + self.getPage("/error") + self.assertStatus("500 Internal Server Error") + self.assertInBody("Invalid page") |