Python 测试Django文件响应
我有一个Django视图,它返回一个文件。Python 测试Django文件响应,python,django,django-views,Python,Django,Django Views,我有一个Django视图,它返回一个文件。FileResponse就是为此而构建的。然而,我不明白如何测试这一点 现在,我使用HttpResponse并对其进行如下测试: response=client.get(url) io=io=BytesIO(response.content) io对象现在可以用于进一步的测试 但是,如果我尝试使用FileResponse(它源自StreamingHttpResponse,因此具有streaming\u content,而不是content),我会得到以
FileResponse
就是为此而构建的。然而,我不明白如何测试这一点
现在,我使用HttpResponse
并对其进行如下测试:
response=client.get(url)
io=io=BytesIO(response.content)
io
对象现在可以用于进一步的测试
但是,如果我尝试使用
FileResponse
(它源自StreamingHttpResponse
,因此具有streaming\u content
,而不是content
),我会得到以下异常:
TypeError:需要类似字节的对象,而不是“映射”
如果我将映射
对象强制转换为字节
,如下所示:
response=client.get(url)
io=io=BytesIO(字节(响应、流式处理内容))
我遇到另一个异常:TypeError:“bytes”对象不能解释为整数
如何从
FileResponse.streaming\u content
获取BytesIO
对象?streaming\u content是可编辑的,而不是通过testring,因此您必须加入它们
fileres = bytes("test", 'utf-8')
stream = b''.join(response.streaming_content)
assert stream == fileres
streaming\u content()
是字符串的迭代器,因此您应该对其进行迭代。实际上,您可以直接迭代响应(\uuuu iter\uuu()
被定义为self.streaming\u content
)