pylons / tests / test_units / test_jsonrpc.py

# -*- coding: utf-8 -*-
from paste.fixture import TestApp
from paste.registry import RegistryManager

import webob.exc as exc
import json

from __init__ import TestWSGIController

def make_basejsonrpc():
    from pylons.controllers import JSONRPCController, JSONRPCError

    class BaseJSONRPCController(JSONRPCController):

        def __init__(self):
            self._pylons_log_debug = True

        def echo(self, message):
            return message

        def int_arg_check(self, arg):
            if not isinstance(arg, int):
                raise JSONRPCError(1, 'That is not an integer')
            else:
                return 'got an integer'

        def return_garbage(self):
            return JSONRPCController

        def _private(self):
            return 'private method'

    return BaseJSONRPCController


class TestJSONRPCController(TestWSGIController):

    def __init__(self, *args, **kwargs):
        from pylons.testutil import ControllerWrap, SetupCacheGlobal

        BaseJSONRPCController = make_basejsonrpc()
        TestWSGIController.__init__(self, *args, **kwargs)
        self.baseenviron = {}
        self.baseenviron['pylons.routes_dict'] = {}
        app = ControllerWrap(BaseJSONRPCController)
        app = self.sap = SetupCacheGlobal(app, self.baseenviron)
        app = RegistryManager(app)
        self.app = TestApp(app)

    def test_echo(self):
        response = self.jsonreq('echo', args=('hello, world',))
        assert dict(id='test',
                    result='hello, world') == response

    def test_int_arg_check(self):
        response = self.jsonreq('int_arg_check', args=('1',))
        assert dict(id='test',
                    error={'code': 1,
                           'message': 'That is not an integer'}) == response

    def test_return_garbage(self):
        response = self.jsonreq('return_garbage')
        assert dict(id='test',
                    error={'code': -32603,
                           'message': "Internal error"}) == response

    def test_private_method(self):
        response = self.jsonreq('_private')
        assert dict(id='test',
                    error={'code': -32601,
                           'message': "Method not found"}) == response

    def test_content_type(self):
        response = self.jsonreq('echo', args=('foo',))
        assert self.response.header('Content-Type') == 'application/json'

    def test_missing_method(self):
        response = self.jsonreq('foo')
        assert dict(id='test',
                    error={'code': -32601,
                           'message': "Method not found"}) == response

    def test_no_content_length(self):
        data = json.dumps(dict(id='test',
                               method='echo',
                               args=('foo',)))
        self.assertRaises(exc.HTTPLengthRequired,
                          lambda: self.app.post('/', extra_environ=\
                                                    dict(CONTENT_LENGTH='')))

    def test_zero_content_length(self):
        data = json.dumps(dict(id='test',
                               method='echo',
                               args=('foo',)))
        self.assertRaises(exc.HTTPLengthRequired,
                          lambda: self.app.post('/', extra_environ=\
                                                    dict(CONTENT_LENGTH='0')))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.