如何在龙卷风中使用图像数据模拟HTTPRESPONDE?

发布于 2025-01-30 01:21:42 字数 1110 浏览 3 评论 0原文

问题

我正在测试龙卷风 Web应用程序的处理程序。这是我的测试案例:

class HandlerTestCase(tornado_openapi3.AsyncOpenAPITestCase):

    def get_app(self) -> Application:
        return make_some_app()

    @mock.patch.object(Client, 'get')
    def test_some_handler(self, mock_get):
        mock_get.return_value = HTTPResponse(request=HTTPRequest(url='some_url'), code=200,
                                             buffer=io.TextIOWrapper(io.BytesIO(b'test_buffer')),
                                             headers=HTTPHeaders({'Content-Type': 'image/*'}))
        response = self.fetch(<some_path>, raise_error=False)
        self.assertEquals(response.code, 200)

即使我明确将响应的mimeType image/*设置为,我也会收到以下错误消息:

openapi_core.templating.media_types.exceptions.MediaTypeNotFound: Content for the following mimetype not found: application/json. Valid mimetypes: ['image/*']

有效的mimeTypes在我的OpenAPI Schema中指定,但我不明白为什么响应的模拟型似乎是application/json

解决方案

响应标头未写入我的测试代码中的响应中。我改编了。我想这是测试驱动开发的一个很好的例子。

Problem

I am testing a handler for a tornado web application. This is my test case:

class HandlerTestCase(tornado_openapi3.AsyncOpenAPITestCase):

    def get_app(self) -> Application:
        return make_some_app()

    @mock.patch.object(Client, 'get')
    def test_some_handler(self, mock_get):
        mock_get.return_value = HTTPResponse(request=HTTPRequest(url='some_url'), code=200,
                                             buffer=io.TextIOWrapper(io.BytesIO(b'test_buffer')),
                                             headers=HTTPHeaders({'Content-Type': 'image/*'}))
        response = self.fetch(<some_path>, raise_error=False)
        self.assertEquals(response.code, 200)

Even though I explicitly set the response's mimetype to image/*, I get the following error message:

openapi_core.templating.media_types.exceptions.MediaTypeNotFound: Content for the following mimetype not found: application/json. Valid mimetypes: ['image/*']

The valid mimetypes are specified in my OpenAPI schema specifications, but I don't understand why the response's mimetype seems to be application/json.

Solution

The response headers were not written into the response in my tested code. I adapted that. This is a good example of test-driven development, I guess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文