~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to ivle/webapp/base/rest.py

ivle.webapp.base.rest#JSONRESTView: Check for bad JSON input, rather than
    crashing!
ivle.webapp.base.test.test_rest: Test responses to invalid POST, PATCH and
    PUT inputs.

Show diffs side-by-side

added added

removed removed

Lines of Context:
60
60
        elif req.method == 'PATCH' or (req.method == 'PUT' and
61
61
              'X-IVLE-Patch-Semantics' in req.headers_in and
62
62
              req.headers_in['X-IVLE-Patch-Semantics'].lower() == 'yes'):
63
 
            outjson = self.PATCH(req, cjson.decode(req.read()))
 
63
            try:
 
64
                input = cjson.decode(req.read())
 
65
            except cjson.DecodeError:
 
66
                raise BadRequest('Invalid JSON data')
 
67
            outjson = self.PATCH(req, input)
64
68
        elif req.method == 'PUT':
65
 
            outjson = self.PUT(req, cjson.decode(req.read()))
 
69
            try:
 
70
                input = cjson.decode(req.read())
 
71
            except cjson.DecodeError:
 
72
                raise BadRequest('Invalid JSON data')
 
73
            outjson = self.PUT(req, input)
66
74
        # POST implies named operation.
67
75
        elif req.method == 'POST':
68
76
            # TODO: Check Content-Type and implement multipart/form-data.