Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(48)

Unified Diff: utils/testrunner/http_server_test_runner.dart

Issue 10966020: Added support for running an HTTP server during the test and being able to serve up static files. L… (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: utils/testrunner/http_server_test_runner.dart
===================================================================
--- utils/testrunner/http_server_test_runner.dart (revision 0)
+++ utils/testrunner/http_server_test_runner.dart (revision 0)
@@ -0,0 +1,115 @@
+#library('server');
Siggi Cherem (dart-lang) 2012/09/20 23:55:41 +cpyright, +docs
gram 2012/09/21 00:25:46 Done.
+#import('dart:io');
+#import('../../pkg/args/lib/args.dart');
+
+class HttpTestServer {
+ HttpServer server;
+ static Map mime = null;
Siggi Cherem (dart-lang) 2012/09/20 23:55:41 nit: rename to mime_types, possibly make it a stat
gram 2012/09/21 00:25:46 That would be nice but for some reason Map literal
Siggi Cherem (dart-lang) 2012/09/21 00:37:53 there are considered if you prefix them with 'cons
+
+ /** If set, serve up static files from this directory. */
+ String staticFileDirectory;
+
+ HttpTestServer(port, this.staticFileDirectory) {
+ if (mime == null) {
+ /* A common subset of all possible MIME types. */
+ mime = {
+ 'json' : 'applicaton/json',
Siggi Cherem (dart-lang) 2012/09/20 23:55:41 FYI - you could possibly read lots of mime types f
gram 2012/09/21 00:25:46 I could, but I think the list I have here is reaso
+ 'js' : 'application/javascript',
+ 'cgm' : 'image/cgm',
+ 'g3fax': 'image/g3fax',
+ 'gif' : 'image/gif',
+ 'jpeg' : 'image/jpeg',
+ 'jpg': 'image/jpeg',
+ 'png': 'image/png',
+ 'tif' : 'image/tiff',
+ 'tiff': 'image/tiff',
+ 'ac3' : 'audio/ac3',
+ 'mp3' : 'audio/mpeg',
+ 'ogg' : 'audio/ogg',
+ 'css' : 'text/css',
+ 'csv' : 'text/csv',
+ 'htm' : 'text/html',
+ 'html' : 'text/html',
+ 'txt' : 'text/plain',
+ 'rtf' : 'text/rtf',
+ 'mp4' : 'video/mp4',
+ 'qt' : 'video/quicktime',
+ 'vc1' : 'video/vc1'
+ };
+ }
+ server = new HttpServer();
+ server.listen("127.0.0.1", port);
+ server.onError = (e) {
+ };
+ server.defaultRequestHandler =
+ (HttpRequest request, HttpResponse response) {
Siggi Cherem (dart-lang) 2012/09/20 23:55:41 I might prefer to move these as methods on this cl
gram 2012/09/21 00:25:46 I don't think so. It uses a factory constructor an
Siggi Cherem (dart-lang) 2012/09/21 00:37:53 seems small enough now, but if this grows, this mi
+ try {
+ if (staticFileDirectory != null) {
+ String fname = request.path;
+ String path = '$staticFileDirectory$fname';
+ File f = new File(path);
+ if (f.existsSync()) {
+ var p = path.substring(path.lastIndexOf('.')+1).toLowerCase();
+ if (mime.containsKey(p)) {
+ var ct = mime[p];
+ var idx = ct.indexOf('/');
+ response.headers.contentType =
+ new ContentType(ct.substring(0, idx), ct.substring(idx+1));
+ }
+ response.statusCode = HttpStatus.OK;
+ response.reasonPhrase = "OK";
+ var content = f.readAsBytesSync();
+ response.outputStream.write(content);
+ response.outputStream.close();
+ } else {
+ response.statusCode = HttpStatus.NOT_FOUND;
+ response.reasonPhrase = "Not Found";
+ response.outputStream.close();
+ }
+ }
+ } catch(e,s) {
+ response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
+ response.reasonPhrase = "$e";
+ response.outputStream.close();
+ }
+ };
+ }
+
+ void addHandler(Function matcher, handler) {
+ if (handler is Function) {
+ server.addRequestHandler(matcher, handler);
+ } else {
+ server.addRequestHandler(matcher, handler.onRequest);
+ }
+ }
+
+ void close() {
+ server.close();
+ }
+}
+
+ArgParser getOptionParser() {
+ var parser = new ArgParser();
+ parser.addOption('port', abbr: 'p',
+ help: 'Set the server listening port.',
+ defaultsTo: '80');
+
+ parser.addOption('root', abbr: 'r',
+ help: 'Set the directory for static files.',
+ defaultsTo: '/tmp');
+ return parser;
+}
+
+main() {
+ var optionsParser = getOptionParser();
+ try {
+ var argResults = optionsParser.parse(new Options().arguments);
+ var server = new HttpTestServer(
+ int.parse(argResults['port']),
+ argResults['root']);
+ } catch (e) {
+ print(e);
+ print('Usage: http_server_test_runner.dart <options>');
+ print(optionsParser.getUsage());
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698