OLD | NEW |
---|---|
(Empty) | |
1 # Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 """Simple HTTP server used in Chromoting End-to-end tests. | |
6 | |
7 Serves the static host and client pages with associated javascript files. | |
8 Stores state about host actions and communicates it to the client. | |
9 | |
10 Built on CherryPy (http://www.cherrypy.org/) and requires the Chromium | |
11 version of CherryPy to be installed from | |
12 chromium/tools/build/third_party/cherrypy/. | |
13 | |
14 """ | |
15 | |
16 import json | |
17 import os | |
18 import sys | |
19 | |
20 try: | |
21 import cherrypy | |
22 except ImportError: | |
23 print ('CNS requires CherryPy v3 or higher to be installed. Please install\n' | |
Jamie
2014/03/04 21:40:35
What is CNS? Maybe just "This script requires..."?
chaitali
2014/03/05 22:02:22
Done.
| |
24 'and try again.') | |
25 sys.exit(1) | |
26 | |
27 | |
28 def HttpMethodsAllowed(methods=['GET', 'HEAD']): | |
29 method = cherrypy.request.method.upper() | |
30 if method not in methods: | |
31 cherrypy.response.headers['Allow'] = ', '.join(methods) | |
32 raise cherrypy.HTTPError(405) | |
33 | |
34 cherrypy.tools.allow = cherrypy.Tool('on_start_resource', HttpMethodsAllowed) | |
35 | |
36 | |
37 class KeyTest(object): | |
38 """Handler for keyboard test in Chromoting E2E tests.""" | |
39 | |
40 keytest_succeeded = False | |
41 keytest_text = None | |
42 | |
43 @cherrypy.expose | |
44 @cherrypy.tools.allow(methods=['POST']) | |
45 def test(self, text): | |
46 """Stores status of host keyboard actions.""" | |
47 | |
Jamie
2014/03/04 21:40:35
I think these blank lines at the start of the meth
chaitali
2014/03/05 22:02:22
Done.
| |
48 self.keytest_succeeded = True | |
49 self.keytest_text = text | |
50 | |
51 def process(self): | |
52 """Build the JSON message that will be conveyed to the client."""\ | |
Jamie
2014/03/04 21:40:35
Nit: Stray backslash?
chaitali
2014/03/05 22:02:22
Done.
| |
53 | |
54 message = {} | |
55 message['keypressSucceeded'] = self.keytest_succeeded | |
56 message['keypressText'] = self.keytest_text | |
Jamie
2014/03/04 21:40:35
I think this can be more succinctly declared simil
chaitali
2014/03/05 22:02:22
Done.
| |
57 | |
58 # The message is now built so reset state on the server | |
59 if self.keytest_succeeded: | |
Jamie
2014/03/04 21:40:35
Is this server single-threaded? If not, then you'l
chaitali
2014/03/05 22:02:22
Done. Added server.threadpool = 1 in config below.
| |
60 self.keytest_succeeded = False | |
61 self.keytest_text = None | |
62 | |
63 return message | |
64 | |
65 | |
66 class Root(object): | |
67 """Root Handler for the server.""" | |
68 | |
69 # Every test has its own class which should be instantiated here | |
70 keytest = KeyTest() | |
71 | |
72 # Every test's class should have a process method that the client polling | |
73 # will call when that test is running. | |
74 # The method should be registered here with the test name. | |
75 TEST_DICT = { | |
76 'keytest': keytest.process | |
77 } | |
78 | |
79 @cherrypy.expose | |
80 @cherrypy.tools.allow() | |
81 def index(self): | |
82 """Index page to test if server is ready.""" | |
83 | |
84 return 'Simple HTTP Server for Chromoting Browser Tests!' | |
85 | |
86 @cherrypy.expose | |
87 @cherrypy.tools.allow() | |
88 def poll(self, test): | |
89 """Responds to poll request from client page with status of host actions.""" | |
90 | |
91 if test not in self.TEST_DICT: | |
92 cherrypy.response.status = 500 | |
93 return | |
94 | |
95 cherrypy.response.headers['Content-Type'] = 'application/json' | |
96 return json.dumps(self.TEST_DICT[test]()) | |
97 | |
98 | |
99 app_config = { | |
100 '/static': { | |
101 'tools.staticdir.on': True, | |
102 'tools.staticdir.dir': | |
103 os.path.abspath(os.path.dirname(__file__)) | |
104 } | |
105 } | |
106 cherrypy.tree.mount(Root(), '/', config=app_config) | |
107 cherrypy.config.update({'server.socket_host': '0.0.0.0', | |
108 }) | |
109 cherrypy.engine.start() | |
110 cherrypy.engine.block() | |
OLD | NEW |