http: Split tests into three files

This commit is contained in:
Stein Magnus Jodal 2014-05-20 23:05:11 +02:00
parent e9ff16fe63
commit 5897675eab
3 changed files with 150 additions and 137 deletions

View File

@ -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')

View File

@ -5,11 +5,6 @@ import unittest
import mock import mock
import tornado.escape
import tornado.testing
import tornado.web
import mopidy
from mopidy import http from mopidy import http
from mopidy.http import actor, handlers from mopidy.http import actor, handlers
@ -57,137 +52,7 @@ class HttpRouterTest(unittest.TestCase):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
TestRouterMissingPath(self.config).get_request_handlers() TestRouterMissingPath(self.config).get_request_handlers()
def test_default_uri_helper(self): def test_get_root_url(self):
router = TestRouter(self.config) router = TestRouter(self.config)
self.assertEqual('http://127.0.0.1:6680/test/', router.get_root_url()) 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)

113
tests/http/test_server.py Normal file
View File

@ -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)