Skip to content

Instantly share code, notes, and snippets.

@icefoxen
Created April 18, 2016 20:05
Show Gist options
  • Save icefoxen/028ca0828991374a5b1fffa3e896f644 to your computer and use it in GitHub Desktop.
Save icefoxen/028ca0828991374a5b1fffa3e896f644 to your computer and use it in GitHub Desktop.
djangorestframework APITestCase does not post files correctly
# This case works, using django.test.TestCase
# It creates the output:
# Good request: {'SERVER_PORT': '80', 'REQUEST_METHOD': 'POST', 'wsgi.url_scheme': 'http', 'CONTENT_LENGTH': 158, 'wsgi.input': <django.test.client.FakePayload object at 0x7f79b8287f28>, 'PATH_INFO': '/api/v0/dataset/upload', 'CONTENT_TYPE': 'multipart/form-data; boundary=BoUnDaRyStRiNg', 'QUERY_STRING': ''}
class DatasetAPITest2(TestCase):
"""Tests for uploading files to the dataset API.
This has to be a django.test.TestCase because APITestCase doesn't appear
to submit multipart/form-data properly, """
@classmethod
def setUpTestData(cls):
# Set up test user auth and token.
user = User.objects.create_superuser(username="testadmin",
email="test@foo.org", password="testpass")
def setUp(self):
self.user = User.objects.get(username="testadmin")
self.client.login(username='testadmin', password='testpass')
def test_dataset_list_post(self):
"Tests using post to create a dataset."
testfile = io.StringIO("Test faux-file")
url = reverse('client_api:dataset_upload')
response = self.client.post(url, {"file":testfile})
print("Good request:", response.request)
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT)
# This case has identical code and is set to use the multipart/form-data content type
# (instead of the default json content type, which doesn't handle file uploads gracefully)
# but using the rest_framework.test.APITestCase class.
# It produces the following output:
# Flawed request: {'wsgi.url_scheme': 'http', 'QUERY_STRING': '', 'PATH_INFO': '/api/v0/dataset/upload', 'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': 'multipart/form-data', 'SERVER_PORT': '80', 'wsgi.input': <django.test.client.FakePayload object at 0x7f2b0b6e7780>, 'CONTENT_LENGTH': 49}
# ======================================================================
# FAIL: test_dataset_list_post (client_api.tests.DatasetAPITest)
# Tests using post to create a dataset.
# ----------------------------------------------------------------------
# Traceback (most recent call last):
# File "/home/user/server/src/client_api/tests.py", line 182, in test_dataset_list_post
# self.assertEqual(response.status_code, status.HTTP_200_OK)
# AssertionError: 400 != 200
class DatasetAPITest(APITestCase):
@classmethod
def setUpTestData(cls):
# Set up test user auth and token.
user = User.objects.create_superuser(username="testadmin",
email="test@foo.org", password="testpass")
def setUp(self):
self.user = User.objects.get(username="testadmin")
self.client.login(username='testadmin', password='testpass')
def test_dataset_list_post(self):
"Tests using post to create a dataset."
testfile = io.StringIO("Test faux-file")
url = reverse('client_api:dataset_upload')
response = self.client.post(url, {"file":testfile},
content_type='multipart/form-data')
print("Flawed request:", response.request)
#content_type='application/x-www-form-urlencoded')
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, testData)
# The view that the request is going to
class DatasetUploadView(APIView):
parser_classes = (FormParser, MultiPartParser, FileUploadParser)
def post(self, request, format=None):
print("Request:", request.data)
file_obj = request.FILES['file']
print(file_obj)
return Response("right", status=204)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment