Skip to content

Commit

Permalink
Fixed issues with byte strings
Browse files Browse the repository at this point in the history
  • Loading branch information
timmartin19 committed Feb 23, 2016
1 parent 7a660e4 commit 5c1e5f7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
9 changes: 4 additions & 5 deletions ripozo/resources/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@ def _get_charset(environ):
:rtype: unicode
"""
content_type = environ.get('CONTENT_TYPE', '')
content_type, params = parse_header(content_type)
charset = params.get('charset', 'utf-8')
# Decode according to RFC 5987 https://tools.ietf.org/html/rfc5987
if not isinstance(charset, six.text_type):
charset = charset.decode('ISO-8859-1')
return charset
if not isinstance(content_type, six.text_type):
content_type = content_type.decode('latin1')
content_type, params = parse_header(content_type)
return params.get('charset', 'utf-8')


def _parse_body(environ):
Expand Down
6 changes: 3 additions & 3 deletions ripozo_tests/unit/resources/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import json

import unittest2
from six import StringIO
from six import StringIO, BytesIO
from werkzeug.test import EnvironBuilder

from ripozo.resources.constants.input_categories import QUERY_ARGS, BODY_ARGS, URL_PARAMS
Expand Down Expand Up @@ -220,7 +220,7 @@ def test_parse_body_byte_string(self):
"""Ensure that byte strings can be properly decoded"""
expected = {"some": ["body"]}
body_string = b'some=body'
body = StringIO(body_string)
body = BytesIO(body_string)
environ = EnvironBuilder(input_stream=body).get_environ()

resp = _parse_body(environ)
Expand Down Expand Up @@ -307,6 +307,6 @@ def test_get_charset(self):

def test_get_charset_byte_header(self):
"""Test getting the charset when the headers are bytes"""
environ = {b'CONTENT_TYPE': b'text/plain; charset=blah'}
environ = {'CONTENT_TYPE': b'text/plain; charset=blah'}
charset = _get_charset(environ)
self.assertEqual(charset, 'blah')

0 comments on commit 5c1e5f7

Please sign in to comment.