Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
06a5bcc8
Commit
06a5bcc8
authored
Jan 16, 2017
by
Artem Muterko
Committed by
Tom Christie
Jan 16, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add mediatype tests (#4813)
parent
e965270f
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
25 additions
and
4 deletions
+25
-4
rest_framework/utils/mediatypes.py
+2
-4
tests/test_negotiation.py
+23
-0
No files found.
rest_framework/utils/mediatypes.py
View file @
06a5bcc8
...
@@ -49,10 +49,8 @@ def order_by_precedence(media_type_lst):
...
@@ -49,10 +49,8 @@ def order_by_precedence(media_type_lst):
@python_2_unicode_compatible
@python_2_unicode_compatible
class
_MediaType
(
object
):
class
_MediaType
(
object
):
def
__init__
(
self
,
media_type_str
):
def
__init__
(
self
,
media_type_str
):
if
media_type_str
is
None
:
self
.
orig
=
''
if
(
media_type_str
is
None
)
else
media_type_str
media_type_str
=
''
self
.
full_type
,
self
.
params
=
parse_header
(
self
.
orig
.
encode
(
HTTP_HEADER_ENCODING
))
self
.
orig
=
media_type_str
self
.
full_type
,
self
.
params
=
parse_header
(
media_type_str
.
encode
(
HTTP_HEADER_ENCODING
))
self
.
main_type
,
sep
,
self
.
sub_type
=
self
.
full_type
.
partition
(
'/'
)
self
.
main_type
,
sep
,
self
.
sub_type
=
self
.
full_type
.
partition
(
'/'
)
def
match
(
self
,
other
):
def
match
(
self
,
other
):
...
...
tests/test_negotiation.py
View file @
06a5bcc8
...
@@ -6,6 +6,7 @@ from rest_framework.negotiation import DefaultContentNegotiation
...
@@ -6,6 +6,7 @@ from rest_framework.negotiation import DefaultContentNegotiation
from
rest_framework.renderers
import
BaseRenderer
from
rest_framework.renderers
import
BaseRenderer
from
rest_framework.request
import
Request
from
rest_framework.request
import
Request
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.test
import
APIRequestFactory
from
rest_framework.utils.mediatypes
import
_MediaType
factory
=
APIRequestFactory
()
factory
=
APIRequestFactory
()
...
@@ -55,3 +56,25 @@ class TestAcceptedMediaType(TestCase):
...
@@ -55,3 +56,25 @@ class TestAcceptedMediaType(TestCase):
accepted_renderer
,
accepted_media_type
=
self
.
select_renderer
(
request
)
accepted_renderer
,
accepted_media_type
=
self
.
select_renderer
(
request
)
assert
accepted_media_type
==
'application/openapi+json;version=2.0'
assert
accepted_media_type
==
'application/openapi+json;version=2.0'
assert
accepted_renderer
.
format
==
'swagger'
assert
accepted_renderer
.
format
==
'swagger'
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'
)
params_str
=
''
for
key
,
val
in
mediatype
.
params
.
items
():
params_str
+=
';
%
s=
%
s'
%
(
key
,
val
)
expected
=
'test/*'
+
params_str
assert
str
(
mediatype
)
==
expected
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment