Ejemplo n.º 1
0
class TestRouter(TestCase):
    """
    Test for the Router class.
    """

    def setUp(self):
        """
        Creates a new instance to test with per test.
        """
        self.router_instance = Router()
        self.router_instance.connect(
            '/path/',
            controller='controller',
            conditions={'method': 'GET'})

    def test_router_successful_match(self):
        """
        Verify the Router finds connected paths.
        """
        self.assertTrue(self.router_instance.match('/path/'))

    def test_router_missing_match(self):
        """
        Verify the Router returns None on unsuccessful match.
        """
        self.assertIsNone(self.router_instance.match('/idonotexist/'))
Ejemplo n.º 2
0
 def setUp(self):
     """
     Creates a new instance to test with per test.
     """
     self.router_instance = Router()
     self.router_instance.connect(
         '/path/',
         controller='controller',
         conditions={'method': 'GET'})
Ejemplo n.º 3
0
 def setUp(self):
     """
     Creates a new instance to test with per test.
     """
     self.router_instance = Router()
     self.router_instance.connect(
         '/hello/',
         controller='commissaire_http.handlers.hello_world',
         conditions={'method': 'GET'})
     self.router_instance.connect(
         '/world/',
         controller='commissaire_http.handlers.create_world',
         conditions={'method': 'PUT'})
     self.dispatcher_instance = Dispatcher(
         self.router_instance,
         handler_packages=['commissaire_http.handlers']
     )
Ejemplo n.º 4
0
class TestDispatcher(TestCase):
    """
    Test for the Dispatcher class.
    """

    def setUp(self):
        """
        Creates a new instance to test with per test.
        """
        self.router_instance = Router()
        self.router_instance.connect(
            '/hello/',
            controller='commissaire_http.handlers.hello_world',
            conditions={'method': 'GET'})
        self.router_instance.connect(
            '/world/',
            controller='commissaire_http.handlers.create_world',
            conditions={'method': 'PUT'})
        self.dispatcher_instance = Dispatcher(
            self.router_instance,
            handler_packages=['commissaire_http.handlers']
        )

    def test_dispatcher_initialization(self):
        """
        Verify the Dispatcher initializes properly.
        """
        self.assertEquals(
            self.router_instance, self.dispatcher_instance._router)
        self.assertTrue(self.dispatcher_instance._handler_map)

    def test_dispatcher_reload_handlers(self):
        """
        Verify the Dispatcher.reload_handlers actually loads handlers.
        """
        self.dispatcher_instance._handler_map = {}
        self.assertFalse(self.dispatcher_instance._handler_map)
        self.dispatcher_instance.reload_handlers()
        self.assertTrue(self.dispatcher_instance._handler_map)

    def test_dispatcher_dispatch_with_valid_path(self):
        """
        Verify the Dispatcher.dispatch works with valid paths.
        """
        environ = {
            'PATH_INFO': '/hello/',
            'REQUEST_METHOD': 'GET',
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('200 OK', mock.ANY)
        self.assertEquals('{"Hello": "there"}', result[0].decode())

    def test_dispatcher_dispatch_with_valid_path_and_params(self):
        """
        Verify the Dispatcher.dispatch works with valid paths and params.
        """
        environ = {
            'PATH_INFO': '/hello/',
            'REQUEST_METHOD': 'GET',
            'QUERY_STRING': 'name=bob'
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('200 OK', mock.ANY)
        self.assertEquals('{"Hello": "bob"}', result[0].decode())

    def test_dispatcher_dispatch_with_valid_path_with_wsgi_input(self):
        """
        Verify the Dispatcher.dispatch works when wsgi_input is in use.
        """
        environ = {
            'PATH_INFO': '/world/',
            'REQUEST_METHOD': 'PUT',  # PUT uses wsgi.input
            'wsgi.input': BytesIO(b'{"name": "world"}'),
            'CONTENT_LENGTH': b'18',
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('200 OK', mock.ANY)
        self.assertEquals('world', json.loads(result[0].decode())['name'])

    def test_dispatcher_dispatch_with_invalid_path(self):
        """
        Verify the Dispatcher.dispatch works with invalid paths.
        """
        environ = {
            'PATH_INFO': '/idonotexist/',
            'REQUEST_METHOD': 'GET',
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('404 Not Found', mock.ANY)
        self.assertEquals('Not Found', result[0].decode())

    def test_dispatcher__get_params_with_uri_param(self):
        """
        Verify Dispacher._get_params returns proper parameters from a uri.
        """
        environ = {
            'PATH_INFO': '/test/',
            'REQUEST_METHOD': 'GET',
        }
        route = {
            'test': 'test',
            'controller': 'testing',
        }
        route_data = mock.MagicMock(minkeys=['test'])
        self.assertEquals(
            {'test': 'test'},
            self.dispatcher_instance._get_params(environ, route, route_data))

    def test_dispatcher__get_params_with_query_string(self):
        """
        Verify Dispacher._get_params returns proper parameters from a query string.
        """
        environ = {
            'PATH_INFO': '/test/',
            'QUERY_STRING': 'from=querystring',
            'REQUEST_METHOD': 'GET',
        }
        route = {
            'controller': 'testing',
        }
        route_data = mock.MagicMock(minkeys=[])
        self.assertEquals(
            {'from': 'querystring'},
            self.dispatcher_instance._get_params(environ, route, route_data))

    def test_dispatcher__get_params_with_wsgi_input(self):
        """
        Verify Dispacher._get_params returns proper parameters from wsgi.input.
        """
        environ = {
            'PATH_INFO': '/test/',
            'REQUEST_METHOD': 'PUT',
            'CONTENT_LENGTH': 16,
            'wsgi.input': BytesIO(b'{"from": "wsgi"}')
        }
        route = {
            'controller': 'testing',
        }
        route_data = mock.MagicMock(minkeys=[])
        self.assertEquals(
            {'from': 'wsgi'},
            self.dispatcher_instance._get_params(environ, route, route_data))
class TestDispatcher(TestCase):
    """
    Test for the Dispatcher class.
    """
    def setUp(self):
        """
        Creates a new instance to test with per test.
        """
        self.router_instance = Router()
        self.router_instance.connect(
            '/hello/',
            controller='commissaire_http.handlers.hello_world',
            conditions={'method': 'GET'})
        self.router_instance.connect(
            '/world/',
            controller='commissaire_http.handlers.create_world',
            conditions={'method': 'PUT'})
        self.dispatcher_instance = Dispatcher(
            self.router_instance,
            handler_packages=['commissaire_http.handlers'])
        self.dispatcher_instance._bus = mock.MagicMock('Bus')

    def test_dispatcher_initialization(self):
        """
        Verify the Dispatcher initializes properly.
        """
        self.assertEquals(self.router_instance,
                          self.dispatcher_instance._router)
        self.assertTrue(self.dispatcher_instance._handler_map)

    def test_dispatcher_reload_handlers(self):
        """
        Verify the Dispatcher.reload_handlers actually loads handlers.
        """
        self.dispatcher_instance._handler_map = {}
        self.assertFalse(self.dispatcher_instance._handler_map)
        self.dispatcher_instance.reload_handlers()
        self.assertTrue(self.dispatcher_instance._handler_map)

    def test_dispatcher_dispatch_with_valid_path(self):
        """
        Verify the Dispatcher.dispatch works with valid paths.
        """
        match = {'controller': commissaire_http.handlers.hello_world}
        route = mock.MagicMock(minkeys=[])
        environ = {
            'PATH_INFO': '/hello/',
            'REQUEST_METHOD': 'GET',

            # RoutesMiddleware inserts this.
            'wsgiorg.routing_args': ((), match),
            'routes.route': route
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('200 OK', mock.ANY)
        self.assertEquals('{"Hello": "there"}', result[0].decode())

    def test_dispatcher_dispatch_with_valid_path_and_params(self):
        """
        Verify the Dispatcher.dispatch works with valid paths and params.
        """
        match = {'controller': commissaire_http.handlers.hello_world}
        route = mock.MagicMock(minkeys=[])
        environ = {
            'PATH_INFO': '/hello/',
            'REQUEST_METHOD': 'GET',
            'QUERY_STRING': 'name=bob',

            # RoutesMiddleware inserts this.
            'wsgiorg.routing_args': ((), match),
            'routes.route': route
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('200 OK', mock.ANY)
        self.assertEquals('{"Hello": "bob"}', result[0].decode())

    def test_dispatcher_dispatch_with_valid_path_with_wsgi_input(self):
        """
        Verify the Dispatcher.dispatch works when wsgi_input is in use.
        """
        match = {'controller': commissaire_http.handlers.create_world}
        route = mock.MagicMock(minkeys=[])
        environ = {
            'PATH_INFO': '/world/',
            'REQUEST_METHOD': 'PUT',  # PUT uses wsgi.input
            'wsgi.input': BytesIO(b'{"name": "world"}'),
            'CONTENT_LENGTH': b'18',

            # RoutesMiddleware inserts this.
            'wsgiorg.routing_args': ((), match),
            'routes.route': route
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('201 Created', mock.ANY)
        self.assertEquals('world', json.loads(result[0].decode())['name'])

    def test_dispatcher_dispatch_with_invalid_path(self):
        """
        Verify the Dispatcher.dispatch works with invalid paths.
        """
        environ = {
            'PATH_INFO': '/idonotexist/',
            'REQUEST_METHOD': 'GET',

            # RoutesMiddleware inserts this.
            'routes.route': None
        }
        start_response = mock.MagicMock()
        result = self.dispatcher_instance.dispatch(environ, start_response)
        start_response.assert_called_once_with('404 Not Found', mock.ANY)
        self.assertEquals('Not Found', result[0].decode())
Ejemplo n.º 6
0
from commissaire_http.router import Router
from commissaire_http import CommissaireHttpServer

# NOTE: Only added for this example
for name in ('Dispatcher', 'Router', 'Bus', 'CommissaireHttpServer'):
    logger = logging.getLogger(name)
    logger.setLevel(logging.DEBUG)
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter(
        '%(name)s(%(levelname)s): %(message)s'))
    logger.handlers.append(handler)
# --


# Make a router that takes in "handler"s.
mapper = Router()
mapper.connect(
    R'/hello/',
    controller='commissaire_http.handlers.hello_world',
    conditions={'method': 'GET'})
mapper.connect(
    R'/world/',
    controller='commissaire_http.handlers.create_world',
    conditions={'method': 'PUT'})
mapper.connect(
    R'/hello_class/',
    controller='commissaire_http.handlers.ClassHandlerExample.hello',
    conditions={'method': 'GET'})
mapper.connect(
    R'/api/v0/clusters/',
    controller='commissaire_http.handlers.clusters.list_clusters',
Ejemplo n.º 7
0
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
Routing items.
"""

from commissaire_http.dispatcher import Dispatcher
from commissaire_http.router import Router

from commissaire_http.handlers import (clusters, hosts, networks,
                                       container_managers)
from commissaire_http.handlers.clusters import operations

#: Global HTTP router for the dispatcher
ROUTER = Router(optional_slash=True)
hosts._register(ROUTER)
clusters._register(ROUTER)
networks._register(ROUTER)
operations._register(ROUTER)
container_managers._register(ROUTER)

#: Global HTTP dispatcher for the server
DISPATCHER = Dispatcher(ROUTER,
                        handler_packages=[
                            'commissaire_http.handlers',
                            'commissaire_http.handlers.clusters',
                            'commissaire_http.handlers.container_managers',
                            'commissaire_http.handlers.clusters.operations',
                            'commissaire_http.handlers.networks',
                            'commissaire_http.handlers.hosts'