Reputation: 157
For my project made in Django, I have a page with a form allowing the user to upload a zipfile.
Then from this zip file, I open it and read a file from there.
I started with the unit tests to validate all cases :
- Not a .zip extension
- zipfile.is_zipfile()
return False
- Can't find the file I want to read inside
- File inside is invalid
For my unit tests, I create different files for each error, and upload it using django.test.Client
as so :
with open_file('wrong_format.zip') as file:
response = self.client.post(url, {'archive': file})
self.assertEqual(response.status_code, 200)
self.assertContains(response, '<li>Only zip archived are allowed</li>')
But I get the following error : UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 10: invalid start byte
It works fine when I do it from the browser.
Here's the stack :
Traceback (most recent call last):
File "/app/plugins/tests/uploads.py", line 33, in test_bad_archive
response = self.client.post(url, {'archive': file})
File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 548, in post
secure=secure, **extra)
File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 347, in post
post_data = self._encode_data(data, content_type)
File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 311, in _encode_data
return encode_multipart(BOUNDARY, data)
File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 201, in encode_multipart
lines.extend(encode_file(boundary, key, value))
File "/usr/local/lib/python3.6/site-packages/django/test/client.py", line 254, in encode_file
to_bytes(file.read())
File "/usr/local/lib/python3.6/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x99 in position 10: invalid start byte
Upvotes: 1
Views: 1118
Reputation: 12080
Opening the file without the binary flag b
was ok in Python 2 where binary and string were the same datatype, but Python 3 is more strict about it. So if you do open(file, 'rb')
the system won't try to interpret the zip file as text.
Upvotes: 2