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 ('This script requires CherryPy v3 or higher to be installed.\n' |
| 24 'Please install 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 self.keytest_succeeded = True |
| 48 self.keytest_text = text |
| 49 |
| 50 def process(self): |
| 51 """Build the JSON message that will be conveyed to the client.""" |
| 52 message = { |
| 53 'keypressSucceeded': self.keytest_succeeded, |
| 54 'keypressText': self.keytest_text |
| 55 } |
| 56 |
| 57 # The message is now built so reset state on the server |
| 58 if self.keytest_succeeded: |
| 59 self.keytest_succeeded = False |
| 60 self.keytest_text = None |
| 61 |
| 62 return message |
| 63 |
| 64 |
| 65 class Root(object): |
| 66 """Root Handler for the server.""" |
| 67 |
| 68 # Every test has its own class which should be instantiated here |
| 69 keytest = KeyTest() |
| 70 |
| 71 # Every test's class should have a process method that the client polling |
| 72 # will call when that test is running. |
| 73 # The method should be registered here with the test name. |
| 74 TEST_DICT = { |
| 75 'keytest': keytest.process |
| 76 } |
| 77 |
| 78 @cherrypy.expose |
| 79 @cherrypy.tools.allow() |
| 80 def index(self): |
| 81 """Index page to test if server is ready.""" |
| 82 return 'Simple HTTP Server for Chromoting Browser Tests!' |
| 83 |
| 84 @cherrypy.expose |
| 85 @cherrypy.tools.allow() |
| 86 def poll(self, test): |
| 87 """Responds to poll request from client page with status of host actions.""" |
| 88 if test not in self.TEST_DICT: |
| 89 cherrypy.response.status = 500 |
| 90 return |
| 91 |
| 92 cherrypy.response.headers['Content-Type'] = 'application/json' |
| 93 return json.dumps(self.TEST_DICT[test]()) |
| 94 |
| 95 |
| 96 app_config = { |
| 97 '/': { |
| 98 'tools.staticdir.on': True, |
| 99 'tools.staticdir.dir': |
| 100 os.path.abspath(os.path.dirname(__file__)) |
| 101 } |
| 102 } |
| 103 cherrypy.tree.mount(Root(), '/', config=app_config) |
| 104 cherrypy.config.update({'server.socket_host': '0.0.0.0', |
| 105 'server.threadpool': 1, |
| 106 }) |
| 107 cherrypy.engine.start() |
| 108 cherrypy.engine.block() |
OLD | NEW |