Exemple #1
0
    def test_build_success(self):
        b1 = JsonResponseBuilder()

        mr1 = MockRequest()
        mr1.params = {
            'hoge': 'hogege'
        }
        v1 = {'mogera': 'moge'}

        result1 = b1.build_success(v1, mr1)
        eq_(result1.status_code, 200)
        eq_(result1.body, '{"status": "ok", "result": {"mogera": "moge"}}')
        eq_(result1.get_header('Content-Type'), 'application/json')
        eq_(result1.get_header('Pragma'), 'no-cache')
        eq_(result1.get_header('Cache-Control'), 'no-cache')

        mr2 = MockRequest()
        mr2.params = {
            'j': 'j123',
            'hoge': 'hogege'
        }
        v2 = {'a': 'bb'}
        result2 = b1.build_success(v2, mr2)
        eq_(result2.status_code, 200)
        eq_(result2.body, 'j123({"status": "ok", "result": {"a": "bb"}});')
        eq_(result2.get_header('Content-Type'), 'application/javascript')
        eq_(result2.get_header('Pragma'), 'no-cache')
        eq_(result2.get_header('Cache-Control'), 'no-cache')
Exemple #2
0
    def test_build_error(self):
        b1 = JsonResponseBuilder()

        mr1 = MockRequest()
        e1 = WebApiError('error1', status=500, result=None)
        result1 = b1.build_error(e1, mr1)
        eq_(result1.status_code, 500)
        eq_(result1.body, '{"status": "error", "message": "error1", "result": null}')
        eq_(result1.get_header('Content-Type'), 'application/json')
        eq_(result1.get_header('Pragma'), 'no-cache')
        eq_(result1.get_header('Cache-Control'), 'no-cache')

        mr2 = MockRequest()
        mr2.params = {
            'j': 'j234',
            'hoge': 'hogege'
        }
        e2 = WebApiError('error098', status=403, result='abcd')
        result2 = b1.build_error(e2, mr2)
        eq_(result2.status_code, 403)
        eq_(result2.body, 'j234({"status": "error", "message": "error098", "result": "abcd"});')
        eq_(result2.get_header('Content-Type'), 'application/javascript')
        eq_(result2.get_header('Pragma'), 'no-cache')
        eq_(result2.get_header('Cache-Control'), 'no-cache')