Skip to content

Commit 450c541

Browse files
committed
Merge pull request #2868 from ticosax/versioning-header-all
Check AcceptHeaderVersioning with content negotiation in place
2 parents c397705 + 63511c0 commit 450c541

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

rest_framework/negotiation.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"""
55
from __future__ import unicode_literals
66
from django.http import Http404
7-
from rest_framework import exceptions
7+
from rest_framework import HTTP_HEADER_ENCODING, exceptions
88
from rest_framework.settings import api_settings
99
from rest_framework.utils.mediatypes import order_by_precedence, media_type_matches
1010
from rest_framework.utils.mediatypes import _MediaType
@@ -54,13 +54,19 @@ def select_renderer(self, request, renderers, format_suffix=None):
5454
for media_type in media_type_set:
5555
if media_type_matches(renderer.media_type, media_type):
5656
# Return the most specific media type as accepted.
57+
media_type_wrapper = _MediaType(media_type)
5758
if (
5859
_MediaType(renderer.media_type).precedence >
59-
_MediaType(media_type).precedence
60+
media_type_wrapper.precedence
6061
):
6162
# Eg client requests '*/*'
6263
# Accepted media type is 'application/json'
63-
return renderer, renderer.media_type
64+
full_media_type = ';'.join(
65+
(renderer.media_type,) +
66+
tuple('{0}={1}'.format(
67+
key, value.decode(HTTP_HEADER_ENCODING))
68+
for key, value in media_type_wrapper.params.items()))
69+
return renderer, full_media_type
6470
else:
6571
# Eg client requests 'application/json; indent=8'
6672
# Accepted media type is 'application/json; indent=8'

tests/test_versioning.py

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ def test_accept_header_versioning(self):
8282
response = view(request)
8383
assert response.data == {'version': '1.2.3'}
8484

85+
request = factory.get('/endpoint/', HTTP_ACCEPT='*/*; version=1.2.3')
86+
response = view(request)
87+
assert response.data == {'version': '1.2.3'}
88+
8589
request = factory.get('/endpoint/', HTTP_ACCEPT='application/json')
8690
response = view(request)
8791
assert response.data == {'version': None}

0 commit comments

Comments
 (0)