Initial tests for API client

This commit is contained in:
Tom Christie 2016-08-19 12:20:23 +01:00
parent 0cc3f5008f
commit e5b4498c27
2 changed files with 42 additions and 1 deletions

View File

@ -16,7 +16,7 @@ from django.utils import six
from django.utils.encoding import force_bytes
from django.utils.http import urlencode
from rest_framework.compat import requests
from rest_framework.compat import coreapi, requests
from rest_framework.settings import api_settings
@ -126,6 +126,14 @@ def get_requests_client():
return DjangoTestSession()
def get_api_client():
assert coreapi is not None, 'coreapi must be installed'
session = get_requests_client()
return coreapi.Client(transports=[
coreapi.transports.HTTPTransport(session=session)
])
class APIRequestFactory(DjangoRequestFactory):
renderer_classes_list = api_settings.TEST_REQUEST_RENDERER_CLASSES
default_format = api_settings.TEST_REQUEST_DEFAULT_FORMAT

33
tests/test_api_client.py Normal file
View File

@ -0,0 +1,33 @@
from __future__ import unicode_literals
import unittest
from django.conf.urls import url
from django.test import override_settings
from rest_framework.compat import coreapi
from rest_framework.response import Response
from rest_framework.test import APITestCase, get_api_client
from rest_framework.views import APIView
class Root(APIView):
def get(self, request):
return Response({
'hello': 'world',
})
urlpatterns = [
url(r'^$', Root.as_view()),
]
@unittest.skipUnless(coreapi, 'coreapi not installed')
@override_settings(ROOT_URLCONF='tests.test_api_client')
class APIClientTests(APITestCase):
def test_api_client(self):
client = get_api_client()
schema = client.get('/')
data = client.action(schema, ['echo'])
assert data == {'hello': 'world'}