Opened 11 years ago
Closed 11 years ago
#23912 closed Bug (duplicate)
django.test.client.RequestFactory file upload fails
| Reported by: | Kévin Etienne | Owned by: | nobody |
|---|---|---|---|
| Component: | Testing framework | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Accepted | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When using post on RequestFactory to upload a file we are calling django.test.client.encode_file.
encode_file tries to join a list of bytes but file.read() return a str.
Which result to the following error:
====================================================================== ERROR: test_upload_file (test_client.tests.RequestFactoryTest) ---------------------------------------------------------------------- Traceback (most recent call last): File "/tests.py", line 10, in test_upload_file factory.post('/somewhere/', {'file': f}) File "/Users/k/code/github.com/incuna/django/django/test/client.py", line 293, in post post_data = self._encode_data(data, content_type) File "/Users/k/code/github.com/incuna/django/django/test/client.py", line 257, in _encode_data return encode_multipart(BOUNDARY, data) File "/Users/k/code/github.com/incuna/django/django/test/client.py", line 182, in encode_multipart return b'\r\n'.join(lines) TypeError: sequence item 4: expected bytes, bytearray, or an object with the buffer interface, str found
Change History (4)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
| Has patch: | set |
|---|---|
| Summary: | django.test.client.RequestFactory file upload → django.test.client.RequestFactory file upload fails |
| Triage Stage: | Unreviewed → Accepted |
On PR 3617, there's a comment that says that PR would address this ticket as well.
comment:3 by , 11 years ago
Should we close this as a duplicate? Would the test in the related PR prevent a regression here or do we need another one?
comment:4 by , 11 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
This can be resolved by using
open(filepath, 'rb')but when using the test client we have no control over it. I think it's probably best to ensurefile.read()returns alwaysbytes.