|
6 | 6 | from rest_framework import status
|
7 | 7 | from rest_framework.authentication import BasicAuthentication
|
8 | 8 | from rest_framework.decorators import (
|
9 |
| - action, api_view, authentication_classes, parser_classes, |
10 |
| - permission_classes, renderer_classes, schema, throttle_classes |
| 9 | + action, api_view, authentication_classes, content_negotiation_class, |
| 10 | + metadata_class, parser_classes, permission_classes, renderer_classes, |
| 11 | + schema, throttle_classes, versioning_class |
11 | 12 | )
|
| 13 | +from rest_framework.negotiation import BaseContentNegotiation |
12 | 14 | from rest_framework.parsers import JSONParser
|
13 | 15 | from rest_framework.permissions import IsAuthenticated
|
14 | 16 | from rest_framework.renderers import JSONRenderer
|
15 | 17 | from rest_framework.response import Response
|
16 | 18 | from rest_framework.schemas import AutoSchema
|
17 | 19 | from rest_framework.test import APIRequestFactory
|
18 | 20 | from rest_framework.throttling import UserRateThrottle
|
| 21 | +from rest_framework.versioning import QueryParameterVersioning |
19 | 22 | from rest_framework.views import APIView
|
20 | 23 |
|
21 | 24 |
|
@@ -150,6 +153,43 @@ def view(request):
|
150 | 153 | response = view(request)
|
151 | 154 | assert response.status_code == status.HTTP_429_TOO_MANY_REQUESTS
|
152 | 155 |
|
| 156 | + def test_versioning_class(self): |
| 157 | + @api_view(["GET"]) |
| 158 | + @versioning_class(QueryParameterVersioning) |
| 159 | + def view(request): |
| 160 | + return Response({"version": request.version}) |
| 161 | + |
| 162 | + request = self.factory.get("/?version=1.2.3") |
| 163 | + response = view(request) |
| 164 | + assert response.data == {"version": "1.2.3"} |
| 165 | + |
| 166 | + def test_metadata_class(self): |
| 167 | + # From TestMetadata.test_none_metadata() |
| 168 | + @api_view() |
| 169 | + @metadata_class(None) |
| 170 | + def view(request): |
| 171 | + return Response({}) |
| 172 | + |
| 173 | + request = self.factory.options('/') |
| 174 | + response = view(request) |
| 175 | + assert response.status_code == status.HTTP_405_METHOD_NOT_ALLOWED |
| 176 | + assert response.data == {'detail': 'Method "OPTIONS" not allowed.'} |
| 177 | + |
| 178 | + def test_content_negotiation(self): |
| 179 | + class CustomContentNegotiation(BaseContentNegotiation): |
| 180 | + def select_renderer(self, request, renderers, format_suffix): |
| 181 | + assert request.META['HTTP_ACCEPT'] == 'custom/type' |
| 182 | + return (renderers[0], renderers[0].media_type) |
| 183 | + |
| 184 | + @api_view(["GET"]) |
| 185 | + @content_negotiation_class(CustomContentNegotiation) |
| 186 | + def view(request): |
| 187 | + return Response({}) |
| 188 | + |
| 189 | + request = self.factory.get('/', HTTP_ACCEPT='custom/type') |
| 190 | + response = view(request) |
| 191 | + assert response.status_code == status.HTTP_200_OK |
| 192 | + |
153 | 193 | def test_schema(self):
|
154 | 194 | """
|
155 | 195 | Checks CustomSchema class is set on view
|
|
0 commit comments