I have a Django view that returns a file. The FileResponse
is built for that purpose. However, I do not understand how to test this.
Right now I use the HttpResponse
and test it like this:
response = client.get(url)
io = io = BytesIO(response.content)
The io
object can now be used for further testing.
However, if I try the following with FileResponse
(which is derived from StreamingHttpResponse
and thus has streaming_content
instead of content
), I get the following exception:
TypeError: a bytes-like object is required, not 'map'
If I cast the map
object to bytes
like this:
response = client.get(url)
io = io = BytesIO(bytes(response.streaming_content))
I get another exception: TypeError: 'bytes' object cannot be interpreted as an integer
How can I get an BytesIO
object from FileResponse.streaming_content
?
streaming_content is iterable, not bytestring, so you will have to join them.
fileres = bytes("test", 'utf-8')
stream = b''.join(response.streaming_content)
assert stream == fileres
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With