如何在龙卷风中使用图像数据模拟HTTPRESPONDE?
问题
我正在测试龙卷风
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论