From 5897675eab831b3e974296636c18a627b5821546 Mon Sep 17 00:00:00 2001 From: Stein Magnus Jodal Date: Tue, 20 May 2014 23:05:11 +0200 Subject: [PATCH] http: Split tests into three files --- tests/http/test_handlers.py | 35 +++++++++ tests/http/test_router.py | 139 +----------------------------------- tests/http/test_server.py | 113 +++++++++++++++++++++++++++++ 3 files changed, 150 insertions(+), 137 deletions(-) create mode 100644 tests/http/test_handlers.py create mode 100644 tests/http/test_server.py diff --git a/tests/http/test_handlers.py b/tests/http/test_handlers.py new file mode 100644 index 00000000..be7d67e5 --- /dev/null +++ b/tests/http/test_handlers.py @@ -0,0 +1,35 @@ +from __future__ import unicode_literals + +import os + +import tornado.testing +import tornado.web + +import mopidy +from mopidy.http import handlers + + +class StaticFileHandlerTest(tornado.testing.AsyncHTTPTestCase): + def get_app(self): + return tornado.web.Application([ + (r'/(.*)', handlers.StaticFileHandler, { + 'path': os.path.dirname(__file__), + 'default_filename': 'test_router.py' + }) + ]) + + def test_static_handler(self): + response = self.fetch('/test_router.py', method='GET') + + self.assertEqual( + response.headers['X-Mopidy-Version'], mopidy.__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'], mopidy.__version__) + self.assertEqual( + response.headers['Cache-Control'], 'no-cache') diff --git a/tests/http/test_router.py b/tests/http/test_router.py index e8856b84..4b27f91d 100644 --- a/tests/http/test_router.py +++ b/tests/http/test_router.py @@ -5,11 +5,6 @@ import unittest import mock -import tornado.escape -import tornado.testing -import tornado.web - -import mopidy from mopidy import http from mopidy.http import actor, handlers @@ -57,137 +52,7 @@ class HttpRouterTest(unittest.TestCase): with self.assertRaises(ValueError): TestRouterMissingPath(self.config).get_request_handlers() - def test_default_uri_helper(self): + def test_get_root_url(self): router = TestRouter(self.config) + self.assertEqual('http://127.0.0.1:6680/test/', router.get_root_url()) - - -class StaticFileHandlerTest(tornado.testing.AsyncHTTPTestCase): - def get_app(self): - app = tornado.web.Application([ - (r'/(.*)', handlers.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'], - mopidy.__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'], - mopidy.__version__) - self.assertEqual(response.headers['Cache-Control'], - 'no-cache') - - -class DefaultHTTPServerTest(tornado.testing.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 = mopidy.__version__ - - actor_http = actor.HttpFrontend(config=config, core=core) - return tornado.web.Application(actor_http._get_request_handlers()) - - def test_root_should_return_index(self): - response = self.fetch('/', method='GET') - self.assertIn( - 'Static content serving', - tornado.escape.to_unicode(response.body) - ) - self.assertEqual(response.headers['X-Mopidy-Version'], - mopidy.__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:', - tornado.escape.to_unicode(response.body) - ) - self.assertEqual(response.headers['X-Mopidy-Version'], - mopidy.__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', - tornado.escape.to_unicode(response.body) - ) - self.assertEqual(response.headers['X-Mopidy-Version'], - mopidy.__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".', - tornado.escape.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".', - tornado.escape.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': None, 'error': - {'message': 'Invalid Request', 'code': -32600, - 'data': '"jsonrpc" member must be included'}}, - tornado.escape.json_decode(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': None, 'error': - {'message': 'Parse error', 'code': -32700}}, - tornado.escape.json_decode(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': mopidy.__version__}, - tornado.escape.json_decode(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) diff --git a/tests/http/test_server.py b/tests/http/test_server.py new file mode 100644 index 00000000..a80cefe5 --- /dev/null +++ b/tests/http/test_server.py @@ -0,0 +1,113 @@ +from __future__ import unicode_literals + +import mock + +import tornado.testing + +import mopidy +from mopidy.http import actor + + +class HttpServerTest(tornado.testing.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 = mopidy.__version__ + + actor_http = actor.HttpFrontend(config=config, core=core) + return tornado.web.Application(actor_http._get_request_handlers()) + + def test_root_should_return_index(self): + response = self.fetch('/', method='GET') + + self.assertIn( + 'Static content serving', + tornado.escape.to_unicode(response.body)) + self.assertEqual( + response.headers['X-Mopidy-Version'], mopidy.__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:', + tornado.escape.to_unicode(response.body)) + self.assertEqual( + response.headers['X-Mopidy-Version'], mopidy.__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', + tornado.escape.to_unicode(response.body)) + self.assertEqual( + response.headers['X-Mopidy-Version'], mopidy.__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".', + tornado.escape.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".', + tornado.escape.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': None, 'error': + {'message': 'Invalid Request', 'code': -32600, + 'data': '"jsonrpc" member must be included'}}, + tornado.escape.json_decode(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': None, 'error': + {'message': 'Parse error', 'code': -32700}}, + tornado.escape.json_decode(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': mopidy.__version__}, + tornado.escape.json_decode(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)