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

Side by Side Diff: utils/testrunner/http_server_runner.dart

Issue 11093033: Added ability to have custom test HTTP server handlers in the test directories. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 8 years, 2 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library http_server; 5 library http_server_runner;
6 import 'dart:io'; 6 import 'dart:io';
7 import '../../pkg/args/lib/args.dart'; 7 import 'http_server.dart';
8
9 /** A simple HTTP server. Currently handles serving static files. */
10 class HttpTestServer {
11 HttpServer server;
12
13 /** If set, serve up static files from this directory. */
14 String staticFileDirectory;
15
16 /* A common subset of all possible MIME types. */
17 static const MIME_TYPES = const {
18 'json' : 'applicaton/json',
19 'js' : 'application/javascript',
20 'cgm' : 'image/cgm',
21 'g3fax': 'image/g3fax',
22 'gif' : 'image/gif',
23 'jpeg' : 'image/jpeg',
24 'jpg' : 'image/jpeg',
25 'png' : 'image/png',
26 'tif' : 'image/tiff',
27 'tiff' : 'image/tiff',
28 'ac3' : 'audio/ac3',
29 'mp3' : 'audio/mpeg',
30 'ogg' : 'audio/ogg',
31 'css' : 'text/css',
32 'csv' : 'text/csv',
33 'htm' : 'text/html',
34 'html' : 'text/html',
35 'txt' : 'text/plain',
36 'rtf' : 'text/rtf',
37 'mp4' : 'video/mp4',
38 'qt' : 'video/quicktime',
39 'vc1' : 'video/vc1'
40 };
41
42 HttpTestServer(port, this.staticFileDirectory) {
43 server = new HttpServer();
44 server.listen("127.0.0.1", port);
45 server.onError = (e) {
46 };
47 server.defaultRequestHandler =
48 (HttpRequest request, HttpResponse response) {
49 try {
50 if (staticFileDirectory != null) {
51 String fname = request.path;
52 String path = '$staticFileDirectory$fname';
53 File f = new File(path);
54 if (f.existsSync()) {
55 var p = path.substring(path.lastIndexOf('.') + 1).toLowerCase();
56 if (MIME_TYPES.containsKey(p)) {
57 var ct = MIME_TYPES[p];
58 var idx = ct.indexOf('/');
59 response.headers.contentType =
60 new ContentType(ct.substring(0, idx),
61 ct.substring(idx + 1));
62 }
63 f.openInputStream().pipe(response.outputStream);
64 } else {
65 response.statusCode = HttpStatus.NOT_FOUND;
66 response.reasonPhrase = '$path does not exist';
67 response.outputStream.close();
68 }
69 }
70 } catch(e,s) {
71 response.statusCode = HttpStatus.INTERNAL_SERVER_ERROR;
72 response.reasonPhrase = "$e";
73 response.outputStream.writeString(s.toString());
74 response.outputStream.close();
75 }
76 };
77 }
78
79 void addHandler(Function matcher, handler) {
80 if (handler is Function) {
81 server.addRequestHandler(matcher, handler);
82 } else {
83 server.addRequestHandler(matcher, handler.onRequest);
84 }
85 }
86
87 void close() {
88 server.close();
89 }
90 }
91
92 ArgParser getOptionParser() {
93 var parser = new ArgParser();
94 parser.addOption('port', abbr: 'p',
95 help: 'Set the server listening port.',
96 defaultsTo: '80');
97
98 parser.addOption('root', abbr: 'r',
99 help: 'Set the directory for static files.',
100 defaultsTo: '/tmp');
101 return parser;
102 }
103 8
104 main() { 9 main() {
105 var optionsParser = getOptionParser(); 10 var optionsParser = getOptionParser();
106 try { 11 try {
107 var argResults = optionsParser.parse(new Options().arguments); 12 var argResults = optionsParser.parse(new Options().arguments);
108 var server = new HttpTestServer( 13 var server = new HttpTestServer(
109 int.parse(argResults['port']), 14 int.parse(argResults['port']),
110 argResults['root']); 15 argResults['root']);
111 } catch (e) { 16 } catch (e) {
112 print(e); 17 print(e);
113 print('Usage: http_server_test_runner.dart <options>'); 18 print('Usage: http_server_runner.dart <options>');
114 print(optionsParser.getUsage()); 19 print(optionsParser.getUsage());
115 } 20 }
116 } 21 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698