improve request decoding and json parsing

This commit is contained in:
Martin Zimmermann 2013-11-05 12:35:17 +01:00
parent 6691810316
commit 8d1171df29
4 changed files with 18 additions and 4 deletions

View File

@ -95,11 +95,11 @@ class Bloomfilter:
class JSONRequest(Request): class JSONRequest(Request):
if werkzeug.version.startswith("0.8"): if werkzeug.version.startswith("0.8"):
def get_data(self): def get_data(self, **kw):
return self.data.decode('utf-8') return self.data.decode('utf-8')
def get_json(self): def get_json(self):
try: try:
return json.loads(self.get_data().decode('utf-8')) return json.loads(self.get_data(as_text=True))
except ValueError: except ValueError:
raise BadRequest('Unable to read JSON request') raise BadRequest('Unable to read JSON request')

View File

@ -55,7 +55,7 @@ def new(app, environ, request, uri):
for field in set(data.keys()) - set(['text', 'author', 'website', 'email', 'parent']): for field in set(data.keys()) - set(['text', 'author', 'website', 'email', 'parent']):
data.pop(field) data.pop(field)
if not data.get("text"): if "text" not in data or data["text"] is None or len(data["text"]) < 3:
raise BadRequest("no text given") raise BadRequest("no text given")
if "id" in data and not isinstance(data["id"], int): if "id" in data and not isinstance(data["id"], int):
@ -162,7 +162,7 @@ def single(app, environ, request, id):
if request.method == 'PUT': if request.method == 'PUT':
data = request.get_json() data = request.get_json()
if data.get("text") is not None and len(data['text']) < 3: if "text" not in data or data["text"] is None or len(data["text"]) < 3:
raise BadRequest("no text given") raise BadRequest("no text given")
for key in set(data.keys()) - set(["text", "author", "website"]): for key in set(data.keys()) - set(["text", "author", "website"]):

View File

@ -1,3 +1,5 @@
# -*- encoding: utf-8 -*-
from __future__ import unicode_literals from __future__ import unicode_literals
@ -92,6 +94,18 @@ class TestComments(unittest.TestCase):
assert rv["mode"] == 1 assert rv["mode"] == 1
assert rv["text"] == '<p>Lorem ipsum ...</p>\n' assert rv["text"] == '<p>Lorem ipsum ...</p>\n'
def textCreateWithNonAsciiText(self):
rv = self.post('/new?uri=%2Fpath%2F', data=json.dumps({'text': 'Здравствуй, мир!'}))
assert rv.status_code == 201
assert any(filter(lambda header: header[0] == 'Set-Cookie', rv.headers))
rv = loads(rv.data)
assert rv["mode"] == 1
assert rv["text"] == '<p>Здравствуй, мир!</p>\n'
def testCreateMultiple(self): def testCreateMultiple(self):
a = self.post('/new?uri=test', data=json.dumps({'text': '...'})) a = self.post('/new?uri=test', data=json.dumps({'text': '...'}))

0
specs/test_cors.py Normal file
View File