Created
March 21, 2019 09:09
-
-
Save nghiaht/682c2d8d40272c52dbf7adf214f1c0f1 to your computer and use it in GitHub Desktop.
Testing Django Rest Framework with multipart/form-data request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.test import TestCase | |
from rest_framework.test import APIClient | |
class FileUploadTestCase(TestCase): | |
def test_upload(self): | |
client = APIClient() | |
client.credentials(HTTP_AUTHORIZATION='Token ' + self.token.key) | |
# or self.client (Django https://docs.djangoproject.com/en/dev/topics/testing/advanced/) | |
response = client.post(reverse('upload-file'), { | |
"image": open("tests/test.png", "rb"), | |
"name": "test" | |
}) | |
self.assertEqual(200, response.status_code) | |
AllStar6581
commented
Dec 17, 2021
•
@AllStar6581 's response works for me, thanks, more than a year but still effective :)
thanks @AllStar6581 , this is the cleanest of all
Thanks @AllStar6581 lifesaver.
Thanks, @AllStar6581
I was not using encode_multipart
and the view received no data.
@AllStar6581 Thank you! I was searching for working solution for 2 days.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment