From 82faee01bc6aced112737f6215ecc7d3936490d8 Mon Sep 17 00:00:00 2001 From: Sebastian Wagner Date: Thu, 25 Jan 2018 11:27:07 +0100 Subject: [PATCH] mgr/dashboard_v2: Added test_restresource.py Signed-off-by: Sebastian Wagner --- src/pybind/mgr/dashboard_v2/restresource.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pybind/mgr/dashboard_v2/restresource.py b/src/pybind/mgr/dashboard_v2/restresource.py index 068b031e023..ecbd5c4b1d4 100644 --- a/src/pybind/mgr/dashboard_v2/restresource.py +++ b/src/pybind/mgr/dashboard_v2/restresource.py @@ -5,12 +5,14 @@ import cherrypy def _takes_json(func): def inner(*args, **kwargs): - body = cherrypy.request.body.read() + content_length = int(cherrypy.request.headers['Content-Length']) + body = cherrypy.request.body.read(content_length) + if not body: + raise cherrypy.HTTPError(400, 'Empty body. Content-Length={}'.format(content_length)) try: data = json.loads(body.decode('utf-8')) - except json.JSONDecodeError as e: - raise cherrypy.HTTPError(400, 'Failed to decode JSON: {}' - .format(str(e))) + except Exception as e: + raise cherrypy.HTTPError(400, 'Failed to decode JSON: {}'.format(str(e))) return func(data, *args, **kwargs) return inner -- 2.39.5