Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing Django FileResponse

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?

like image 309
Eerik Sven Puudist Avatar asked Apr 16 '19 12:04

Eerik Sven Puudist


1 Answers

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
like image 110
rasca0027 Avatar answered Sep 26 '22 03:09

rasca0027