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
196c21f3
Commit
196c21f3
authored
Feb 01, 2011
by
tom christie tom@tomchristie.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Browser UA tests passing
parent
e1981659
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
6 deletions
+24
-6
djangorestframework/resource.py
+5
-0
djangorestframework/tests.py
+19
-6
No files found.
djangorestframework/resource.py
View file @
196c21f3
...
...
@@ -17,6 +17,8 @@ import re
__all__
=
[
'Resource'
]
_MSIE_USER_AGENT
=
re
.
compile
(
r'^Mozilla/[0-9]+\.[0-9]+ \([^)]*; MSIE [0-9]+\.[0-9]+[a-z]?;[^)]*\)(?!.* Opera )'
)
class
Resource
(
object
):
"""Handles incoming requests and maps them to REST operations,
...
...
@@ -57,6 +59,7 @@ class Resource(object):
CONTENT_PARAM
=
'_content'
# Allow override of body content in form params (allows sending arbitrary content with standard forms)
CSRF_PARAM
=
'csrfmiddlewaretoken'
# Django's CSRF token used in form params
_MUNGE_IE_ACCEPT_HEADER
=
True
def
__new__
(
cls
,
*
args
,
**
kwargs
):
"""Make the class callable so it can be used as a Django view."""
...
...
@@ -312,6 +315,8 @@ class Resource(object):
if
self
.
ACCEPT_QUERY_PARAM
and
request
.
GET
.
get
(
self
.
ACCEPT_QUERY_PARAM
,
None
):
# Use _accept parameter override
accept_list
=
[
request
.
GET
.
get
(
self
.
ACCEPT_QUERY_PARAM
)]
elif
self
.
_MUNGE_IE_ACCEPT_HEADER
and
request
.
META
.
has_key
(
'HTTP_USER_AGENT'
)
and
_MSIE_USER_AGENT
.
match
(
request
.
META
[
'HTTP_USER_AGENT'
]):
accept_list
=
[
'text/html'
,
'*/*'
]
elif
request
.
META
.
has_key
(
'HTTP_ACCEPT'
):
# Use standard HTTP Accept negotiation
accept_list
=
request
.
META
[
"HTTP_ACCEPT"
]
.
split
(
','
)
...
...
djangorestframework/tests.py
View file @
196c21f3
...
...
@@ -50,19 +50,20 @@ OPERA_11_0_MSIE_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 8.0; X11; Linux x86_
OPERA_11_0_OPERA_USER_AGENT
=
'Opera/9.80 (X11; Linux x86_64; U; pl) Presto/2.7.62 Version/11.00'
class
UserAgentMungingTest
(
TestCase
):
""""""
"""We need to fake up the accept headers when we deal with MSIE. Blergh.
http://www.gethifi.com/blog/browser-rest-http-accept-headers"""
def
setUp
(
self
):
class
MockResource
(
Resource
):
anon_allowed_methods
=
allowed_methods
=
(
'GET'
,)
def
get
(
self
,
request
,
auth
):
return
{
'a'
:
1
,
'b'
:
2
,
'c'
:
3
}
def
setUp
(
self
):
self
.
rf
=
RequestFactory
()
self
.
MockResource
=
MockResource
def
test_m
sie8_ua_munge_accept
(
self
):
def
test_m
unge_msie_accept_header
(
self
):
"""Send MSIE user agent strings and ensure that we get an HTML response,
even if we set a */* accept header.
(Which MSIE annoyingly does)
"""
even if we set a */* accept header."""
for
user_agent
in
(
MSIE_9_USER_AGENT
,
MSIE_8_USER_AGENT
,
MSIE_7_USER_AGENT
):
...
...
@@ -70,7 +71,19 @@ class UserAgentMungingTest(TestCase):
resp
=
self
.
MockResource
(
req
)
self
.
assertEqual
(
resp
[
'Content-Type'
],
'text/html'
)
def
test_other_ua_dont_munge_accept
(
self
):
def
test_dont_munge_msie_accept_header
(
self
):
"""Turn off _MUNGE_IE_ACCEPT_HEADER, send MSIE user agent strings and ensure
that we get a JSON response if we set a */* accept header."""
self
.
MockResource
.
_MUNGE_IE_ACCEPT_HEADER
=
False
for
user_agent
in
(
MSIE_9_USER_AGENT
,
MSIE_8_USER_AGENT
,
MSIE_7_USER_AGENT
):
req
=
self
.
rf
.
get
(
'/'
,
HTTP_ACCEPT
=
'*/*'
,
HTTP_USER_AGENT
=
user_agent
)
resp
=
self
.
MockResource
(
req
)
self
.
assertEqual
(
resp
[
'Content-Type'
],
'application/json'
)
def
test_dont_munge_nice_browsers_accept_header
(
self
):
"""Send Non-MSIE user agent strings and ensure that we get a JSON response,
if we set a */* Accept header. (Other browsers will correctly set the Accept header)"""
for
user_agent
in
(
FIREFOX_4_0_USER_AGENT
,
...
...
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