Commit bbcae7c3 authored by chaitali@chromium.org's avatar chaitali@chromium.org

Simple HTTP server for Chromoting End-to-End tests

BUG=341526

Review URL: https://codereview.chromium.org/180273015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@255771 0039d316-1c4b-4281-b951-d872f2087c98
parent ce9a50f3
<!doctype html>
<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>
<head>
<title>Client Page</title>
<script src="clientpage.js"></script>
</head>
<body>
</body>
</html>
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Set some global variables for the browsertest to pick up
var keyTestNamespace = {
keypressSucceeded: false,
keypressText: ''
};
/**
* Method to make an XHR call to the server for status
*/
// TODO(chaitali): Update this method to poll and get status of all
// test vars in each poll and stop when all are true.
function poll() {
var request = new XMLHttpRequest();
request.open('GET', 'poll?test=keytest', true);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
console.log('Polling status : ' + request.responseText);
var data;
try {
data = JSON.parse(request.responseText);
} catch (err) {
console.log('Could not parse server response.');
return;
}
// If keypress succeeded then
// update relevant vars and stop polling.
if (data.keypressSucceeded == true) {
keyTestNamespace.keypressSucceeded = data.keypressSucceeded;
keyTestNamespace.keypressText = data.keypressText;
} else {
// If keypress did not succeed we should
// continue polling.
setTimeout(poll, 1000);
}
}
};
request.onerror = function() {
console.log('Polling failed');
};
request.send();
}
window.addEventListener(
'load',
poll.bind(null),
false);
<!doctype html>
<!--
Copyright 2014 The Chromium Authors. All rights reserved.
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->
<html>
<head>
<title>Host Page</title>
<script src="hostpage.js"></script>
</head>
<body>
<center>
<textarea id="testtext" rows="10" cols="50" tabindex="1"></textarea>
</center>
</body>
</html>
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* Keypress handler for the textarea. Sends the textarea value
* to the HTTP server when "Enter" key is pressed.
* @param {Event} event The keypress event.
*/
function handleTextareaKeyPressed(event) {
// If the "Enter" key is pressed then process the text in the textarea.
if (event.which == 13) {
var testTextVal = document.getElementById('testtext').value;
var postParams = 'text=' + testTextVal;
var request = new XMLHttpRequest();
request.open('POST', 'keytest/test', true);
request.setRequestHeader(
'Content-type', 'application/x-www-form-urlencoded');
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
console.log('Sent POST request to server.');
}
};
request.onerror = function() {
console.log('Request failed');
};
request.send(postParams);
}
}
window.addEventListener(
'load',
function() {
document.getElementById('testtext').addEventListener(
'keypress',
handleTextareaKeyPressed,
false);
},
false);
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Simple HTTP server used in Chromoting End-to-end tests.
Serves the static host and client pages with associated javascript files.
Stores state about host actions and communicates it to the client.
Built on CherryPy (http://www.cherrypy.org/) and requires the Chromium
version of CherryPy to be installed from
chromium/tools/build/third_party/cherrypy/.
"""
import json
import os
import sys
try:
import cherrypy
except ImportError:
print ('This script requires CherryPy v3 or higher to be installed.\n'
'Please install and try again.')
sys.exit(1)
def HttpMethodsAllowed(methods=['GET', 'HEAD']):
method = cherrypy.request.method.upper()
if method not in methods:
cherrypy.response.headers['Allow'] = ', '.join(methods)
raise cherrypy.HTTPError(405)
cherrypy.tools.allow = cherrypy.Tool('on_start_resource', HttpMethodsAllowed)
class KeyTest(object):
"""Handler for keyboard test in Chromoting E2E tests."""
keytest_succeeded = False
keytest_text = None
@cherrypy.expose
@cherrypy.tools.allow(methods=['POST'])
def test(self, text):
"""Stores status of host keyboard actions."""
self.keytest_succeeded = True
self.keytest_text = text
def process(self):
"""Build the JSON message that will be conveyed to the client."""
message = {
'keypressSucceeded': self.keytest_succeeded,
'keypressText': self.keytest_text
}
# The message is now built so reset state on the server
if self.keytest_succeeded:
self.keytest_succeeded = False
self.keytest_text = None
return message
class Root(object):
"""Root Handler for the server."""
# Every test has its own class which should be instantiated here
keytest = KeyTest()
# Every test's class should have a process method that the client polling
# will call when that test is running.
# The method should be registered here with the test name.
TEST_DICT = {
'keytest': keytest.process
}
@cherrypy.expose
@cherrypy.tools.allow()
def index(self):
"""Index page to test if server is ready."""
return 'Simple HTTP Server for Chromoting Browser Tests!'
@cherrypy.expose
@cherrypy.tools.allow()
def poll(self, test):
"""Responds to poll request from client page with status of host actions."""
if test not in self.TEST_DICT:
cherrypy.response.status = 500
return
cherrypy.response.headers['Content-Type'] = 'application/json'
return json.dumps(self.TEST_DICT[test]())
app_config = {
'/': {
'tools.staticdir.on': True,
'tools.staticdir.dir':
os.path.abspath(os.path.dirname(__file__))
}
}
cherrypy.tree.mount(Root(), '/', config=app_config)
cherrypy.config.update({'server.socket_host': '0.0.0.0',
'server.threadpool': 1,
})
cherrypy.engine.start()
cherrypy.engine.block()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment