1
from ivle.webapp.base.views import RESTView, JSONRESTView
2
from ivle.webapp.errors import BadRequest
3
from ivle.webapp.testing import FakeUser, FakeRequest
5
class JSONRESTViewTest(JSONRESTView):
6
'''A small JSON REST view for testing purposes.'''
8
return {'method': 'get'}
10
def PUT(self, req, data):
11
return {'method': 'put',
12
'result': data['result'], 'test': data['test']}
14
def PATCH(self, req, data):
15
return {'method': 'patch',
16
'result': data['result'], 'test': data['test']}
18
class TestJSONRESTView:
21
view = JSONRESTViewTest(req)
23
assert req.content_type == 'application/json'
24
assert req.response_body == '{"method": "get"}\n'
29
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
30
view = JSONRESTViewTest(req)
32
assert req.content_type == 'application/json'
33
assert req.response_body == \
34
'{"test": "FAI\\ua746ED", "method": "put", "result": 1}\n'
39
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
40
view = JSONRESTViewTest(req)
42
assert req.content_type == 'application/json'
43
assert req.response_body == \
44
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
46
def testEmulatedPATCH(self):
49
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
50
req.request_body = '{"test": "FAI\\uA746ED", "result": 1}'
51
view = JSONRESTViewTest(req)
53
assert req.content_type == 'application/json'
54
assert req.response_body == \
55
'{"test": "FAI\\ua746ED", "method": "patch", "result": 1}\n'
57
def testInvalidMethod(self):
59
req.method = 'FAKEANDBOGUS'
60
view = JSONRESTViewTest(req)
66
raise AssertionError("did not raise BadRequest")
68
def testInvalidMethodWithPATCHEmulation(self):
70
req.method = 'FAKEANDBOGUS'
71
req.headers_in['X-IVLE-Patch-Semantics'] = 'yes'
72
view = JSONRESTViewTest(req)
78
raise AssertionError("did not raise BadRequest")