[Web] Accept charset in content-type for json messages
Trac: https://dev.deluge-torrent.org/ticket/3521 Closes: https://github.com/deluge-torrent/deluge/pull/385
This commit is contained in:
parent
d7c520c85e
commit
b0dba97fec
|
@ -98,6 +98,24 @@ class TestJSON:
|
|||
with pytest.raises(JSONException):
|
||||
json._on_json_request(request)
|
||||
|
||||
def test_on_json_request_valid_content_type(self):
|
||||
"""Ensure content-type application/json is accepted"""
|
||||
json = JSON()
|
||||
request = MagicMock()
|
||||
request.getHeader.return_value = b'application/json'
|
||||
json_data = {'method': 'some.method', 'id': 0, 'params': []}
|
||||
request.json = json_lib.dumps(json_data).encode()
|
||||
json._on_json_request(request)
|
||||
|
||||
def test_on_json_request_valid_content_type_with_charset(self):
|
||||
"""Ensure content-type parameters such as charset are ignored"""
|
||||
json = JSON()
|
||||
request = MagicMock()
|
||||
request.getHeader.return_value = b'application/json;charset=utf-8'
|
||||
json_data = {'method': 'some.method', 'id': 0, 'params': []}
|
||||
request.json = json_lib.dumps(json_data).encode()
|
||||
json._on_json_request(request)
|
||||
|
||||
|
||||
@pytest.mark.usefixtures('daemon', 'client', 'component')
|
||||
class TestJSONCustomUserTestCase:
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
# See LICENSE for more details.
|
||||
#
|
||||
|
||||
import cgi
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
|
@ -190,7 +191,7 @@ class JSON(resource.Resource, component.Component):
|
|||
Handler to take the json data as a string and pass it on to the
|
||||
_handle_request method for further processing.
|
||||
"""
|
||||
content_type = request.getHeader(b'content-type').decode()
|
||||
content_type, _ = cgi.parse_header(request.getHeader(b'content-type').decode())
|
||||
if content_type != 'application/json':
|
||||
message = 'Invalid JSON request content-type: %s' % content_type
|
||||
raise JSONException(message)
|
||||
|
|
Loading…
Reference in New Issue