Opened 10 years ago

Closed 10 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.

See here: https://github.com/django/django/blob/e22c64dfc0230134c0e9d9ae1d9a5c1589fc8b6e/django/test/client.py#L195-L202

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 Kévin Etienne, 10 years ago

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 ensure file.read() returns always bytes.

comment:2 by Tim Graham, 10 years ago

Has patch: set
Summary: django.test.client.RequestFactory file uploaddjango.test.client.RequestFactory file upload fails
Triage Stage: UnreviewedAccepted

On PR 3617, there's a comment that says that PR would address this ticket as well.

comment:3 by Tim Graham, 10 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 Kévin Etienne, 10 years ago

Resolution: duplicate
Status: newclosed

Thanks timgraham PR 3617 is covering it, closing as duplicate of #23911 (ticket)

Note: See TracTickets for help on using tickets.
Back to Top