Ticket #5397: issue_5397.patch

File issue_5397.patch, 4.3 KB (added by Ben Sturmfels, 9 years ago)
  • new file mediagoblin/tests/test_tools.py

    From dc4d38106941e2cc26ec29da1e27a75c793570b3 Mon Sep 17 00:00:00 2001
    From: Ben Sturmfels <ben@sturm.com.au>
    Date: Tue, 19 Jan 2016 23:53:52 +1100
    Subject: [PATCH] trac#5397: Allow decode_request to parse content-type headers
     with extra options.
    
    It previously parsed "Content-Type: application/x-www-form-urlencoded", but not "Content-Type: application/x-www-form-urlencoded; charset=utf-8".
    ---
     mediagoblin/tests/test_tools.py | 61 +++++++++++++++++++++++++++++++++++++++++
     mediagoblin/tools/request.py    |  7 +++--
     2 files changed, 66 insertions(+), 2 deletions(-)
     create mode 100644 mediagoblin/tests/test_tools.py
    
    diff --git a/mediagoblin/tests/test_tools.py b/mediagoblin/tests/test_tools.py
    new file mode 100644
    index 0000000..6d3dd47
    - +  
     1# GNU MediaGoblin -- federated, autonomous media hosting
     2# Copyright (C) 2011, 2012 MediaGoblin contributors.  See AUTHORS.
     3#
     4# This program is free software: you can redistribute it and/or modify
     5# it under the terms of the GNU Affero General Public License as published by
     6# the Free Software Foundation, either version 3 of the License, or
     7# (at your option) any later version.
     8#
     9# This program is distributed in the hope that it will be useful,
     10# but WITHOUT ANY WARRANTY; without even the implied warranty of
     11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12# GNU Affero General Public License for more details.
     13#
     14# You should have received a copy of the GNU Affero General Public License
     15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16
     17from __future__ import absolute_import, unicode_literals
     18
     19from werkzeug.wrappers import Request
     20from werkzeug.test import EnvironBuilder
     21
     22from mediagoblin.tools.request import decode_request
     23
     24class TestDecodeRequest(object):
     25    """Test the decode_request function."""
     26
     27    def test_form_type(self):
     28        """Try a normal form-urlencoded request."""
     29        builder = EnvironBuilder(method='POST', data={'foo': 'bar'})
     30        request = Request(builder.get_environ())
     31        data = decode_request(request)
     32        assert data['foo'] == 'bar'
     33
     34    def test_json_type(self):
     35        """Try a normal JSON request."""
     36        builder = EnvironBuilder(
     37            method='POST', content_type='application/json',
     38            data='{"foo": "bar"}')
     39        request = Request(builder.get_environ())
     40        data = decode_request(request)
     41        assert data['foo'] == 'bar'
     42
     43    def test_content_type_with_options(self):
     44        """Content-Type can also have options."""
     45        builder = EnvironBuilder(
     46            method='POST',
     47            content_type='application/x-www-form-urlencoded; charset=utf-8')
     48        request = Request(builder.get_environ())
     49        # Must populate form field manually with non-default content-type.
     50        request.form = {'foo': 'bar'}
     51        data = decode_request(request)
     52        assert data['foo'] == 'bar'
     53
     54    def test_form_type_is_default(self):
     55        """Assume form-urlencoded if blank in the request."""
     56        builder = EnvironBuilder(method='POST', content_type='')
     57        request = Request(builder.get_environ())
     58        # Must populate form field manually with non-default content-type.
     59        request.form = {'foo': 'bar'}
     60        data = decode_request(request)
     61        assert data['foo'] == 'bar'
  • mediagoblin/tools/request.py

    diff --git a/mediagoblin/tools/request.py b/mediagoblin/tools/request.py
    index d2cb0f6..af06967 100644
    a b  
    1717import json
    1818import logging
    1919
     20from werkzeug.http import parse_options_header
     21
    2022from mediagoblin.db.models import User, AccessToken
    2123from mediagoblin.oauth.tools.request import decode_authorization_header
    2224
    def setup_user_in_request(request):  
    6062def decode_request(request):
    6163    """ Decodes a request based on MIME-Type """
    6264    data = request.data
     65    content_type, _ = parse_options_header(request.content_type)
    6366
    64     if request.content_type == json_encoded:
     67    if content_type == json_encoded:
    6568        data = json.loads(data)
    66     elif request.content_type == form_encoded or request.content_type == "":
     69    elif content_type == form_encoded or content_type == "":
    6770        data = request.form
    6871    else:
    6972        data = ""