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

« back to all changes in this revision

Viewing changes to ivle/webapp/base/test/test_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:
221
221
        view.render(req)
222
222
        assert req.content_type == 'application/json'
223
223
        assert req.response_body == '{"method": "POST"}\n'
 
224
 
 
225
    def testInvalidPOSTData(self):
 
226
        req = FakeRequest()
 
227
        req.method = 'POST'
 
228
        req.request_body = 'I am invalid&&&&'
 
229
        view = JSONRESTViewTest(req)
 
230
        try:
 
231
            view.render(req)
 
232
        except BadRequest, e:
 
233
            print e.message
 
234
            assert e.message == 'No named operation specified.'
 
235
        else:
 
236
            raise AssertionError("did not raise BadRequest")
 
237
 
 
238
    def testInvalidPATCHData(self):
 
239
        req = FakeRequest()
 
240
        req.method = 'PATCH'
 
241
        req.request_body = 'I am invalid'
 
242
        view = JSONRESTViewTest(req)
 
243
        try:
 
244
            view.render(req)
 
245
        except BadRequest, e:
 
246
            assert e.message == 'Invalid JSON data'
 
247
        else:
 
248
            raise AssertionError("did not raise BadRequest")
 
249
 
 
250
    def testInvalidPUTData(self):
 
251
        req = FakeRequest()
 
252
        req.method = 'PUT'
 
253
        req.request_body = 'I am invalid'
 
254
        view = JSONRESTViewTest(req)
 
255
        try:
 
256
            view.render(req)
 
257
        except BadRequest, e:
 
258
            assert e.message == 'Invalid JSON data'
 
259
        else:
 
260
            raise AssertionError("did not raise BadRequest")