mopidy/tests/http/test_router.py
dz0ny f1d1a4713b Squashed commit of the following:
commit dbb7005aa866cdc337bde9c8169e9bf15e5c8042
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 11 22:12:58 2014 +0200

    Fix: Make PR mergable

commit 5bb27da72c4276a930bf33955e6583f6781d23f6
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Thu May 8 23:31:54 2014 +0200

    Add: helper method for extensin url

commit 8a348b26b65102084a606ff73384a478bb785cf1
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Thu May 8 00:35:50 2014 +0200

    Add: Refactor ws and rpc to handlers, reuse code

commit 677c809d2b39a6c982ab835368fdb8a3ad9d1a92
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Thu May 8 00:18:10 2014 +0200

    Fix: Return proper HTTP headers

commit fe5fea2fc2a0d28a39532d6d4cd2b21013d57d24
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Wed May 7 23:48:19 2014 +0200

    Add: RPC post handler
    Add: tests for http post handler

commit e77e60310853b368758b09b303a96a95ff1b9b93
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 4 22:15:04 2014 +0200

    Add: Documentation on how to extend http api

commit a3a14fb5d15f095e5bab23a590e0a8360a039f9a
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 4 19:48:34 2014 +0200

    Add: HTTP tests for default router and static handler

commit 0d9544256bcb8f048eaedb5cdd57b1de027d387b
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 4 15:44:32 2014 +0200

    Fix: Move StaticFileHandler to main http package

commit c83c9f661e658e4a843dc5c8c6ba5dc3f1ea9c1e
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 4 15:29:49 2014 +0200

    Add: default Router implementation

commit 258cb7210bdf13833884c04cfb7fb4fa704394a7
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sun May 4 15:00:46 2014 +0200

    Add: Switch to registry for router registration

commit b7bfe7b814235b030d7ac30de90e2331e3d809d3
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sat May 3 21:52:58 2014 +0200

    Fix: Private methods
    Fix: Point to mopidy.html instead main.html
    Fix: Less noise in console

commit 232abe3029e93f78ce25db0c1bd44743cc23ed2d
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sat May 3 21:32:07 2014 +0200

    Fix: Start IOLoop in separate thread, so actor can stop it

commit d686892c2fa993cbedc99c8e8e7f9c961ac6f35a
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sat May 3 19:30:49 2014 +0200

    Fix: Router load order
    Fix: JS helper library WSS default url
    Add: Handlers from extensions

commit a1b0f5673a6719f229df27feccb284324675e9d1
Author: dz0ny <dz0ny@ubuntu.si>
Date:   Sat May 3 14:53:30 2014 +0200

    Add: Switch to Tornado framework
2014-05-12 14:54:50 +02:00

198 lines
6.4 KiB
Python

from __future__ import unicode_literals
import os
import unittest
import mock
from mopidy import __version__, http
from tornado.escape import to_unicode
from tornado.testing import AsyncHTTPTestCase
from tornado.web import Application
try:
import tornado
except ImportError:
tornado = False
if tornado:
from mopidy.http import actor
class TestRouter(http.Router):
name = 'test'
path = os.path.join(os.path.dirname(__file__), 'static')
class TestRouterMissingPath(http.Router):
name = 'test'
class TestRouterMissingName(http.Router):
path = os.path.join(os.path.dirname(__file__), 'static')
@unittest.skipUnless(tornado, 'tornado is missing')
class HttpRouterTest(unittest.TestCase):
def setUp(self):
self.config = {
'http': {
'hostname': '127.0.0.1',
'port': 6680,
'static_dir': None,
'allow_draft76': True,
'zeroconf': '',
}
}
self.http = actor.HttpFrontend(config=self.config, core=mock.Mock())
def test_default_router(self):
router = TestRouter(self.config)
self.assertEqual(router.setup_routes()[0][0], r'/test/(.*)')
self.assertIs(router.setup_routes()[0][1], http.StaticFileHandler)
self.assertEqual(router.setup_routes()[0][2]['path'],
os.path.join(os.path.dirname(__file__), 'static'))
def test_default_router_missing_name(self):
with self.assertRaises(ValueError):
TestRouterMissingName(self.config)
def test_default_router_missing_path(self):
with self.assertRaises(ValueError):
TestRouterMissingPath(self.config).setup_routes()
def test_default_uri_helper(self):
router = TestRouter(self.config)
self.assertEqual('http://127.0.0.1:6680/test/', router.linkify())
class StaticFileHandlerTest(AsyncHTTPTestCase):
def get_app(self):
app = Application([(r'/(.*)', http.StaticFileHandler, {
'path': os.path.dirname(__file__),
'default_filename': 'test_router.py'
})])
return app
def test_static_handler(self):
response = self.fetch('/test_router.py', method='GET')
self.assertEqual(response.headers['X-Mopidy-Version'],
__version__)
self.assertEqual(response.headers['Cache-Control'],
'no-cache')
def test_static_default_filename(self):
response = self.fetch('/', method='GET')
self.assertEqual(response.headers['X-Mopidy-Version'],
__version__)
self.assertEqual(response.headers['Cache-Control'],
'no-cache')
class DefaultHTTPServerTest(AsyncHTTPTestCase):
def get_app(self):
config = {
'http': {
'hostname': '127.0.0.1',
'port': 6680,
'static_dir': None,
'zeroconf': '',
}
}
core = mock.Mock()
core.get_version = mock.MagicMock(name='get_version')
core.get_version.return_value = __version__
actor_http = actor.HttpFrontend(config=config, core=core)
return Application(actor_http._create_routes())
def test_root_should_return_index(self):
response = self.fetch('/', method='GET')
self.assertIn(
'Static content serving',
to_unicode(response.body)
)
self.assertEqual(response.headers['X-Mopidy-Version'],
__version__)
self.assertEqual(response.headers['Cache-Control'],
'no-cache')
def test_mopidy_should_return_index(self):
response = self.fetch('/mopidy/', method='GET')
self.assertIn(
'Here you can see events arriving from Mopidy in real time:',
to_unicode(response.body)
)
self.assertEqual(response.headers['X-Mopidy-Version'],
__version__)
self.assertEqual(response.headers['Cache-Control'],
'no-cache')
def test_should_return_js(self):
response = self.fetch('/mopidy/mopidy.js', method='GET')
self.assertIn(
'function Mopidy',
to_unicode(response.body)
)
self.assertEqual(response.headers['X-Mopidy-Version'],
__version__)
self.assertEqual(response.headers['Cache-Control'],
'no-cache')
def test_should_return_ws(self):
response = self.fetch('/mopidy/ws', method='GET')
self.assertEqual(
'Can "Upgrade" only to "WebSocket".',
to_unicode(response.body)
)
def test_should_return_ws_old(self):
response = self.fetch('/mopidy/ws/', method='GET')
self.assertEqual(
'Can "Upgrade" only to "WebSocket".',
to_unicode(response.body)
)
def test_should_return_rpc_error(self):
cmd = tornado.escape.json_encode({
'action': 'get_version'
})
response = self.fetch('/mopidy/rpc', method='POST', body=cmd)
self.assertEqual(
'{"jsonrpc": "2.0", "id": null, "error": '
'{"message": "Invalid Request", "code": -32600, '
'"data": "\\"jsonrpc\\" member must be included"}}',
to_unicode(response.body)
)
def test_should_return_parse_error(self):
cmd = "{[[[]}"
response = self.fetch('/mopidy/rpc', method='POST', body=cmd)
self.assertEqual(
'{"jsonrpc": "2.0", "id": null, "error": '
'{"message": "Parse error", "code": -32700}}',
to_unicode(response.body)
)
def test_should_return_mopidy_version(self):
cmd = tornado.escape.json_encode({
"method": "core.get_version",
"params": [],
"jsonrpc": "2.0",
"id": 1
})
response = self.fetch('/mopidy/rpc', method='POST', body=cmd)
self.assertEqual(
'{"jsonrpc": "2.0", "id": 1, "result": "%s"}' % __version__,
response.body
)
def test_should_return_extra_headers(self):
response = self.fetch('/mopidy/rpc', method='HEAD')
self.assertIn('Accept', response.headers)
self.assertIn('X-Mopidy-Version', response.headers)
self.assertIn('Cache-Control', response.headers)
self.assertIn('Content-Type', response.headers)