2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2017-01-19 13:43:18 +03:00
|
|
|
import pytest
|
|
|
|
from django.http import Http404
|
2012-10-05 13:23:47 +04:00
|
|
|
from django.test import TestCase
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2017-01-19 13:50:17 +03:00
|
|
|
from rest_framework.negotiation import (
|
|
|
|
BaseContentNegotiation, DefaultContentNegotiation
|
|
|
|
)
|
2013-05-18 18:45:05 +04:00
|
|
|
from rest_framework.renderers import BaseRenderer
|
2015-06-25 23:55:51 +03:00
|
|
|
from rest_framework.request import Request
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2017-01-16 21:17:51 +03:00
|
|
|
from rest_framework.utils.mediatypes import _MediaType
|
2013-02-04 23:51:31 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
|
2016-07-27 13:49:01 +03:00
|
|
|
class MockOpenAPIRenderer(BaseRenderer):
|
|
|
|
media_type = 'application/openapi+json;version=2.0'
|
|
|
|
format = 'swagger'
|
|
|
|
|
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockJSONRenderer(BaseRenderer):
|
2012-10-05 13:23:47 +04:00
|
|
|
media_type = 'application/json'
|
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class MockHTMLRenderer(BaseRenderer):
|
2012-10-05 13:23:47 +04:00
|
|
|
media_type = 'text/html'
|
|
|
|
|
2013-05-18 20:21:43 +04:00
|
|
|
|
2013-05-18 18:45:05 +04:00
|
|
|
class NoCharsetSpecifiedRenderer(BaseRenderer):
|
|
|
|
media_type = 'my/media'
|
|
|
|
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
class TestAcceptedMediaType(TestCase):
|
|
|
|
def setUp(self):
|
2016-07-27 13:49:01 +03:00
|
|
|
self.renderers = [MockJSONRenderer(), MockHTMLRenderer(), MockOpenAPIRenderer()]
|
2012-10-05 13:23:47 +04:00
|
|
|
self.negotiator = DefaultContentNegotiation()
|
|
|
|
|
2012-10-18 01:58:18 +04:00
|
|
|
def select_renderer(self, request):
|
|
|
|
return self.negotiator.select_renderer(request, self.renderers)
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_without_accept_use_renderer(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
request = Request(factory.get('/'))
|
2013-05-18 20:21:43 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2017-01-04 15:58:01 +03:00
|
|
|
assert accepted_media_type == 'application/json'
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_underspecifies_accept_use_renderer(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
request = Request(factory.get('/', HTTP_ACCEPT='*/*'))
|
2013-05-18 20:21:43 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2017-01-04 15:58:01 +03:00
|
|
|
assert accepted_media_type == 'application/json'
|
2012-10-05 13:23:47 +04:00
|
|
|
|
|
|
|
def test_client_overspecifies_accept_use_client(self):
|
2013-02-04 23:51:31 +04:00
|
|
|
request = Request(factory.get('/', HTTP_ACCEPT='application/json; indent=8'))
|
2013-05-18 20:21:43 +04:00
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2017-01-04 15:58:01 +03:00
|
|
|
assert accepted_media_type == 'application/json; indent=8'
|
2016-07-27 13:49:01 +03:00
|
|
|
|
|
|
|
def test_client_specifies_parameter(self):
|
|
|
|
request = Request(factory.get('/', HTTP_ACCEPT='application/openapi+json;version=2.0'))
|
|
|
|
accepted_renderer, accepted_media_type = self.select_renderer(request)
|
2017-01-04 15:58:01 +03:00
|
|
|
assert accepted_media_type == 'application/openapi+json;version=2.0'
|
|
|
|
assert accepted_renderer.format == 'swagger'
|
2017-01-16 21:17:51 +03:00
|
|
|
|
|
|
|
def test_match_is_false_if_main_types_not_match(self):
|
|
|
|
mediatype = _MediaType('test_1')
|
|
|
|
anoter_mediatype = _MediaType('test_2')
|
|
|
|
assert mediatype.match(anoter_mediatype) is False
|
|
|
|
|
|
|
|
def test_mediatype_match_is_false_if_keys_not_match(self):
|
|
|
|
mediatype = _MediaType(';test_param=foo')
|
|
|
|
another_mediatype = _MediaType(';test_param=bar')
|
|
|
|
assert mediatype.match(another_mediatype) is False
|
|
|
|
|
|
|
|
def test_mediatype_precedence_with_wildcard_subtype(self):
|
|
|
|
mediatype = _MediaType('test/*')
|
|
|
|
assert mediatype.precedence == 1
|
|
|
|
|
|
|
|
def test_mediatype_string_representation(self):
|
|
|
|
mediatype = _MediaType('test/*; foo=bar')
|
2017-11-06 12:02:48 +03:00
|
|
|
assert str(mediatype) == 'test/*; foo=bar'
|
2017-01-19 13:43:18 +03:00
|
|
|
|
|
|
|
def test_raise_error_if_no_suitable_renderers_found(self):
|
|
|
|
class MockRenderer(object):
|
|
|
|
format = 'xml'
|
|
|
|
renderers = [MockRenderer()]
|
|
|
|
with pytest.raises(Http404):
|
|
|
|
self.negotiator.filter_renderers(renderers, format='json')
|
2017-01-19 13:50:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
class BaseContentNegotiationTests(TestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.negotiator = BaseContentNegotiation()
|
|
|
|
|
|
|
|
def test_raise_error_for_abstract_select_parser_method(self):
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
self.negotiator.select_parser(None, None)
|
|
|
|
|
|
|
|
def test_raise_error_for_abstract_select_renderer_method(self):
|
|
|
|
with pytest.raises(NotImplementedError):
|
|
|
|
self.negotiator.select_renderer(None, None)
|