mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-08-03 12:00:12 +03:00
Merge fa4e904259
into e3c5a13451
This commit is contained in:
commit
dbda474138
|
@ -1,6 +1,7 @@
|
|||
# -- coding: utf-8 --
|
||||
|
||||
from __future__ import unicode_literals
|
||||
import tempfile
|
||||
from django.contrib.auth.models import User
|
||||
from django.test import TestCase
|
||||
from rest_framework.compat import patterns, url
|
||||
|
@ -9,6 +10,10 @@ from rest_framework.response import Response
|
|||
from rest_framework.test import APIClient, APIRequestFactory, force_authenticate
|
||||
|
||||
|
||||
BOUNDARY = 'BoUnDaRyStRiNg'
|
||||
MULTIPART_CONTENT = 'multipart/form-data; boundary=%s' % BOUNDARY
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
def view(request):
|
||||
return Response({
|
||||
|
@ -26,9 +31,18 @@ def session_view(request):
|
|||
})
|
||||
|
||||
|
||||
@api_view(['GET', 'POST'])
|
||||
def echo(request):
|
||||
return Response({
|
||||
'data': request.DATA.dict(),
|
||||
'files': request.FILES.dict()
|
||||
})
|
||||
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^view/$', view),
|
||||
url(r'^session-view/$', session_view),
|
||||
url(r'^echo/$', echo),
|
||||
)
|
||||
|
||||
|
||||
|
@ -97,6 +111,37 @@ class TestAPITestClient(TestCase):
|
|||
self.assertEqual(response.status_code, 403)
|
||||
self.assertEqual(response.data, expected)
|
||||
|
||||
def test_echo(self):
|
||||
"""
|
||||
The echo test view returns the request data as-is.
|
||||
"""
|
||||
example_data = {'foo': 'bar'}
|
||||
response = self.client.post('/echo/', example_data)
|
||||
self.assertEqual(response.data['data'], example_data)
|
||||
self.assertEqual(response.data['files'], {})
|
||||
|
||||
def test_echo_multipart(self):
|
||||
"""
|
||||
The test client sends data correctly with multipart content type.
|
||||
"""
|
||||
example_data = {'foo': 'bar'}
|
||||
response = self.client.post('/echo/', example_data, content_type=MULTIPART_CONTENT)
|
||||
self.assertEqual(response.data['data'], example_data)
|
||||
self.assertEqual(response.data['files'], {})
|
||||
|
||||
def test_fileupload(self):
|
||||
"""
|
||||
The test client is capable of uploading a file.
|
||||
"""
|
||||
with tempfile.NamedTemporaryFile(suffix='.txt') as example_file:
|
||||
example_file.write(b'This is a dummy file.')
|
||||
example_file.seek(0)
|
||||
response = self.client.post(
|
||||
'/echo/',
|
||||
{'file': example_file},
|
||||
)
|
||||
self.assertEqual(list(response.data['files'].keys()), [u'file'])
|
||||
|
||||
|
||||
class TestAPIRequestFactory(TestCase):
|
||||
def test_csrf_exempt_by_default(self):
|
||||
|
|
Loading…
Reference in New Issue
Block a user