diff --git a/mopidy/http/__init__.py b/mopidy/http/__init__.py index 0e7bb1ce..24729a6e 100644 --- a/mopidy/http/__init__.py +++ b/mopidy/http/__init__.py @@ -50,6 +50,8 @@ class Router(object): extension registry under the ``http:router`` key. :param config: dict structure of the entire Mopidy configuration + :param core: :class:`pykka.ActorProxy` to the core actor, giving full + access to the core API """ name = None @@ -74,8 +76,9 @@ class Router(object): If you override :meth:`get_request_handlers` this attribute is not used. """ - def __init__(self, config): + def __init__(self, config, core): self.config = config + self.core = core self.hostname = config['http']['hostname'] self.port = config['http']['port'] if not self.name: diff --git a/mopidy/http/actor.py b/mopidy/http/actor.py index 01feced2..63f6cfd4 100644 --- a/mopidy/http/actor.py +++ b/mopidy/http/actor.py @@ -90,7 +90,7 @@ class HttpFrontend(pykka.ThreadingActor, CoreListener): def _get_extension_request_handlers(self): request_handlers = [] for router_class in self.routers: - router = router_class(self.config) + router = router_class(self.config, self.core) request_handlers.extend(router.get_request_handlers()) logger.info( 'Loaded HTTP extension: %s', router_class.__name__) diff --git a/tests/http/test_router.py b/tests/http/test_router.py index 4b27f91d..2e24c1bf 100644 --- a/tests/http/test_router.py +++ b/tests/http/test_router.py @@ -6,7 +6,7 @@ import unittest import mock from mopidy import http -from mopidy.http import actor, handlers +from mopidy.http import handlers class TestRouter(http.Router): @@ -32,10 +32,16 @@ class HttpRouterTest(unittest.TestCase): 'zeroconf': '', } } - self.http = actor.HttpFrontend(config=self.config, core=mock.Mock()) + self.core = mock.Mock() - def test_default_router(self): - router = TestRouter(self.config) + def test_keeps_reference_to_config_and_core(self): + router = TestRouter(self.config, self.core) + + self.assertIs(router.config, self.config) + self.assertIs(router.core, self.core) + + def test_default_request_handlers(self): + router = TestRouter(self.config, self.core) (pattern, handler_class, kwargs) = router.get_request_handlers()[0] @@ -46,13 +52,15 @@ class HttpRouterTest(unittest.TestCase): def test_default_router_missing_name(self): with self.assertRaises(ValueError): - TestRouterMissingName(self.config) + TestRouterMissingName(self.config, self.core) def test_default_router_missing_path(self): + router = TestRouterMissingPath(self.config, self.core) + with self.assertRaises(ValueError): - TestRouterMissingPath(self.config).get_request_handlers() + router.get_request_handlers() def test_get_root_url(self): - router = TestRouter(self.config) + router = TestRouter(self.config, self.core) self.assertEqual('http://127.0.0.1:6680/test/', router.get_root_url())