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

Unified Diff: tools/testing/dart/http_server.dart

Issue 12417004: Update the test runner to use the new dart:io API (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fixes Created 7 years, 9 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: tools/testing/dart/http_server.dart
diff --git a/tools/testing/dart/http_server.dart b/tools/testing/dart/http_server.dart
index d065e0bcaac33f64a9dda61127ccee40c4d58c7f..634cc6220e5144f2dee874554115493f1bde34ed 100644
--- a/tools/testing/dart/http_server.dart
+++ b/tools/testing/dart/http_server.dart
@@ -101,11 +101,12 @@ class TestingServers {
* "Access-Control-Allow-Origin: client:port1
* "Access-Control-Allow-Credentials: true"
*/
- void startServers(String host, {int port: 0, int crossOriginPort: 0}) {
- _startHttpServer(host, port: port);
- _startHttpServer(host,
- port: crossOriginPort,
- allowedPort:_serverList[0].port);
+ Future startServers(String host, {int port: 0, int crossOriginPort: 0}) {
+ return _startHttpServer(host, port: port).then((_) {
+ _startHttpServer(host,
+ port: crossOriginPort,
+ allowedPort:_serverList[0].port);
+ });
}
String httpServerCommandline() {
@@ -125,25 +126,28 @@ class TestingServers {
}
}
- void _startHttpServer(String host, {int port: 0, int allowedPort: -1}) {
- var httpServer = new HttpServer();
- httpServer.onError = (e) {
- DebugLogger.error('HttpServer: an error occured: $e');
- };
- httpServer.defaultRequestHandler = (request, response) {
- _handleFileOrDirectoryRequest(request, response, allowedPort);
- };
- httpServer.addRequestHandler(
- (req) => req.path == "/echo", _handleEchoRequest);
-
- httpServer.listen(host, port);
- _serverList.add(httpServer);
+ Future _startHttpServer(String host, {int port: 0, int allowedPort: -1}) {
+ return HttpServer.bind(host, port).then((HttpServer httpServer) {
+ httpServer.listen(
+ (HttpRequest request) {
Bill Hesse 2013/03/13 12:56:41 Doesn't indentation look nicer (smaller) if we put
Søren Gjesse 2013/03/13 15:17:18 Done.
+ if (request.uri.path == "/echo") {
+ _handleEchoRequest(request, request.response);
+ } else {
+ _handleFileOrDirectoryRequest(
+ request, request.response, allowedPort);
+ }
+ },
+ onError: (e) {
+ DebugLogger.error('HttpServer: an error occured: $e');
+ });
+ _serverList.add(httpServer);
+ });
}
void _handleFileOrDirectoryRequest(HttpRequest request,
HttpResponse response,
int allowedPort) {
- var path = _getFilePathFromRequestPath(request.path);
+ var path = _getFilePathFromRequestPath(request.uri.path);
if (path != null) {
var file = new File.fromPath(path);
file.exists().then((exists) {
@@ -163,7 +167,7 @@ class TestingServers {
}
});
} else {
- if (request.path == '/') {
+ if (request.uri.path == '/') {
var entries = [new _Entry('root_dart', 'root_dart/'),
new _Entry('root_build', 'root_build/'),
new _Entry('echo', 'echo')];
@@ -176,7 +180,7 @@ class TestingServers {
void _handleEchoRequest(HttpRequest request, HttpResponse response) {
response.headers.set("Access-Control-Allow-Origin", "*");
- request.inputStream.pipe(response.outputStream);
+ request.pipe(response);
}
Path _getFilePathFromRequestPath(String urlRequestPath) {
@@ -215,18 +219,18 @@ class TestingServers {
var completer = new Completer();
var entries = [];
- directory.list()
- ..onFile = (filepath) {
- var filename = new Path(filepath).filename;
- entries.add(new _Entry(filename, filename));
- }
- ..onDir = (dirpath) {
- var filename = new Path(dirpath).filename;
- entries.add(new _Entry(filename, '$filename/'));
- }
- ..onDone = (_) {
+ directory.list().listen(
+ (FileSystemEntity fse) {
+ var filename = new Path(fse.path).filename;
+ if (fse is File) {
+ entries.add(new _Entry(filename, filename));
+ } else if (fse is Directory) {
+ entries.add(new _Entry(filename, '$filename/'));
+ }
+ },
+ onDone: () {
completer.complete(entries);
- };
+ });
return completer.future;
}
@@ -237,11 +241,11 @@ class TestingServers {
var header = '''<!DOCTYPE html>
<html>
<head>
- <title>${request.path}</title>
+ <title>${request.uri.path}</title>
</head>
<body>
<code>
- <div>${request.path}</div>
+ <div>${request.uri.path}</div>
<hr/>
<ul>''';
var footer = '''
@@ -252,14 +256,14 @@ class TestingServers {
entries.sort();
- response.outputStream.writeString(header);
+ response.write(header);
for (var entry in entries) {
- response.outputStream.writeString(
- '<li><a href="${new Path(request.path).append(entry.name)}">'
+ response.write(
+ '<li><a href="${new Path(request.uri.path).append(entry.name)}">'
'${entry.displayName}</a></li>');
}
- response.outputStream.writeString(footer);
- response.outputStream.close();
+ response.write(footer);
+ response.close();
}
void _sendFileContent(HttpRequest request,
@@ -296,26 +300,23 @@ class TestingServers {
} else if (path.filename.endsWith('.dart')) {
response.headers.set('Content-Type', 'application/dart');
}
- file.openInputStream().pipe(response.outputStream);
+ file.openRead().pipe(response);
}
void _sendNotFound(HttpRequest request, HttpResponse response) {
// NOTE: Since some tests deliberately try to access non-existent files.
// We might want to remove this warning (otherwise it will show
// up in the debug.log every time).
- DebugLogger.warning('HttpServer: could not find file for request path: '
- '"${request.path}"');
- response.statusCode = HttpStatus.NOT_FOUND;
- try {
- response.outputStream.close();
- } catch (e) {
- if (e is StreamException) {
- DebugLogger.warning('HttpServer: error while closing the response '
- 'stream: $e');
- } else {
- throw e;
- }
+ if (request.uri.path != "/favicon.ico") {
+ DebugLogger.warning('HttpServer: could not find file for request path: '
+ '"${request.uri.path}"');
}
+ response.statusCode = HttpStatus.NOT_FOUND;
+ response.close();
+ response.done.catchError((e) {
+ DebugLogger.warning('HttpServer: error while closing the response '
Bill Hesse 2013/03/13 12:56:41 Put the entire string on the next line?
Søren Gjesse 2013/03/13 15:17:18 Done.
+ 'stream: $e');
+ });
}
}

Powered by Google App Engine
This is Rietveld 408576698