Jonathan Ballet avatar Jonathan Ballet committed a4e9410

Fix #87: Split charset encoding form content-type

The content-type of an request can contain one or more charset informations,
which should be stripped of, if we only want to deal with the 'real'
content-type.

Comments (0)

Files changed (2)

         """
         type_formencoded = "application/x-www-form-urlencoded"
 
-        ctype = self.request.META.get('CONTENT_TYPE', type_formencoded)
+        # Content-Type format:
+        # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17
+        ctype = self.request.META.get('CONTENT_TYPE',
+                                      type_formencoded).split(";")[0]
+
         
         if type_formencoded in ctype:
             return None

tests/test_project/apps/testapp/tests.py

 from django.test import TestCase
+from django.http import HttpRequest
 from django.contrib.auth.models import User
 from django.utils import simplejson
 from django.conf import settings
 
-from piston import oauth
+from piston import oauth, utils
 from piston.models import Consumer, Token
 from piston.forms import OAuthAuthenticationForm
 
 
 
 
+class MimerTests(TestCase):
+    def setUp(self):
+        self.request = HttpRequest()
+        self.mimer = utils.Mimer(self.request)
+
+    def test_content_type_without_encoding(self):
+        self.request.META['CONTENT_TYPE'] = 'application/json'
+        self.assertEqual('application/json', mimer.content_type())
+
+    def test_content_type_with_encoding(self):
+        self.request.META['CONTENT_TYPE'] = 'application/json; charset=UTF-8'
+        self.assertEqual('application/json', mimer.content_type())
+
+
 class OAuthTests(MainTests):
     signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.