Python:如何对自定义HTTP请求处理器进行单元测试?

14 投票
3 回答
8306 浏览
提问于 2025-04-18 17:41

我有一个自定义的HTTP请求处理器,可以简单地理解成下面这样的代码:

# Python 3:
from http import server

class MyHandler(server.BaseHTTPRequestHandler):
    def do_GET(self):
        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        # Here's where all the complicated logic is done to generate HTML.
        # For clarity here, replace with a simple stand-in:
        html = "<html><p>hello world</p></html>"

        self.wfile.write(html.encode())

我想对这个处理器进行单元测试,也就是确保我的 do_GET 方法能正常执行,没有任何错误,而不需要真正启动一个网络服务器。有没有什么简单的方法可以模拟 SimpleHTTPServer,让我可以测试这段代码呢?

3 个回答

2

这件事有点复杂,主要看你想多深入了解BaseHTTPRequestHandler的行为,以便定义你的单元测试。最基本的来说,我觉得你可以参考一下mock库中的这个例子

>>> from mock import MagicMock
>>> thing = ProductionClass()
>>> thing.method = MagicMock(return_value=3)
>>> thing.method(3, 4, 5, key='value')
3
>>> thing.method.assert_called_with(3, 4, 5, key='value')

如果你知道你的类会调用BaseHTTPRequestHandler中的哪些方法,你就可以模拟这些方法的返回结果,让它们变得合适。这当然会变得相当复杂,具体取决于你想测试多少种不同类型的服务器响应。

6

在jakevdp的回答基础上,我也成功地检查了输出:

try:
    import unittest2 as unittest
except ImportError:
    import unittest
try:
    from io import BytesIO as IO
except ImportError:
    from StringIO import StringIO as IO
from server import MyHandlerSSL  # My BaseHTTPRequestHandler child


class TestableHandler(MyHandlerSSL):
    # On Python3, in socketserver.StreamRequestHandler, if this is
    # set it will use makefile() to produce the output stream. Otherwise,
    # it will use socketserver._SocketWriter, and we won't be able to get
    # to the data
    wbufsize = 1

    def finish(self):
        # Do not close self.wfile, so we can read its value
        self.wfile.flush()
        self.rfile.close()

    def date_time_string(self, timestamp=None):
        """ Mocked date time string """
        return 'DATETIME'

    def version_string(self):
        """ mock the server id """
        return 'BaseHTTP/x.x Python/x.x.x'


class MockSocket(object):
    def getsockname(self):
        return ('sockname',)


class MockRequest(object):
    _sock = MockSocket()

    def __init__(self, path):
        self._path = path

    def makefile(self, *args, **kwargs):
        if args[0] == 'rb':
            return IO(b"GET %s HTTP/1.0" % self._path)
        elif args[0] == 'wb':
            return IO(b'')
        else:
            raise ValueError("Unknown file type to make", args, kwargs)


class HTTPRequestHandlerTestCase(unittest.TestCase):
    maxDiff = None

    def _test(self, request):
        handler = TestableHandler(request, (0, 0), None)
        return handler.wfile.getvalue()

    def test_unauthenticated(self):
        self.assertEqual(
                self._test(MockRequest(b'/')),
                b"""HTTP/1.0 401 Unauthorized\r
Server: BaseHTTP/x.x Python/x.x.x\r
Date: DATETIME\r
WWW-Authenticate: Basic realm="MyRealm", charset="UTF-8"\r
Content-type: text/html\r
\r
<html><head><title>Authentication Failed</title></html><body><h1>Authentication Failed</h1><p>Authentication Failed. Authorised Personnel Only.</p></body></html>"""
                )


def main():
    unittest.main()


if __name__ == "__main__":
    main()

我正在测试的代码对“/”这个请求返回了401未授权的错误。你可以根据自己的测试情况,修改这个响应。

4

这是我想到的一种模拟服务器的方法。请注意,这种方法在Python 2和Python 3中都可以使用。唯一的问题是我找不到获取GET请求结果的方法,不过至少测试可以捕捉到遇到的任何异常!

try:
    # Python 2.x
    import BaseHTTPServer as server
    from StringIO import StringIO as IO
except ImportError:
    # Python 3.x
    from http import server
    from io import BytesIO as IO


class MyHandler(server.BaseHTTPRequestHandler):
    """Custom handler to be tested"""
    def do_GET(self):
        # print just to confirm that this method is being called
        print("executing do_GET") # just to confirm...

        self.send_response(200)
        self.send_header("Content-type", "text/html")
        self.end_headers()

        # Here's where all the complicated logic is done to generate HTML.
        # For clarity here, replace with a simple stand-in:
        html = "<html><p>hello world</p></html>"

        self.wfile.write(html.encode())


def test_handler():
    """Test the custom HTTP request handler by mocking a server"""
    class MockRequest(object):
        def makefile(self, *args, **kwargs):
            return IO(b"GET /")

    class MockServer(object):
        def __init__(self, ip_port, Handler):
            handler = Handler(MockRequest(), ip_port, self)

    # The GET request will be sent here
    # and any exceptions will be propagated through.
    server = MockServer(('0.0.0.0', 8888), MyHandler)


test_handler()

撰写回答