forked from mopidy/mopidy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
257 lines (183 loc) · 7.95 KB
/
test_server.py
File metadata and controls
257 lines (183 loc) · 7.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
from __future__ import absolute_import, unicode_literals
import os
import mock
import tornado.testing
import tornado.wsgi
import mopidy
from mopidy.http import actor, handlers
class HttpServerTest(tornado.testing.AsyncHTTPTestCase):
def get_config(self):
return {
'http': {
'hostname': '127.0.0.1',
'port': 6680,
'static_dir': None,
'zeroconf': '',
}
}
def get_app(self):
core = mock.Mock()
core.get_version = mock.MagicMock(name='get_version')
core.get_version.return_value = mopidy.__version__
testapps = [dict(name='testapp')]
teststatics = [dict(name='teststatic')]
apps = [{
'name': 'mopidy',
'factory': handlers.make_mopidy_app_factory(testapps, teststatics),
}]
http_server = actor.HttpServer(
config=self.get_config(), core=core, sockets=[],
apps=apps, statics=[])
return tornado.web.Application(http_server._get_request_handlers())
class RootRedirectTest(HttpServerTest):
def test_should_redirect_to_mopidy_app(self):
response = self.fetch('/', method='GET', follow_redirects=False)
self.assertEqual(response.code, 302)
self.assertEqual(response.headers['Location'], '/mopidy/')
class LegacyStaticDirAppTest(HttpServerTest):
def get_config(self):
config = super(LegacyStaticDirAppTest, self).get_config()
config['http']['static_dir'] = os.path.dirname(__file__)
return config
def test_should_return_index(self):
response = self.fetch('/', method='GET', follow_redirects=False)
self.assertEqual(response.code, 404, 'No index.html in this dir')
def test_should_return_static_files(self):
response = self.fetch('/test_server.py', method='GET')
self.assertIn(
'test_should_return_static_files',
tornado.escape.to_unicode(response.body))
self.assertEqual(
response.headers['X-Mopidy-Version'], mopidy.__version__)
self.assertEqual(response.headers['Cache-Control'], 'no-cache')
class MopidyAppTest(HttpServerTest):
def test_should_return_index(self):
response = self.fetch('/mopidy/', method='GET')
body = tornado.escape.to_unicode(response.body)
self.assertIn(
'This web server is a part of the Mopidy music server.', body)
self.assertIn('testapp', body)
self.assertIn('teststatic', body)
self.assertEqual(
response.headers['X-Mopidy-Version'], mopidy.__version__)
self.assertEqual(response.headers['Cache-Control'], 'no-cache')
def test_without_slash_should_redirect(self):
response = self.fetch('/mopidy', method='GET', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/mopidy/')
def test_should_return_static_files(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')
class MopidyWebSocketHandlerTest(HttpServerTest):
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))
class MopidyRPCHandlerTest(HttpServerTest):
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)
class HttpServerWithStaticFilesTest(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
config = {
'http': {
'hostname': '127.0.0.1',
'port': 6680,
'static_dir': None,
'zeroconf': '',
}
}
core = mock.Mock()
statics = [dict(name='static', path=os.path.dirname(__file__))]
http_server = actor.HttpServer(
config=config, core=core, sockets=[], apps=[], statics=statics)
return tornado.web.Application(http_server._get_request_handlers())
def test_without_slash_should_redirect(self):
response = self.fetch('/static', method='GET', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/static/')
def test_can_serve_static_files(self):
response = self.fetch('/static/test_server.py', method='GET')
self.assertEqual(200, response.code)
self.assertEqual(
response.headers['X-Mopidy-Version'], mopidy.__version__)
self.assertEqual(
response.headers['Cache-Control'], 'no-cache')
def wsgi_app_factory(config, core):
def wsgi_app(environ, start_response):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
start_response(status, response_headers)
return ['Hello, world!\n']
return [
('(.*)', tornado.web.FallbackHandler, {
'fallback': tornado.wsgi.WSGIContainer(wsgi_app),
}),
]
class HttpServerWithWsgiAppTest(tornado.testing.AsyncHTTPTestCase):
def get_app(self):
config = {
'http': {
'hostname': '127.0.0.1',
'port': 6680,
'static_dir': None,
'zeroconf': '',
}
}
core = mock.Mock()
apps = [{
'name': 'wsgi',
'factory': wsgi_app_factory,
}]
http_server = actor.HttpServer(
config=config, core=core, sockets=[], apps=apps, statics=[])
return tornado.web.Application(http_server._get_request_handlers())
def test_without_slash_should_redirect(self):
response = self.fetch('/wsgi', method='GET', follow_redirects=False)
self.assertEqual(response.code, 301)
self.assertEqual(response.headers['Location'], '/wsgi/')
def test_can_wrap_wsgi_apps(self):
response = self.fetch('/wsgi/', method='GET')
self.assertEqual(200, response.code)
self.assertIn(
'Hello, world!', tornado.escape.to_unicode(response.body))