8000 Added test for OPTIONS before object creation from a PUT · alumni/django-rest-framework@6e7e4fc · GitHub
[go: up one dir, main page]

Skip to content < 8000 div data-target="react-partial.reactRoot">

Commit 6e7e4fc

Browse files
author
Edmond Wong
committed
Added test for OPTIONS before object creation from a PUT
1 parent 3063a50 commit 6e7e4fc

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

rest_framework/generics.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ def metadata(self, request):
360360
self.get_object()
361361
except Http404:
362362
# Http404 should be acceptable and the serializer
363-
# metadata should be populated.
363+
# metadata should be populated. Except this so the
364+
# outer "else" clause of the try-except-else block
365+
# will be executed.
364366
pass
365367
except (exceptions.APIException, PermissionDenied):
366368
pass

rest_framework/tests/test_generics.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,48 @@ def test_options_instance_view(self):
272272
self.assertEqual(response.status_code, status.HTTP_200_OK)
273273
self.assertEqual(response.data, expected)
274274

275+
def test_options_before_instance_create(self):
276+
"""
277+
OPTIONS requests to RetrieveUpdateDestroyAPIView should return metadata
278+
before the instance has been created
279+
"""
280+
request = factory.options('/999')
281+
with self.assertNumQueries(1):
282+
response = self.view(request, pk=999).render()
283+
expected = {
284+
'parses': [
285+
'application/json',
286+
'application/x-www-form-urlencoded',
287+
'multipart/form-data'
288+
],
289+
'renders': [
290+
'application/json',
291+
'text/html'
292+
],
293+
'name': 'Instance',
294+
'description': 'Example description for OPTIONS.',
295+
'actions': {
296+
'PUT': {
297+
'text': {
298+
'max_length': 100,
299+
'read_only': False,
300+
'required': True,
301+
'type': 'string',
302+
'label': 'Text comes here',
303+
'help_text': 'Text description.'
304+
},
305+
'id': {
306+
'read_only': True,
307+
'required': False,
308+
'type': 'integer',
309+
'label': 'ID',
310+
},
311+
}
312+
}
313+
}
314+
self.assertEqual(response.status_code, status.HTTP_200_OK)
315+
self.assertEqual(response.data, expected)
316+
275317
def test_get_instance_view_incorrect_arg(self):
276318
"""
277319
GET requests with an incorrect pk type, should raise 404, not 500.

0 commit comments

Comments
 (0)
0